aboutsummaryrefslogtreecommitdiffstats
path: root/cypress/e2e/files/search.cy.ts
blob: 59c0019acb4ddb9176f5491c33abdb006d48c538 (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
/*!
 * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

import type { User } from '@nextcloud/cypress'
import { FilesNavigationPage } from '../../pages/FilesNavigation'
import { getRowForFile, navigateToFolder } from './FilesUtils'

describe('files: search', () => {

	let user: User

	const navigation = new FilesNavigationPage()

	before(() => {
		cy.createRandomUser().then(($user) => {
			user = $user
			cy.mkdir(user, '/some folder')
			cy.mkdir(user, '/some folder/nested folder')
			cy.mkdir(user, '/other folder')
			cy.mkdir(user, '/12345')
			cy.uploadContent(user, new Blob(['content']), 'text/plain', '/file.txt')
			cy.uploadContent(user, new Blob(['content']), 'text/plain', '/some folder/a file.txt')
			cy.uploadContent(user, new Blob(['content']), 'text/plain', '/some folder/a second file.txt')
			cy.uploadContent(user, new Blob(['content']), 'text/plain', '/some folder/nested folder/deep file.txt')
			cy.uploadContent(user, new Blob(['content']), 'text/plain', '/other folder/another file.txt')
			cy.login(user)
		})
	})

	beforeEach(() => {
		cy.visit('/apps/files')
	})

	it('updates the query on the URL', () => {
		navigation.searchScopeTrigger().click()
		navigation.searchScopeMenu()
			.should('be.visible')
			.findByRole('menuitem', { name: /search globally/i })
			.should('be.visible')
			.click()

		navigation.searchInput().type('file')
		cy.url().should('match', /query=file($|&)/)
	})

	it('can search globally', () => {
		navigation.searchScopeTrigger().click()
		navigation.searchScopeMenu()
			.should('be.visible')
			.findByRole('menuitem', { name: /search globally/i })
			.should('be.visible')
			.click()
		navigation.searchInput().type('file')

		getRowForFile('file.txt').should('be.visible')
		getRowForFile('a file.txt').should('be.visible')
		getRowForFile('a second file.txt').should('be.visible')
		getRowForFile('another file.txt').should('be.visible')
	})

	it('filter does also search locally', () => {
		navigateToFolder('some folder')
		getRowForFile('a file.txt').should('be.visible')

		navigation.searchInput().type('file')

		getRowForFile('a file.txt').should('be.visible')
		getRowForFile('a second file.txt').should('be.visible')
		getRowForFile('deep file.txt').should('be.visible')
		cy.get('[data-cy-files-list-row-fileid]').should('have.length', 3)
	})

	it('See "search everywhere" button', () => {
		// Not visible initially
		cy.get('[data-cy-files-filters]')
			.findByRole('button', { name: /Search everywhere/i })
			.should('not.to.exist')

		// add a filter
		navigation.searchInput().type('file')

		// see its visible
		cy.get('[data-cy-files-filters]')
			.findByRole('button', { name: /Search everywhere/i })
			.should('be.visible')

		// clear the filter
		navigation.searchClearButton().click()

		// see its not visible again
		cy.get('[data-cy-files-filters]')
			.findByRole('button', { name: /Search everywhere/i })
			.should('not.to.exist')
	})

	it('can make local search a global search', () => {
		navigateToFolder('some folder')
		getRowForFile('a file.txt').should('be.visible')

		navigation.searchInput().type('file')

		// see local results
		getRowForFile('a file.txt').should('be.visible')
		getRowForFile('a second file.txt').should('be.visible')
		getRowForFile('deep file.txt').should('be.visible')
		cy.get('[data-cy-files-list-row-fileid]').should('have.length', 3)

		// toggle global search
		cy.get('[data-cy-files-filters]')
			.findByRole('button', { name: /Search everywhere/i })
			.should('be.visible')
			.click()

		// see global results
		getRowForFile('file.txt').should('be.visible')
		getRowForFile('a file.txt').should('be.visible')
		getRowForFile('deep file.txt').should('be.visible')
		getRowForFile('a second file.txt').should('be.visible')
		getRowForFile('another file.txt').should('be.visible')
	})

	it('shows empty content when there are no results', () => {
		navigateToFolder('some folder')
		getRowForFile('a file.txt').should('be.visible')

		navigation.searchScopeTrigger().click()
		navigation.searchScopeMenu()
			.should('be.visible')
			.findByRole('menuitem', { name: /search globally/i })
			.should('be.visible')
			.click()
		navigation.searchInput().type('xyz')

		// see the empty content message
		cy.contains('[role="note"]', /No search results for .xyz./)
			.should('be.visible')
			.within(() => {
				// see within there is a search box with the same value
				cy.findByRole('searchbox', { name: /search for files/i })
					.should('be.visible')
					.and('have.value', 'xyz')
			})
	})

	it('can alter search', () => {
		navigation.searchScopeTrigger().click()
		navigation.searchScopeMenu()
			.should('be.visible')
			.findByRole('menuitem', { name: /search globally/i })
			.should('be.visible')
			.click()
		navigation.searchInput().type('other')

		getRowForFile('another file.txt').should('be.visible')
		getRowForFile('other folder').should('be.visible')
		cy.get('[data-cy-files-list-row-fileid]').should('have.length', 2)

		navigation.searchInput().type(' file')
		navigation.searchInput().should('have.value', 'other file')
		getRowForFile('another file.txt').should('be.visible')
		cy.get('[data-cy-files-list-row-fileid]').should('have.length', 1)
	})

	it('returns to file list if search is cleared', () => {
		navigation.searchScopeTrigger().click()
		navigation.searchScopeMenu()
			.should('be.visible')
			.findByRole('menuitem', { name: /search globally/i })
			.should('be.visible')
			.click()
		navigation.searchInput().type('other')

		getRowForFile('another file.txt').should('be.visible')
		getRowForFile('other folder').should('be.visible')
		cy.get('[data-cy-files-list-row-fileid]').should('have.length', 2)

		navigation.searchClearButton().click()
		navigation.searchInput().should('have.value', '')
		getRowForFile('file.txt').should('be.visible')
		cy.get('[data-cy-files-list-row-fileid]').should('have.length', 5)
	})

	/**
	 * Problem:
	 * 1. Being on the search view
	 * 2. Press the refresh button (name of the current view)
	 * 3. See that the router link does not preserve the query
	 *
	 * We fix this with a navigation guard and need to verify that it works
	 */
	it('keeps the query in the URL', () => {
		navigation.searchScopeTrigger().click()
		navigation.searchScopeMenu()
			.should('be.visible')
			.findByRole('menuitem', { name: /search globally/i })
			.should('be.visible')
			.click()
		navigation.searchInput().type('file')

		// see that the search view is loaded
		getRowForFile('a file.txt').should('be.visible')
		// see the correct url
		cy.url().should('match', /query=file($|&)/)

		cy.intercept('SEARCH', '**/remote.php/dav/').as('search')
		// refresh the view
		cy.findByRole('button', { description: /reload current directory/i }).click()
		// wait for the request
		cy.wait('@search')
		// see that the search view is reloaded
		getRowForFile('a file.txt').should('be.visible')
		// see the correct url
		cy.url().should('match', /query=file($|&)/)
	})
})