All files / tests/store/modules list.ts

66.67% Statements 14/21
100% Branches 0/0
36.36% Functions 4/11
70% Lines 14/20
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  1x 1x   1x   1x         1x                                       1x     1x     1x       1x   3x     12x                 1x     1x       1x                
/* eslint-disable no-param-reassign, no-shadow */
import deepEqual from "fast-deep-equal";
import { scaffoldStore } from "../../../src/undoRedo";
 
const debug = process.env.NODE_ENV !== "production";
 
const state = {
  list: [],
  shadow: []
};
 
const getters = {
  getList: ({ list }: { list: Array<any> }) => list,
  getItem: (state: any) => ({ item }: { item: any }) =>
    state.list.find((i: any) => deepEqual(i, item)),
  getShadow: ({ shadow }: { shadow: Array<any> }) => shadow
};
 
interface Context {
  commit: Function;
  state: any;
  getters: any;
  rootState: any;
  rootGetters: any;
}
 
interface Payload {
  index?: number;
  item?: any;
}
 
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 });
  }
};
 
export const mutations = {
  emptyState: (state: any) => {
    state.list = [];
  },
  addItem: (state: any, { item }: { item: any }) => {
    state.list = [...state.list, item];
  },
  updateItem: (state: any, { item, index }: Payload) => {
    state.list.splice(index, 1, item);
  },
  removeItem: (state: any, { index }: Payload) => {
    state.list.splice(index, 1);
  },
  addShadow: (state: any, { item }: Payload) => {
    state.shadow = [...state.shadow, item];
  },
  removeShadow: (state: any, { index }: Payload) => {
    state.shadow.splice(index, 1);
  }
};
 
export default scaffoldStore({
  state,
  getters,
  actions,
  mutations,
  namespaced: true,
  debug
});