Testes
Utilizamos o framework Jest, para realizar os nossos testes, você pode ler mais sobre ele aqui
Focamos mais em testar a store, mais especificamente nossas actions e a relação delas com o SDK, mas se tivermos alguma mutation que tem uma lógica mais complexa, testaremos ela também.
Como criar um teste
Continuaremos com o exemplo de Products
// Importamos a store
import { createProductsStore } from "./store";
import { STATUS } from "@/consts";
describe("products store", () => {
// Nós mockamos o SDK, para podermos simular seu comportamento
const sdk = {
products: {
getProducts: jest.fn(),
getProduct: jest.fn(),
createProduct: jest.fn(),
updateProduct: jest.fn(),
deleteProduct: jest.fn(),
},
};
// Instanciamos a store e actions
const { actions } = createProductsStore(sdk);
// Criamos um produto fake para nós utilizarmos nos testes
const fakeProduct = {
name: "product",
description: "produto bem legal",
quantity: 100,
price: 15.9,
};
// Ao final de cada teste, é importante resetar as funções que testamos
afterEach(() => {
commit.mockReset();
dispatch.mockReset();
});
describe("actions", () => {
describe("fetchProducts", () => {
it("should fetch products", async () => {
sdk.products.products.getProducts.mockImplementation(() => {
return { success: true, payload: [fakeProducts] };
});
await actions.fetchProduts({ commit });
expect(commit).toCalledWith("setProducts", [fakeProducts]);
});
});
describe("getProduct", () => {
it("should update form when request has success", async () => {
sdk.products.getProduct.mockImplementation(() => {
return { success: true, payload: fakeProduct };
});
await actions.getProduct({ commit }, { id: 1 });
expect(commit).toHaveBeenCalledWith("updateForm", fakeProduct);
expect(sdk.products.getProduct).toBeCalledWith({ id: 1 });
});
it("should not update form when request has not success", async () => {
sdk.products.getProduct.mockImplementation(() => {
return { success: false, payload: null };
});
await actions.getProduct({ commit }, { id: 1 });
expect(commit).not.toBeCalled();
});
});
describe("saveProduct", () => {
it("should create a product when it is new", async () => {
sdk.products.createProduct.mockImplementation(() => {
return { success: true, payload: fakeProduct };
});
const state = {
isNew: true,
product: { ...fakeProduct, id: null },
};
await actions.saveProduct({ commit, state });
expect(commit).toBeCalledWith("updateForm", fakeProduct);
expect(sdk.products.postProduct).toBeCalledWith({
...fakeProduct,
id: null,
});
});
it("should update a product when it is not new", async () => {
sdk.products.updateProduct.mockImplementation(() => {
return { success: true, payload: fakeProduct };
});
const state = {
isNew: false,
product: fakeProduct,
};
await actions.saveProduct({ commit, state });
expect(commit).toBeCalledWith("updateForm", fakeProduct);
expect(sdk.products.updateProduct).toBeCalledWith(fakeProduct);
});
});
describe("deleteProduct", () => {
it("should delete a product", async () => {
await actions.deleteProduct({ commit }, { id: 1 });
expect(sdk.products.deleteProduct).toBeCalled();
});
});
});
});