Unit testing of actions and getters with jest #501
Answered
by
posva
Oleksii14
asked this question in
Help and Questions
-
Hello! Can you provide a TypeScript based example of how can I test my actions and getters with jest? Here is how I image that: // pinia-store.ts
import { defineStore } from "pinia";
interface State {
mockData: string;
}
const state = (): State => ({
mockData: "",
});
// What type should be added to actions?
export const actions = {
testAction() {
this.mockData = "test"
}
}
export const useGeneralStore = defineStore({
id: "test-id",
state,
actions,
}); // pinia-store.spec.ts
import { actions } from './pinia-store'
describe("pinia store", () => {
desctibe("actions", () => {
const state = {
mockData: ""
}
const action = actions.testAction.bind(state)
action()
expect(state.mockData).toEqual("test")
})
}) |
Beta Was this translation helpful? Give feedback.
Answered by
posva
May 27, 2021
Replies: 1 comment 4 replies
-
Don't split the store definition and then you can create a pinia: // pinia-store.spec.ts
import { useGeneralStore } from './pinia-store'
describe("pinia store", () => {
it("actions", () => {
const pinia = createPinia()
const store = useGeneralStore(pinia)
store.testAction()
expect(store.mockData).toEqual("test")
})
}) |
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
Oleksii14
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Don't split the store definition and then you can create a pinia: