All files / tests/store-non-namespaced index.ts

70.83% Statements 17/24
100% Branches 0/0
36.36% Functions 4/11
73.91% Lines 17/23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87    4x 4x 4x 4x   4x   4x                                       4x         4x             4x     1x     1x       4x   12x     60x                 1x     1x       4x                            
/* eslint-disable no-param-reassign, no-shadow */
 
import Vuex from "vuex";
import Vue from "vue";
import deepEqual from "fast-deep-equal";
import undoRedo, { scaffoldStore } from "@/undoRedo";
 
Vue.use(Vuex);
 
const debug = process.env.NODE_ENV !== "production";
 
interface State {
  list: Array<any>;
  shadow: Array<any>;
}
 
interface Payload {
  index: number;
  item?: any;
}
 
interface Context {
  commit: Function;
  state: any;
  getters: any;
  rootState: any;
  rootGetters: any;
}
 
const state: State = {
  list: [],
  shadow: []
};
 
const getters = {
  getList: ({ list }: State) => list,
  getItem: (state: State) => ({ item }: Payload) =>
    state.list.find(i => deepEqual(i, item)),
  getShadow: ({ shadow }: State) => shadow
};
 
const actions = {
  // NB: add/remove shadow actions to test undo/redo callback actions
  addShadow({ commit }: Context, { item }: Payload) {
    commit("addShadow", { item });
  },
  removeShadow({ commit }: Context, { index }: Payload) {
    commit("removeShadow", { index });
  }
};
 
const mutations = {
  emptyState: (state: State) => {
    state.list = [];
  },
  addItem: (state: State, { item }: Payload) => {
    state.list = [...state.list, item];
  },
  updateItem: (state: State, { item, index }: Payload) => {
    state.list.splice(index, 1, item);
  },
  removeItem: (state: State, { index }: Payload) => {
    state.list.splice(index, 1);
  },
  addShadow: (state: State, { item }: Payload) => {
    state.shadow = [...state.shadow, item];
  },
  removeShadow: (state: State, { index }: Payload) => {
    state.shadow.splice(index, 1);
  }
};
 
export default new Vuex.Store(
  scaffoldStore({
    plugins: [
      undoRedo({
        ignoreMutations: ["addShadow", "removeShadow"]
      })
    ],
    strict: debug,
    state,
    getters,
    actions,
    mutations
  })
);