Skip to content

Commit cb1a6f7

Browse files
authored
Merge pull request #55442 from nextcloud/refactor/migrate-cypress-tests
test: migrate `LoginForm` component test to vitest
2 parents 2fd8795 + 62539ec commit cb1a6f7

File tree

8 files changed

+112
-85
lines changed

8 files changed

+112
-85
lines changed

core/src/components/login/LoginForm.cy.ts

Lines changed: 0 additions & 76 deletions
This file was deleted.

core/src/components/login/LoginForm.vue

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,10 @@ export default {
194194
}
195195
},
196196
197-
data() {
197+
data(props) {
198198
return {
199199
loading: false,
200-
user: '',
200+
user: props.username,
201201
password: '',
202202
}
203203
},
@@ -262,7 +262,7 @@ export default {
262262
},
263263
264264
emailEnabled() {
265-
return this.emailStates ? this.emailStates.every((state) => state === '1') : 1
265+
return this.emailStates.every((state) => state === '1')
266266
},
267267
268268
loginText() {
@@ -286,7 +286,6 @@ export default {
286286
if (this.username === '') {
287287
this.$refs.user.$refs.inputField.$refs.input.focus()
288288
} else {
289-
this.user = this.username
290289
this.$refs.password.$refs.inputField.$refs.input.focus()
291290
}
292291
},
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*!
2+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
import { cleanup, render } from '@testing-library/vue'
7+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
8+
import LoginForm from '../../../components/login/LoginForm.vue'
9+
10+
describe('core: LoginForm', () => {
11+
afterEach(cleanup)
12+
13+
beforeEach(() => {
14+
// Mock the required global state
15+
window.OC = {
16+
theme: {
17+
name: 'J\'s cloud',
18+
},
19+
requestToken: 'request-token',
20+
}
21+
})
22+
23+
/**
24+
* Ensure that characters like ' are not double HTML escaped.
25+
* This was a bug in https://github.com/nextcloud/server/issues/34990
26+
*/
27+
it('does not double escape special characters in product name', () => {
28+
const page = render(LoginForm, {
29+
props: {
30+
username: 'test-user',
31+
},
32+
})
33+
34+
const heading = page.getByRole('heading', { level: 2 })
35+
expect(heading.textContent).toContain('J\'s cloud')
36+
})
37+
38+
it('offers email as login name by default', async () => {
39+
const page = render(LoginForm)
40+
41+
const input = await page.findByRole('textbox', { name: /Account name or email/ })
42+
expect(input).toBeInstanceOf(HTMLInputElement)
43+
})
44+
45+
it('offers only account name if email is not enabled', async () => {
46+
const page = render(LoginForm, {
47+
propsData: {
48+
emailStates: ['0', '1'],
49+
},
50+
})
51+
52+
await expect(async () => page.findByRole('textbox', { name: /Account name or email/ })).rejects.toThrow()
53+
await expect(page.findByRole('textbox', { name: /Account name/ })).resolves.not.toThrow()
54+
})
55+
56+
it('fills username from props into form', () => {
57+
const page = render(LoginForm, {
58+
props: {
59+
username: 'test-user',
60+
},
61+
})
62+
63+
page.debug()
64+
65+
const input: HTMLInputElement = page.getByRole('textbox', { name: /Account name or email/ })
66+
expect(input.id).toBe('user')
67+
expect(input.name).toBe('user')
68+
expect(input.value).toBe('test-user')
69+
})
70+
71+
describe('', () => {
72+
beforeAll(() => {
73+
vi.useFakeTimers()
74+
// mock timeout of 5 seconds
75+
const state = document.createElement('input')
76+
state.type = 'hidden'
77+
state.id = 'initial-state-core-loginTimeout'
78+
state.value = btoa(JSON.stringify(5))
79+
document.body.appendChild(state)
80+
})
81+
82+
afterAll(() => {
83+
vi.useRealTimers()
84+
document.querySelector('#initial-state-core-loginTimeout')?.remove()
85+
})
86+
87+
it('clears password after timeout', () => {
88+
// mount forms
89+
const page = render(LoginForm)
90+
const input: HTMLInputElement = page.getByLabelText('Password', { selector: 'input' })
91+
input.dispatchEvent(new InputEvent('input', { data: 'MyPassword' }))
92+
93+
vi.advanceTimersByTime(2500)
94+
// see its still the value
95+
expect(input.value).toBe('')
96+
97+
// Wait for timeout
98+
vi.advanceTimersByTime(2600)
99+
expect(input.value).toBe('')
100+
})
101+
})
102+
})

dist/core-login.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/core-login.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eslint.config.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ export default defineConfig([
6363
rules: {
6464
'no-console': 'off',
6565
'jsdoc/require-jsdoc': 'off',
66+
'jsdoc/require-param-type': 'off',
67+
'jsdoc/require-param-description': 'off',
6668
'@typescript-eslint/no-explicit-any': 'off',
6769
'@typescript-eslint/no-unused-expressions': 'off',
6870
},

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
"@testing-library/cypress": "^10.1.0",
142142
"@testing-library/jest-dom": "^6.6.4",
143143
"@testing-library/user-event": "^14.6.1",
144-
"@testing-library/vue": "^5.8.3",
144+
"@testing-library/vue": "^5.9.0",
145145
"@types/dockerode": "^3.3.44",
146146
"@types/wait-on": "^5.3.4",
147147
"@vitejs/plugin-vue2": "^2.3.3",

0 commit comments

Comments
 (0)