forked from factorial-io/undo-redo-vuex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-action-group-undo.js
112 lines (97 loc) · 2.88 KB
/
test-action-group-undo.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import test from 'ava';
import Vue from 'vue';
import plain from 'vue-plain';
import store from './store-non-namespaced';
// Get plain objects from vue/vuex getter/setter objects (https://github.com/Coffcer/vue-plain)
Vue.use(plain);
const redo = () => store.dispatch('redo');
const undo = () => store.dispatch('undo');
const item = (id, label) => ({ id, label });
const firstGroupItems = [
item(0, 'First group item one'),
item(1, 'First group item two'),
];
const itemWithoutGroup = item(2, 'Item without group');
const secondGroupItems = [
item(3, 'Second group item one'),
item(4, 'Second group item two'),
item(5, 'Second group item three'),
];
test.serial('Add all items', async t => {
// Commit the items with action groups to the store and assert
await store.commit('addItem', {
item: firstGroupItems[0],
actionGroup: 'firstGroup',
});
await store.commit('addItem', {
item: firstGroupItems[1],
actionGroup: 'firstGroup',
});
await store.commit('addItem', { item: itemWithoutGroup }); // no group here
await store.commit('addItem', {
item: secondGroupItems[0],
actionGroup: 'secondGroup',
});
await store.commit('addItem', {
item: secondGroupItems[1],
actionGroup: 'secondGroup',
});
await store.commit('addItem', {
item: secondGroupItems[2],
actionGroup: 'secondGroup',
});
t.deepEqual(Vue.plain(store.state.list), [
...firstGroupItems,
itemWithoutGroup,
...secondGroupItems,
]);
});
test.serial('Undo secondGroup should remove all secondGroup items', async t => {
await undo();
t.deepEqual(Vue.plain(store.state.list), [
...firstGroupItems,
itemWithoutGroup,
]);
});
test.serial('Redo secondGroup should restore full list', async t => {
await redo();
t.deepEqual(Vue.plain(store.state.list), [
...firstGroupItems,
itemWithoutGroup,
...secondGroupItems,
]);
});
test.serial('Undo secondGroup', async t => {
await undo();
t.deepEqual(Vue.plain(store.state.list), [
...firstGroupItems,
itemWithoutGroup,
]);
});
test.serial('Undo secondGroup and mutation without group', async t => {
await undo();
t.deepEqual(Vue.plain(store.state.list), firstGroupItems);
});
test.serial('Undo firstGroup as well should leave list empty', async t => {
await undo();
t.deepEqual(Vue.plain(store.state.list), []);
});
test.serial('Redo firstGroup should restore items from firstGroup', async t => {
await redo();
t.deepEqual(Vue.plain(store.state.list), firstGroupItems);
});
test.serial('Redo mutation without group should restore that item', async t => {
await redo();
t.deepEqual(Vue.plain(store.state.list), [
...firstGroupItems,
itemWithoutGroup,
]);
});
test.serial('Redo secondGroup should restore full list again', async t => {
await redo();
t.deepEqual(Vue.plain(store.state.list), [
...firstGroupItems,
itemWithoutGroup,
...secondGroupItems,
]);
});