I'm working on a survey builder in Vue, and survey questions which the user creates are committed to Vuex so they can be retrieved later like so:
computed: {
inputs() {
return this.$store.getters.questions(this.pageNumber);
},
},
pageNumber
is a prop the component receives and inputs()
returns an array of questions. This all seems to work in terms of rendering the correct questions on screen but I'm having trouble with Jest tests.
In order to test I was hoping I could mock the store with getters like my attempt below (omitting some parts):
const localVue = createLocalVue();
localVue.use(Vuex);
beforeEach(() => {
state = {
survey: {
pages: [
// pages objects
],
},
};
getters = {
questions: () => [
{ type: 'Radio', config: { label: 'Test label', options: [{ label: 'Test option label' }] }, validation: [] },
],
};
store = new Vuex.Store({
state,
getters,
});
});
But this results in the error:
TypeError: this.$store.getters.questions is not a function
However, removing that arrow function from getters.questions gives me:
[vuex] getters should be function but "getters.questions" is [{"type":"Radio","config":{"label":"Test label","options":[{"label":"Test option label"}]},"validation":[]}].
So I think I could be completely misunderstanding. Could someone point me in the right direction?
"TypeError: Cannot read property 'questions' of undefined"
. I presume this is to do with the way I've structured my store/getters? However it seems to be working in the actual UI of the application. Apologies, I'm probably missing something obvious! – Neoimpressionism