I can't get Gettters mocking to work.... #1255
Answered
by
posva
fermaud
asked this question in
Help and Questions
-
Hi, import { createTestingPinia } from '@pinia/testing';
import { setActivePinia, createPinia, defineStore } from 'pinia';
const useCounter = defineStore('newStore', {
state: () => ({ firstName: 'Tom', lastName: 'Hanks' }),
getters: {
getFirstName(state) {
return state.firstName;
},
getLastName(state) {
return state.lastName;
},
getFullName(state) {
return `${state.getFirstName} ${state.getLastName}`;
},
},
actions: {
updateUserFirstName(firstName) {
this.firstName = firstName;
},
updateUserLastName(lastName) {
this.lastName = lastName;
},
updateUserName(firstName, lastName) {
this.updateUserFirstName(firstName);
this.updateUserLastName(lastName);
},
},
});
describe('Counter Store', () => {
it('update user names', () => {
const pinia = createTestingPinia({ stubActions: false });
const counter = useCounter(pinia);
counter.updateUserName('John', 'Doe');
expect(counter.updateUserFirstName).toHaveBeenCalledTimes(1);
});
it('get full name', () => {
const pinia = createTestingPinia();
const counter = useCounter(pinia);
console.log(counter.getFirstName); // = Tom
counter.getFirstName = 'Rob';
console.log(counter.getFirstName); // = Tom and not Rob...
expect(counter.getFullName).toBe('Rob Hanks');
});
}); |
Beta Was this translation helpful? Give feedback.
Answered by
posva
May 2, 2022
Replies: 1 comment 1 reply
-
Example: import { createTestingPinia } from '@pinia/testing';
import { setActivePinia, createPinia, defineStore } from 'pinia';
const useCounter = defineStore('newStore', {
state: () => ({ firstName: 'Tom', lastName: 'Hanks' }),
getters: {
getFullName(state) {
return `${state.firstName} ${state.lastName}`;
},
},
actions: {
updateUserName(firstName, lastName) {
this.firstName = firstName
this.lastName = lastName
},
},
});
it('get full name', () => {
const counter = useCounter(createPinia());
counter.firstName = 'Rob';
expect(counter.getFullName).toBe('Rob Hanks');
});
}); Note that getters mocking doesn't work on Vue 2 #1201 |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
posva
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
state
in getters is just state, not getters, so it should bestate.firstName
notstate.getFirstName
setFirstName
Example: