aboutsummaryrefslogtreecommitdiffstats
path: root/cypress/e2e/files/scrolling.cy.ts
blob: c4ca4943a408ae4a7e71f8a6c40b03b3617a6c7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*!
 * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

import type { User } from '@nextcloud/cypress'
import { getRowForFile } from './FilesUtils'

describe('files: Scrolling to selected file in file list', { testIsolation: true }, () => {
	const fileIds = new Map<number, string>()
	let user: User

	before(() => {
		cy.createRandomUser().then(($user) => {
			user = $user

			cy.rm(user, '/welcome.txt')
			for (let i = 1; i <= 10; i++) {
				cy.uploadContent(user, new Blob([]), 'text/plain', `/${i}.txt`)
					.then((response) => fileIds.set(i, Number.parseInt(response.headers['oc-fileid']).toString()))
			}
		})
	})

	beforeEach(() => {
		// Adjust height to ensure that those 10 elements can not be rendered in one list
		cy.viewport(1200, 6 * 55 /* rows */ + 55 /* table header */ + 50 /* navigation header */ + 50 /* breadcrumbs */ + 46 /* file filters */)
		cy.login(user)
	})

	it('Can see first file in list', () => {
		cy.visit(`/apps/files/files/${fileIds.get(1)}`)

		// See file is visible
		getRowForFile('1.txt')
			.should('be.visible')

		// we expect also element 6 to be visible
		getRowForFile('6.txt')
			.should('be.visible')
		// but not element 7 - though it should exist (be buffered)
		getRowForFile('7.txt')
			.should('exist')
			.and('not.be.visible')
	})

	// Same kind of tests for partially visible top and bottom
	for (let i = 2; i <= 5; i++) {
		it(`correctly scrolls to row ${i}`, () => {
			cy.visit(`/apps/files/files/${fileIds.get(i)}`)

			// See file is visible
			getRowForFile(`${i}.txt`)
				.should('be.visible')
				.and(notBeOverlappedByTableHeader)

			// we expect also element +4 to be visible
			// (6 visible rows -> 5 without our scrolled row -> so we only have 4 fully visible others + two 1/2 hidden rows)
			getRowForFile(`${i + 4}.txt`)
				.should('be.visible')
			// but not element -1 or +5 - though it should exist (be buffered)
			getRowForFile(`${i - 1}.txt`)
				.should('exist')
				.and(beOverlappedByTableHeader)
			getRowForFile(`${i + 5}.txt`)
				.should('exist')
				.and('be.visible')
				.and(notBeFullyInViewport)
		})
	}

	// this will have half of the footer visible
	it(`correctly scrolls to row 6`, () => {
		cy.visit(`/apps/files/files/${fileIds.get(6)}`)

		// See file is visible
		getRowForFile(`6.txt`)
			.should('be.visible')
			.and(notBeOverlappedByTableHeader)

		// we expect also element 7,8,9,10 visible
		getRowForFile(`10.txt`)
			.should('be.visible')
		// but not row 5
		getRowForFile(`5.txt`)
			.should('exist')
			.and(beOverlappedByTableHeader)
		// see footer is only shown partly
		cy.get('tfoot')
			.should('exist')
			.and(notBeFullyInViewport)
	})

	// Same kind of tests for partially visible top and bottom
	for (let i = 7; i <= 10; i++) {
		it(`correctly scrolls to row ${i}`, () => {
			cy.visit(`/apps/files/files/${fileIds.get(i)}`)

			// See file is visible
			getRowForFile(`${i}.txt`)
				.should('be.visible')
				.and(notBeOverlappedByTableHeader)

			// there are only max. 3 rows left so also row 6+ should be visible
			getRowForFile(`6.txt`)
				.should('be.visible')
			getRowForFile(`10.txt`)
				.should('be.visible')
			// Also the footer is visible
			cy.get('tfoot')
				.contains('10 files')
				.should('be.visible')
		})
	}
})

