-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
3 changed files
with
92 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { computed } from 'vue'; | ||
import { FormSymbol } from './symbols'; | ||
import { injectWithSelf } from './utils'; | ||
|
||
/** | ||
* If a field is validated and is valid | ||
*/ | ||
export function useIsFieldValid(path: string) { | ||
const form = injectWithSelf(FormSymbol); | ||
|
||
return computed(() => { | ||
return form?.fields.value[path]?.meta.valid; | ||
}); | ||
} | ||
|
||
/** | ||
* If the form has been validated and is valid | ||
*/ | ||
export function useIsFormValid() { | ||
const form = injectWithSelf(FormSymbol); | ||
|
||
return computed(() => { | ||
return form?.meta.value.valid; | ||
}); | ||
} |
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,66 @@ | ||
import flushPromises from 'flush-promises'; | ||
import { useIsFormValid, useField, useIsFieldValid, useForm } from '@/vee-validate'; | ||
import { mountWithHoc, setValue } from './helpers'; | ||
|
||
describe('useIsValid()', () => { | ||
const REQUIRED_MESSAGE = 'Field is required'; | ||
const validate = (val: any) => (val ? true : REQUIRED_MESSAGE); | ||
|
||
test('returns the validity of a single field', async () => { | ||
mountWithHoc({ | ||
setup() { | ||
useForm(); | ||
const { value } = useField('test', validate); | ||
const isValid = useIsFieldValid('test'); | ||
|
||
return { | ||
value, | ||
isValid, | ||
}; | ||
}, | ||
template: ` | ||
<input name="field" v-model="value" /> | ||
<span>{{ isValid.toString() }}</span> | ||
`, | ||
}); | ||
await flushPromises(); | ||
|
||
const input = document.querySelector('input'); | ||
const span = document.querySelector('span'); | ||
setValue(input as any, ''); | ||
await flushPromises(); | ||
expect(span?.textContent).toBe('false'); | ||
setValue(input as any, '12'); | ||
await flushPromises(); | ||
expect(span?.textContent).toBe('true'); | ||
}); | ||
|
||
test('returns validity of the form', async () => { | ||
mountWithHoc({ | ||
setup() { | ||
useForm(); | ||
const { value } = useField('test', validate); | ||
const isValid = useIsFormValid(); | ||
|
||
return { | ||
value, | ||
isValid, | ||
}; | ||
}, | ||
template: ` | ||
<input name="field" v-model="value" /> | ||
<span>{{ isValid.toString() }}</span> | ||
`, | ||
}); | ||
|
||
await flushPromises(); | ||
const input = document.querySelector('input'); | ||
const span = document.querySelector('span'); | ||
setValue(input as any, ''); | ||
await flushPromises(); | ||
expect(span?.textContent).toBe('false'); | ||
setValue(input as any, '12'); | ||
await flushPromises(); | ||
expect(span?.textContent).toBe('true'); | ||
}); | ||
}); |