Skip to content

Commit

Permalink
feat: added test cases and fallbacks for unresolved cases
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Dec 18, 2020
1 parent e82e28e commit 71bda03
Show file tree
Hide file tree
Showing 21 changed files with 367 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/vee-validate/src/useFieldError.ts
@@ -1,7 +1,7 @@
import { computed, unref } from 'vue';
import { FormErrorsSymbol } from './symbols';
import { MaybeReactive } from './types';
import { getFromPath, injectWithSelf } from './utils';
import { injectWithSelf } from './utils';

/**
* Gives access to a single field error
Expand All @@ -10,6 +10,6 @@ export function useFieldError(path: MaybeReactive<string>) {
const errors = injectWithSelf(FormErrorsSymbol);

return computed(() => {
return getFromPath(errors?.value, unref(path)) as string | undefined;
return errors?.value?.[unref(path)] as string | undefined;
});
}
6 changes: 5 additions & 1 deletion packages/vee-validate/src/useFormErrors.ts
@@ -1,11 +1,15 @@
import { computed } from 'vue';
import { FormErrorsSymbol } from './symbols';
import { injectWithSelf } from './utils';

/**
* Gives access to all form errors
*/
export function useFormErrors() {
const errors = injectWithSelf(FormErrorsSymbol);
const errors = injectWithSelf(
FormErrorsSymbol,
computed(() => ({}))
);

return errors;
}
2 changes: 1 addition & 1 deletion packages/vee-validate/src/useIsFieldDirty.ts
Expand Up @@ -9,6 +9,6 @@ export function useIsFieldDirty(path: string) {
const form = injectWithSelf(FormSymbol);

return computed(() => {
return form?.fields.value[path]?.meta.dirty;
return form?.fields.value[path]?.meta.dirty as boolean | undefined;
});
}
2 changes: 1 addition & 1 deletion packages/vee-validate/src/useIsFieldTouched.ts
Expand Up @@ -9,6 +9,6 @@ export function useIsFieldTouched(path: string) {
const form = injectWithSelf(FormSymbol);

return computed(() => {
return form?.fields.value[path]?.meta.touched;
return form?.fields.value[path]?.meta.touched as boolean | undefined;
});
}
2 changes: 1 addition & 1 deletion packages/vee-validate/src/useIsFieldValid.ts
Expand Up @@ -9,6 +9,6 @@ export function useIsFieldValid(path: string) {
const form = injectWithSelf(FormSymbol);

return computed(() => {
return form?.fields.value[path]?.meta.valid;
return form?.fields.value[path]?.meta.valid as boolean | undefined;
});
}
9 changes: 7 additions & 2 deletions packages/vee-validate/src/useResetForm.ts
@@ -1,11 +1,16 @@
import { FormSymbol } from './symbols';
import { FormState } from './types';
import { injectWithSelf } from './utils';
import { injectWithSelf, warn } from './utils';

export function useResetForm<TValues = Record<string, any>>() {
const form = injectWithSelf(FormSymbol);

return function resetForm(state?: Partial<FormState<TValues>>) {
return form?.resetForm(state);
if (!form) {
warn('No form context was detected');
return;
}

return form.resetForm(state);
};
}
8 changes: 7 additions & 1 deletion packages/vee-validate/src/useValidateField.ts
@@ -1,6 +1,6 @@
import { FormSymbol } from './symbols';
import { ValidationResult } from './types';
import { injectWithSelf } from './utils';
import { injectWithSelf, warn } from './utils';