describe('files: Scrolling to selected file in file list (GRID MODE)', { testIsolation: true }, () => {
	const fileIds = new Map<number, string>()
	let user: User

	before(() => {
		cy.createRandomUser().then(($user) => {
			user = $user

			cy.rm(user, '/welcome.txt')
			for (let i = 1; i <= 12; i++) {
				cy.uploadContent(user, new Blob([]), 'text/plain', `/${i}.txt`)
					.then((response) => fileIds.set(i, Number.parseInt(response.headers['oc-fileid']).toString()))
			}
			// Set grid mode
			cy.login(user)
			cy.intercept('**/apps/files/api/v1/config/grid_view').as('setGridMode')
			cy.visit('/apps/files')
			cy.findByRole('button', { name: 'Switch to grid view' })
				.should('be.visible')
				.click()
			cy.wait('@setGridMode')
		})
	})

	beforeEach(() => {
		// Adjust height to ensure that those 12 files can not be rendered in one list
		// 768px width will limit the columns to 3
		cy.viewport(768, 3 * 246 /* rows */ + 55 /* table header */ + 50 /* navigation header */ + 50 /* breadcrumbs */ + 46 /* file filters */)
		cy.login(user)
	})

	// First row
	for (let i = 1; i <= 3; i++) {
		it(`Can see files in first row (file ${i})`, () => {
			cy.visit(`/apps/files/files/${fileIds.get(i)}`)

			for (let j = 1; j <= 3; j++) {
				// See all files of that row are visible
				getRowForFile(`${j}.txt`)
					.should('be.visible')
				// we expect also the second row to be visible
				getRowForFile(`${j+3}.txt`)
					.should('be.visible')
				// Because there is no half row on top we also see the third row
				getRowForFile(`${j+6}.txt`)
					.should('be.visible')
				// But not the forth row
				getRowForFile(`${j+9}.txt`)
					.should('exist')
					.and(notBeFullyInViewport)
			}
		})
	}

	// Second row
	// Same kind of tests for partially visible top and bottom
	for (let i = 4; i <= 6; i++) {
		it(`correctly scrolls to second row (file ${i})`, () => {
			cy.visit(`/apps/files/files/${fileIds.get(i)}`)

			// See all three files of that row are visible
			for (let j = 4; j <= 6; j++) {
				getRowForFile(`${j}.txt`)
					.should('be.visible')
					.and(notBeOverlappedByTableHeader)
				// we expect also the next row to be visible
				getRowForFile(`${j + 3}.txt`)
					.should('be.visible')
				// but not the row below (should be half cut)
				getRowForFile(`${j + 6}.txt`)
					.should('exist')
					.and(notBeFullyInViewport)
				// Same for the row above
				getRowForFile(`${j - 3}.txt`)
					.should('exist')
					.and(beOverlappedByTableHeader)
			}
		})
	}

	// Third row
	// this will have half of the footer visible and half of the previous row
	for (let i = 7; i <= 9; i++) {
		it(`correctly scrolls to third row (file ${i})`, () => {
			cy.visit(`/apps/files/files/${fileIds.get(i)}`)

			// See all three files of that row are visible
			for (let j = 7; j <= 9; j++) {
				getRowForFile(`${j}.txt`)
					.should('be.visible')
				// we expect also the next row to be visible
				getRowForFile(`${j + 3}.txt`)
					.should('be.visible')
				// but not the row above
				getRowForFile(`${j - 3}.txt`)
					.should('exist')
					.and(beOverlappedByTableHeader)
			}

			// see footer is only shown partly
			cy.get('tfoot')
				.should('exist')
				.and(notBeFullyInViewport)
		})
	}

	// Forth row which only has row 4 and 3 visible and the full footer
	for (let i = 10; i <= 12; i++) {
		it(`correctly scrolls to forth row (file ${i})`, () => {
			cy.visit(`/apps/files/files/${fileIds.get(i)}`)

			// See all three files of that row are visible
			for (let j = 10; j <= 12; j++) {
				getRowForFile(`${j}.txt`)
					.should('be.visible')
					.and(notBeOverlappedByTableHeader)
				// we expect also the row above to be visible
				getRowForFile(`${j - 3}.txt`)
					.should('be.visible')
			}

			// see footer is shown
			cy.get('tfoot')
				.should('be.visible')
		})
	}
})

/// Some helpers

function notBeOverlappedByTableHeader($el: JQuery<HTMLElement>) {
	return beOverlappedByTableHeader($el, false)
}

function beOverlappedByTableHeader($el: JQuery<HTMLElement>, expected = true) {
	const headerRect = Cypress.$('thead').get(0)!.getBoundingClientRect()
	const elementRect = $el.get(0)!.getBoundingClientRect()
	const overlap = !(headerRect.right < elementRect.left || 
		headerRect.left > elementRect.right || 
		headerRect.bottom < elementRect.top || 
		headerRect.top > elementRect.bottom)

	if (expected) {
		expect(overlap, 'Overlapped by table header').to.be.true
	} else {
		expect(overlap, 'Not overlapped by table header').to.be.false
	}
}

function beFullyInViewport($el: JQuery<HTMLElement>, expected = true) {
	const { top, left, bottom, right } = $el.get(0)!.getBoundingClientRect()
	const { innerHeight, innerWidth } = window
	const fullyVisible = top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth

	if (expected) {
		expect(fullyVisible, 'Fully within viewport').to.be.true
	} else {
		expect(fullyVisible, 'Not fully within viewport').to.be.false
	}
}

function notBeFullyInViewport($el: JQuery<HTMLElement>) {
	return beFullyInViewport($el, false)
}