Skip to content

Commit

Permalink
[chore] Refactor mockStore util
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno Garcia committed Feb 20, 2021
1 parent 54ab52c commit 30b254d
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 25 deletions.
1 change: 1 addition & 0 deletions jest-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "@testing-library/jest-dom";
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ module.exports = {
preset: "@vue/cli-plugin-unit-jest/presets/typescript-and-babel",
transform: {
"^.+\\.vue$": "vue-jest"
}
},
setupFilesAfterEnv: ["<rootDir>/jest-setup.ts"]
};
2 changes: 1 addition & 1 deletion src/components/ProductList.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<ul class="products-list" data-test="products-list">
<ul class="products-list" data-testid="products-list">
<ProductItem
v-for="(product, id) in products"
:key="id"
Expand Down
29 changes: 7 additions & 22 deletions tests/unit/components/ProductList.spec.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
import { shallowMount } from "@vue/test-utils";
import { render } from "@testing-library/vue";
import ProductList from "@/components/ProductList.vue";
import ProductItem from "@/components/ProductItem.vue";
import mockStore from "../mocks/mockStore";

const store = mockStore();
import store from "../mocks/mockStore2";

describe("ProductList.vue", () => {
it("Must have a main container", () => {
const wrapper = shallowMount(ProductList, {
global: {
plugins: [store]
}
});

expect(wrapper.find('[data-test="products-list"]').exists()).toBeTruthy();
});

it("Must use ProductItem component", () => {
const wrapper = shallowMount(ProductList, {
global: {
plugins: [store]
}
});
it("Must render the products", () => {
const { getByText } = render(ProductList, { store });

expect(wrapper.findAllComponents(ProductItem).length).toBe(3);
expect(getByText("Cap")).toBeInTheDocument();
expect(getByText("Shirt")).toBeInTheDocument();
expect(getByText("Mug")).toBeInTheDocument();
});
});
58 changes: 58 additions & 0 deletions tests/unit/mocks/mockStore2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import store from "@/store";

function mockStore(): object {
return {
modules: {
shopping: {
...store.modules.shopping,
state: {
products: [
{
id: "X3W2OPY",
code: "CAP",
name: "Cap",
price: 5,
discounts: ["TWO_X_ONE"],
currency: "€"
},
{
id: "X7R2OPX",
code: "TSHIRT",
name: "Shirt",
price: 20,
discounts: ["BULK"],
currency: "€"
},
{
id: "X2G2OPZ",
code: "MUG",
name: "Mug",
price: 7.5,
discounts: [],
currency: "€"
}
],
summary: {
totalCost: 40,
totalItems: 5,
discountsApplied: [
{
code: "test code 1",
literal: "test literal 1",
total: 3
},
{
code: "test code 2",
literal: "test literal 2",
total: 9
}
],
totalCostWithDiscounts: 23
}
}
}
}
};
}

export default mockStore();
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"src/**/*.tsx",
"src/**/*.vue",
"tests/**/*.ts",
"tests/**/*.tsx"
"tests/**/*.tsx",
"./jest-setup.ts"
],
"exclude": [
"node_modules"
Expand Down

0 comments on commit 30b254d

Please sign in to comment.