Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5ce4d9d

Browse files
committedOct 13, 2020
Fix vee-validate tests
1 parent 1f513f3 commit 5ce4d9d

File tree

3 files changed

+49
-59
lines changed

3 files changed

+49
-59
lines changed
 

‎package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"msw": "^0.21.2",
6868
"portal-vue": "^2.1.7",
6969
"typescript": "^3.8.3",
70-
"vee-validate": "^2.2.15",
70+
"vee-validate": "^4.0.0-beta.8",
7171
"vue": "^3.0.0",
7272
"vue-apollo": "^3.0.4",
7373
"vue-i18n": "^9.0.0-beta.4",

‎src/__tests__/components/Validate.vue

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,35 @@
11
<template>
2-
<form>
3-
<label id="username-label" for="username">Username</label>
2+
<Form>
3+
<Field :rules="validateEmail" name="email" as="input" type="email" />
44

5-
<input
6-
v-model="username"
7-
v-validate="'required|email'"
8-
placeholder="Username..."
9-
name="username"
10-
aria-labelledby="username-label"
11-
/>
5+
<error-message name="email" data-testid="error-message" />
126

13-
<span v-if="errors.has('username')" data-testid="username-errors">{{
14-
errors.first('username')
15-
}}</span>
16-
17-
<label id="password-label" for="password">Password</label>
18-
<input
19-
v-model="password"
20-
placeholder="Password..."
21-
type="password"
22-
name="password"
23-
aria-labelledby="password-label"
24-
/>
25-
<button type="submit">Submit</button>
26-
</form>
7+
<button>Sign up</button>
8+
</Form>
279
</template>
2810

2911
<script>
12+
import {Form, Field, ErrorMessage} from 'vee-validate'
13+
3014
export default {
31-
data: () => ({
32-
username: '',
33-
password: ''
34-
})
15+
name: 'VeeValidate',
16+
components: {
17+
Form,
18+
Field,
19+
ErrorMessage,
20+
},
21+
methods: {
22+
validateEmail(value) {
23+
if (!value) {
24+
return 'This field is required'
25+
}
26+
27+
if (!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(value)) {
28+
return 'This field must be a valid email'
29+
}
30+
31+
return true
32+
},
33+
},
3534
}
3635
</script>

‎src/__tests__/validate-plugin.js

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
1-
test.todo('Your test suite must contain at least one test.')
2-
3-
// // Notice this example is using vee-validate v2.X
4-
// import VeeValidate from 'vee-validate'
5-
// import '@testing-library/jest-dom'
6-
7-
// import {render, fireEvent} from '@testing-library/vue'
8-
// import Validate from './components/Validate'
9-
10-
// test('can validate using plugin', async () => {
11-
// // The third argument of `render` is a callback function that receives the
12-
// // Vue instance as a parameter. This way, we can register plugins such as
13-
// // VeeValidate.
14-
// const {getByPlaceholderText, queryByTestId, getByTestId} = render(
15-
// Validate,
16-
// {},
17-
// vue => vue.use(VeeValidate, {events: 'blur'}),
18-
// )
19-
20-
// // Assert error messages are not in the DOM when rendering the component.
21-
// expect(queryByTestId('username-errors')).toBeNull()
22-
23-
// const usernameInput = getByPlaceholderText('Username...')
24-
// await fireEvent.touch(usernameInput)
25-
26-
// // After "touching" the input (focusing and blurring), validation error
27-
// // should appear.
28-
// expect(getByTestId('username-errors')).toHaveTextContent(
29-
// /the username field is required/i,
30-
// )
31-
// })
1+
import '@testing-library/jest-dom'
2+
3+
import {render, fireEvent} from '@testing-library/vue'
4+
import VeeValidate from './components/Validate'
5+
6+
test('can validate using plugin', async () => {
7+
const {findByText, getByRole, getByTestId} = render(VeeValidate)
8+
9+
// Assert error messages are not in the DOM when rendering the component.
10+
expect(getByTestId('error-message')).toBeEmptyDOMElement()
11+
12+
const emailInput = getByRole('textbox')
13+
14+
await fireEvent.touch(emailInput)
15+
16+
await findByText('This field is required')
17+
18+
await fireEvent.update(emailInput, 'an invalid email')
19+
await fireEvent.blur(emailInput)
20+
21+
await findByText('This field must be a valid email')
22+
})

0 commit comments

Comments
 (0)
Please sign in to comment.