Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use flushPromise() instead of waitFor() #283

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"msw": "^0.21.3",
"tsd": "^0.19.1",
"typescript": "^4.4.3",
"vant": "^3.6.4",
"vee-validate": "^4.3.5",
"vue": "^3.2.12",
"vue-apollo": "^3.0.5",
Expand Down
44 changes: 44 additions & 0 deletions src/__tests__/components/VantValidate.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<script setup>
import { ref, reactive } from 'vue';

const loginForm = reactive({
username: '',
password: '',
});

const msg = ref('empty')

const submitLogin = async () => {
msg.value = 'validation passed';
}
</script>

<template>
<div>
<van-form data-testid="form" @submit="submitLogin">
<van-cell-group inset>
<van-field
v-model="loginForm.username"
label="username"
placeholder="username"
:rules="[{ required: true, message: 'please input username' }]"
/>
<van-field
v-model="loginForm.password"
type="password"
label="password"
placeholder="password"
:rules="[{ required: true, message: 'please input password' }]"
/>
</van-cell-group>
<div>
<van-button
native-type="submit"
>
login
</van-button>
</div>
</van-form>
</div>
<span>{{ msg }}</span>
</template>
18 changes: 18 additions & 0 deletions src/__tests__/fire-event.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {h} from 'vue'
import Vant from 'vant'
import {render, fireEvent} from '..'
import Button from './components/Button'
import VantValidate from './components/VantValidate'
import '@testing-library/jest-dom'

const eventTypes = [
{
Expand Down Expand Up @@ -274,3 +277,18 @@ test('fireEvent.update handles input file', async () => {

expect(console.warn).not.toHaveBeenCalled()
})

test('triggers form validation', async () => {
const { getByPlaceholderText, getByTestId, getByText } = render(VantValidate, {
global: {
plugins: [Vant]
}
});
expect(getByText('empty')).toBeInTheDocument();

await fireEvent.update(getByPlaceholderText('username'), 'user');
await fireEvent.update(getByPlaceholderText('password'), 'psw');
await fireEvent.submit(getByTestId('form'));

expect(getByText('validation passed')).toBeInTheDocument();
})
8 changes: 4 additions & 4 deletions src/fire-event.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable testing-library/no-wait-for-empty-callback */
import {waitFor, fireEvent as dtlFireEvent} from '@testing-library/dom'
import {fireEvent as dtlFireEvent} from '@testing-library/dom'
import {flushPromises} from '@vue/test-utils'

// Vue Testing Lib's version of fireEvent will call DOM Testing Lib's
// version of fireEvent. The reason is because we need to wait another
Expand All @@ -8,15 +8,15 @@ import {waitFor, fireEvent as dtlFireEvent} from '@testing-library/dom'

async function fireEvent(...args) {
dtlFireEvent(...args)
await waitFor(() => {})
await flushPromises()
}

Object.keys(dtlFireEvent).forEach(key => {
fireEvent[key] = async (...args) => {
warnOnChangeOrInputEventCalledDirectly(args[1], key)

dtlFireEvent[key](...args)
await waitFor(() => {})
await flushPromises()
}
})

Expand Down