Created
October 23, 2023 17:07
-
-
Save scvnc/7adf87776aa3773644c79c0fe48cd3f8 to your computer and use it in GitHub Desktop.
A way of stubbing out VeeValidate form field
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
/** | |
* Stub out the component with this | |
*/ | |
export const StubFieldComponent = { | |
props: ["name"], | |
setup: (props: { name: string }): unknown => { | |
if (props.name === undefined) { | |
throw new Error("did not recv props!"); | |
} | |
const field = useField<unknown>(props.name); | |
return { | |
setFieldValueForTest(v: unknown) { | |
field.value.value = v; | |
}, | |
}; | |
}, | |
template: "<div>Field = {{ name }}</div>", | |
}; | |
/** | |
* Obtain the form field and allow it to collect the wrapper | |
*/ | |
const findFormField = (_wrapper: VueWrapper, name: string) => { | |
const stubField = _wrapper | |
.findAllComponents(StubFieldComponent) | |
.find((_stubField) => _stubField.props("name") === name); | |
const exists = () => stubField !== undefined; | |
const assertExists = () => { | |
if (!exists()) { | |
throw new Error(`Cant find a stubbed out Field with name=${name}`); | |
} | |
}; | |
return { | |
setValue: async (val: unknown) => { | |
assertExists(); | |
if (stubField === undefined) { | |
throw new Error(`Cant find a stubbed out Field with name=${name}`); | |
} | |
stubField.vm.setFieldValueForTest(val); | |
}, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment