-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { computed } from 'vue'; | ||
import { FormSymbol } from './symbols'; | ||
import { injectWithSelf, warn } from './utils'; | ||
|
||
/** | ||
* The number of form's submission count | ||
*/ | ||
export function useSubmitCount() { | ||
const form = injectWithSelf(FormSymbol); | ||
if (!form) { | ||
warn('No vee-validate <Form /> or `useForm` was detected in the component tree'); | ||
} | ||
|
||
return computed(() => { | ||
return form?.submitCount.value ?? 0; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import flushPromises from 'flush-promises'; | ||
import { useField, useForm, useSubmitCount } from '@/vee-validate'; | ||
import { mountWithHoc } from './helpers'; | ||
|
||
describe('useSubmitCount()', () => { | ||
test('indicates the number of submissions', async () => { | ||
mountWithHoc({ | ||
setup() { | ||
const { submitForm } = useForm(); | ||
useField('test'); | ||
const submitCount = useSubmitCount(); | ||
|
||
return { | ||
submitCount, | ||
submitForm, | ||
}; | ||
}, | ||
template: ` | ||
<button @click="submitForm()">Submit</button> | ||
<span>{{ submitCount }}</span> | ||
`, | ||
}); | ||
|
||
await flushPromises(); | ||
const button = document.querySelector('button'); | ||
const submitText = document.querySelector('span'); | ||
expect(submitText?.textContent).toBe('0'); | ||
button?.click(); | ||
await flushPromises(); | ||
expect(submitText?.textContent).toBe('1'); | ||
}); | ||
|
||
test('returns 0 and warns if form is not found', async () => { | ||
const spy = jest.spyOn(console, 'warn').mockImplementation(); | ||
mountWithHoc({ | ||
setup() { | ||
const submitCount = useSubmitCount(); | ||
|
||
return { | ||
submitCount, | ||
}; | ||
}, | ||
template: ` | ||
<span>{{ submitCount }}</span> | ||
`, | ||
}); | ||
|
||
await flushPromises(); | ||
const submitText = document.querySelector('span'); | ||
expect(submitText?.textContent).toBe('0'); | ||
expect(console.warn).toHaveBeenCalled(); | ||
spy.mockRestore(); | ||
}); | ||
}); |