/**
* Validates a single field
Expand All @@ -10,6 +10,12 @@ export function useValidateField(path: string) {

return function validateField(): Promise<ValidationResult> {
const field = form?.fields.value[path];
if (!field) {
warn(`field with name ${path} was not found`);
return Promise.resolve({
errors: [],
});
}

if (Array.isArray(field)) {
return field[0].validate();
Expand Down
9 changes: 7 additions & 2 deletions packages/vee-validate/src/useValidateForm.ts
@@ -1,5 +1,5 @@
import { FormSymbol } from './symbols';
import { injectWithSelf } from './utils';
import { injectWithSelf, warn } from './utils';

/**
* Validate multiple fields
Expand All @@ -8,6 +8,11 @@ export function useValidateForm() {
const form = injectWithSelf(FormSymbol);

return function validateField() {
return form?.validate();
if (!form) {
warn('No form context was detected, undefined is returned');
return Promise.resolve(undefined);
}

return form.validate();
};
}
4 changes: 4 additions & 0 deletions packages/vee-validate/src/utils/common.ts
Expand Up @@ -128,3 +128,7 @@ export function injectWithSelf<T>(symbol: InjectionKey<T>, def: T | undefined =

return inject(symbol, vm?.provides[symbol as any] || def);
}

export function warn(message: string) {
console.warn(`[vee-validate]: ${message}`);
}
39 changes: 39 additions & 0 deletions packages/vee-validate/tests/useFieldErrors.spec.ts
Expand Up @@ -31,4 +31,43 @@ describe('useFieldError()', () => {
await flushPromises();
expect(error?.textContent).toBe(REQUIRED_MESSAGE);
});

test('returns undefined if field not found', async () => {
mountWithHoc({
setup() {
useForm();
const message = useFieldError('something');

return {
message,
};
},
template: `
<span>{{ message }}</span>
`,
});

await flushPromises();
const error = document.querySelector('span');
expect(error?.textContent).toBe('');
});

test('returns undefined if form is not found', async () => {
mountWithHoc({
setup() {
const message = useFieldError('something');

return {
message,
};
},
template: `
<span>{{ message }}</span>
`,
});

await flushPromises();
const error = document.querySelector('span');
expect(error?.textContent).toBe('');
});
});
19 changes: 19 additions & 0 deletions packages/vee-validate/tests/useFormErrors.spec.ts
Expand Up @@ -31,4 +31,23 @@ describe('useFormErrors()', () => {
await flushPromises();
expect(error?.textContent).toBe(REQUIRED_MESSAGE);
});

test('returns empty object if form is not found', async () => {
mountWithHoc({
setup() {
const messages = useFormErrors();

return {
messages,
};
},
template: `
<span>{{ messages }}</span>
`,
});

await flushPromises();
const error = document.querySelector('span');
expect(error?.textContent).toBe('{}');
});
});
39 changes: 39 additions & 0 deletions packages/vee-validate/tests/useIsFieldDirty.spec.ts
Expand Up @@ -31,4 +31,43 @@ describe('useIsFieldDirty()', () => {
await flushPromises();
expect(error?.textContent).toBe('true');
});

test('returns undefined if field does not exist', async () => {
mountWithHoc({
setup() {
useForm();
const isDirty = useIsFieldDirty('something');

return {
isDirty,
};
},
template: `
<span>{{ isDirty }}</span>
`,
});

await flushPromises();
const error = document.querySelector('span');
expect(error?.textContent).toBe('');
});

test('returns undefined if form does not exist', async () => {
mountWithHoc({
setup() {
const isDirty = useIsFieldDirty('something');

return {
isDirty,
};
},
template: `
<span>{{ isDirty }}</span>
`,
});

await flushPromises();
const error = document.querySelector('span');
expect(error?.textContent).toBe('');
});
});
39 changes: 39 additions & 0 deletions packages/vee-validate/tests/useIsFieldTouched.spec.ts
Expand Up @@ -32,4 +32,43 @@ describe('useIsFieldTouched()', () => {
await flushPromises();
expect(error?.textContent).toBe('true');
});

test('returns undefined if field does not exist', async () => {
mountWithHoc({
setup() {
useForm();
const isTouched = useIsFieldTouched('something');

return {
isTouched,
};
},
template: `
<span>{{ isTouched }}</span>
`,
});

await flushPromises();
const error = document.querySelector('span');
expect(error?.textContent).toBe('');
});

test('returns undefined if form does not exist', async () => {
mountWithHoc({
setup() {
const isTouched = useIsFieldTouched('something');

return {
isTouched,
};
},
template: `
<span>{{ isTouched }}</span>
`,
});

await flushPromises();
const error = document.querySelector('span');
expect(error?.textContent).toBe('');
});
});
39 changes: 39 additions & 0 deletions packages/vee-validate/tests/useIsFieldValid.spec.ts
Expand Up @@ -34,4 +34,43 @@ describe('useIsFieldValid()', () => {
await flushPromises();
expect(span?.textContent).toBe('true');
});

test('returns undefined if field is not found', async () => {
mountWithHoc({
setup() {
useForm();
const isValid = useIsFieldValid('test');

return {
isValid,
};
},
template: `
<span>{{ isValid }}</span>
`,
});

await flushPromises();
const span = document.querySelector('span');
expect(span?.textContent).toBe('');
});

test('returns undefined if form is not found', async () => {
mountWithHoc({
setup() {
const isValid = useIsFieldValid('test');

return {
isValid,
};
},
template: `
<span>{{ isValid }}</span>
`,
});

await flushPromises();
const span = document.querySelector('span');
expect(span?.textContent).toBe('');
});
});
20 changes: 20 additions & 0 deletions packages/vee-validate/tests/useIsFormDirty.spec.ts
Expand Up @@ -30,4 +30,24 @@ describe('useIsFormDirty()', () => {
await flushPromises();
expect(error?.textContent).toBe('true');
});

test('returns undefined if form is not found', async () => {
mountWithHoc({
setup() {
const isDirty = useIsFormDirty();

return {
isDirty,
};
},
template: `
<span>{{ isDirty }}</span>
`,
});

const error = document.querySelector('span');

await flushPromises();
expect(error?.textContent).toBe('');
});
});
19 changes: 19 additions & 0 deletions packages/vee-validate/tests/useIsFormTouched.spec.ts
Expand Up @@ -30,4 +30,23 @@ describe('useIsFormTouched()', () => {
await flushPromises();
expect(error?.textContent).toBe('true');
});

test('returns undefined if form is not found', async () => {
mountWithHoc({
setup() {
const isTouched = useIsFormTouched();

return {
isTouched,
};
},
template: `
<span>{{ isTouched }}</span>
`,
});

await flushPromises();
const error = document.querySelector('span');
expect(error?.textContent).toBe('');
});
});
19 changes: 19 additions & 0 deletions packages/vee-validate/tests/useIsFormValid.spec.ts
Expand Up @@ -34,4 +34,23 @@ describe('useIsFormValid()', () => {
await flushPromises();
expect(span?.textContent).toBe('true');
});

test('returns undefined if form is not found', async () => {
mountWithHoc({
setup() {
const isValid = useIsFormValid();

return {
isValid,
};
},
template: `
<span>{{ isValid }}</span>
`,
});

await flushPromises();
const span = document.querySelector('span');
expect(span?.textContent).toBe('');
});
});

0 comments on commit 71bda03

Please sign in to comment.