This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
tests.js
148 lines (142 loc) · 4.56 KB
/
tests.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const { test } = require('@ianwalter/bff')
const Vue = require('vue')
const Vuex = require('vuex')
const clone = require('@ianwalter/clone')
const merge = require('@ianwalter/merge')
const VuexReset = require('.')
Vue.use(Vuex)
test('state is reset when trigger mutation executed', t => {
const state = { message: 'Hello!', with: { my: { e: 'yes', open: true } } }
const store = new Vuex.Store({
plugins: [VuexReset()],
state: clone(state),
mutations: {
message: (state, message) => (state.message = message),
data: (state, data) => merge(state, data),
reset: () => {}
}
})
store.commit('message', 'Greetings!')
store.commit('reset')
t.expect(store.state).toEqual(state)
store.commit('message', 'Yo!')
store.commit('reset')
t.expect(store.state).toEqual(state)
store.commit('data', { with: { my: { open: false } } })
t.expect(store.state.with).toEqual({ my: { e: 'yes', open: false } })
store.commit('reset')
t.expect(store.state).toEqual(state)
})
test('only module state is reset when module mutation executed', t => {
const rootMessage = 'Yo!'
const songName = 'One Touch'
const state = { message: 'Hello!' }
const songState = { name: 'Messy Love', collections: [], map: {} }
const store = new Vuex.Store({
plugins: [VuexReset()],
state: clone(state),
mutations: {
message: (state, message) => (state.message = message),
reset: () => {}
},
modules: {
'sample/song': {
namespaced: true,
state: clone(songState),
mutations: {
name: (state, name) => (state.name = name),
collection: (state, collection) => {
state.collections.push(collection)
state.map[collection] = 1
},
reset: () => {}
}
}
}
})
store.commit('message', rootMessage)
store.commit('sample/song/name', songName)
t.expect(store.state['sample/song'].name).toBe(songName)
store.commit('sample/song/collection', 'Summer')
t.expect(store.state['sample/song'].collections).toEqual(['Summer'])
t.expect(store.state['sample/song'].map).toEqual({ Summer: 1 })
store.commit('sample/song/reset')
t.expect(store.state.message).toBe(rootMessage)
t.expect(store.state['sample/song']).toEqual(songState)
store.commit('sample/song/collection', 'Dance')
store.commit('reset')
t.expect(store.state['sample/song']).toEqual(songState)
})
test('module state can be reset when registered dynamically', t => {
const rootMessage = 'Yo!'
const songName = 'One Touch'
const state = { message: 'Hello!' }
const songState = { name: 'Messy Love', collections: [] }
const song = {
namespaced: true,
state: clone(songState),
mutations: {
name: (state, name) => (state.name = name),
collection: (state, collection) => state.collections.push(collection),
reset: () => {}
}
}
const store = new Vuex.Store({
plugins: [VuexReset()],
state: clone(state),
mutations: {
message: (state, message) => (state.message = message),
reset: () => {}
}
})
store.registerModuleState('song', song)
store.commit('message', rootMessage)
store.commit('song/name', songName)
t.expect(store.state.song.name).toBe(songName)
store.commit('song/collection', 'Summer')
t.expect(store.state.song.collections).toEqual(['Summer'])
store.commit('song/reset')
t.expect(store.state.message).toEqual(rootMessage)
t.expect(store.state.song).toEqual(songState)
})
test('ssr state is used but can reset to initial state', t => {
const message = 'Yo!'
const state = { message: 'Hello!', song: 'The Wheel' }
const store = new Vuex.Store({
plugins: [VuexReset({ ssr: { message, song: null } })],
state: clone(state),
mutations: {
reset: () => true
}
})
t.expect(store.state.message).toBe(message)
store.commit('reset')
t.expect(store.state).toEqual(state)
})
test('current route state is kept if it exists when reset', t => {
const state = { message: 'Hello!' }
const route = { path: '/' }
const path = '/welcome'
const store = new Vuex.Store({
plugins: [VuexReset()],
state: clone(state),
modules: {
route: {
namespaced: true,
state: clone(route),
mutations: {
path: (state, path) => (state.path = path)
}
}
},
mutations: {
message: (state, message) => (state.message = message),
reset: () => {}
}
})
store.commit('route/path', path)
store.commit('message', 'Greetings!')
store.commit('reset')
t.expect(store.state.message).toBe(state.message)
t.expect(store.state.route.path).toBe(path)
})