From 4b1ac294bdeab1f0ed268ea29bd6ae0b1cbd393f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9=20Viricel?= Date: Thu, 3 Dec 2020 23:54:59 +0100 Subject: [PATCH] Tested all fruit mutations --- coverage/clover.xml | 4 +- coverage/lcov-report/Fruits.vue.html | 2 +- coverage/lcov-report/index.html | 2 +- src/store/index.js | 14 +++--- src/store/mutations.js | 11 ++++- tests/unit/mutations.spec.js | 71 +++++++++++++++------------- 6 files changed, 61 insertions(+), 43 deletions(-) diff --git a/coverage/clover.xml b/coverage/clover.xml index c7ea88d..e3d58b3 100644 --- a/coverage/clover.xml +++ b/coverage/clover.xml @@ -1,6 +1,6 @@ - - + + diff --git a/coverage/lcov-report/Fruits.vue.html b/coverage/lcov-report/Fruits.vue.html index 84c8f7a..b63bfae 100644 --- a/coverage/lcov-report/Fruits.vue.html +++ b/coverage/lcov-report/Fruits.vue.html @@ -141,7 +141,7 @@ export default { diff --git a/coverage/lcov-report/index.html b/coverage/lcov-report/index.html index 45f3b12..99a9ab7 100644 --- a/coverage/lcov-report/index.html +++ b/coverage/lcov-report/index.html @@ -94,7 +94,7 @@ diff --git a/src/store/index.js b/src/store/index.js index e5b51c0..18d5fdb 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -1,13 +1,15 @@ import Vue from "vue"; import Vuex from "vuex"; +import mutations from "./mutations"; + +export const state = { + fruits: [], + fruit: {} +}; Vue.use(Vuex); export default new Vuex.Store({ - state: { - fruits: [] - }, - mutations: import("./mutations.js"), - actions: {}, - modules: {} + state, + mutations }); diff --git a/src/store/mutations.js b/src/store/mutations.js index 8005d85..9b50d7c 100644 --- a/src/store/mutations.js +++ b/src/store/mutations.js @@ -1,5 +1,14 @@ export default { - setFruits(state, fruits) { + setFruits: (state, fruits) => { state.fruits = fruits; + }, + setFruit: (state, fruit) => { + state.fruit = fruit; + }, + addFruit: (state, fruit) => { + state.fruits = [...state.fruits, fruit]; + }, + removeFruit: (state, id) => { + state.fruits = state.fruits.filter(item => item.id != id); } }; diff --git a/tests/unit/mutations.spec.js b/tests/unit/mutations.spec.js index 53759d2..228fb1c 100644 --- a/tests/unit/mutations.spec.js +++ b/tests/unit/mutations.spec.js @@ -1,36 +1,43 @@ -import mutations from "@/store/mutations.js"; +import { state } from "@/store"; +import mutations from "@/store/mutations"; + +const { setFruits, setFruit, addFruit, removeFruit } = mutations; + +let fruit = { + id: 3, + isFruit: true, + name: "banana", + image: "/image/path.jpg", + price: "907.00", + color: "#763e44", + description: "Iusto illum vero voluptatem.", + taste: "Handcrafted", + expires: "2021-04-11T08:54:24.588Z" +}; describe("Test Vuex mutations.", () => { - it("should add fruits to the state", () => { - const fruits = [ - { - id: 3, - isFruit: true, - name: "banana", - image: "/image/path.jpg", - price: "907.00", - color: "#763e44", - description: "Iusto illum vero voluptatem.", - taste: "Handcrafted", - expires: "2021-04-11T08:54:24.588Z" - } - ]; - const state = { - fruits: [] - }; - mutations.setFruits(state, fruits); - expect(state.fruits).toEqual([ - { - id: 3, - isFruit: true, - name: "banana", - image: "/image/path.jpg", - price: "907.00", - color: "#763e44", - description: "Iusto illum vero voluptatem.", - taste: "Handcrafted", - expires: "2021-04-11T08:54:24.588Z" - } - ]); + it("tests setFruits mutation.", () => { + setFruits(state, [fruit]); + expect(state.fruits[0].id).toBe(3); + }); + + it("tests setFruit mutation.", () => { + setFruit(state, fruit); + expect(state.fruit.id).toBe(3); + }); + + it("tests addFruit mutation.", () => { + const newFruit = Object.assign({}, fruit); + newFruit.id = 4; + addFruit(state, newFruit); + expect(state.fruits[0].id).toBe(3); + expect(state.fruits[1].id).toBe(4); + expect(state.fruits.length).toBe(2); + }); + + it("tests removeFruit mutation.", () => { + removeFruit(state, 4); + expect(state.fruits[0].id).toBe(3); + expect(state.fruits.length).toBe(1); }); });