-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.js
156 lines (128 loc) · 4.39 KB
/
test.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
149
150
151
152
153
154
155
156
var test = require('tape')
var choo = require('choo')
var html = require('choo/html')
var nanobus = require('nanobus')
var createStore = require('./')
test('errors', t => {
t.throws(() => createStore(), /storeName required/, 'throws on empty call')
t.throws(() => createStore({}), /storeName required/, 'throws on missing storeName')
t.throws(() => createStore({ storeName: 'foo' }), /initialState required/, 'throws on missing initialState')
t.throws(() => createStore({ storeName: 'foo', initialState: {} }), /events required/, 'throws on missing events')
t.end()
})
test('storeName', t => {
var storeName = 'demo'
var store = createStore({ storeName, initialState: {}, events: {} })
t.equals(store.storeName, storeName, 'has storeName prop')
t.end()
})
test('integration', t => {
var app, objStore, arrStore, resetStore, globalStore
t.test('instantiation', t => {
t.doesNotThrow(() => {
objStore = createStore({
storeName: 'obj',
initialState: { a: 1 },
events: {
increment: function ({ store }) {
store.a++
}
}
})
}, 'create object store')
t.doesNotThrow(() => {
arrStore = createStore({
storeName: 'arr',
initialState: [1],
events: {
increment: function ({ data, store }) {
store[0]++
}
}
})
}, 'create array store')
t.doesNotThrow(() => {
resetStore = createStore({
storeName: 'reset',
initialState: { a: 3 },
events: {
reset: function ({ store }) {
store.a = store.a * store.a
}
}
})
}, 'create reset store')
t.doesNotThrow(() => {
globalStore = createStore({
storeName: 'global',
initialState: { navigations: 0 },
events: {
navigate: function ({ store }) {
store.navigations++
}
}
})
}, 'create global (navigation) store')
t.end()
})
t.test('registration', t => {
t.doesNotThrow(() => {
app = choo()
app.use(objStore)
app.use(arrStore)
app.use(resetStore)
app.use(globalStore)
app.route('/', (state, emit) => html`<body></body>`)
app.toString('/')
}, 'stores register')
t.end()
})
t.test('initialState', t => {
t.equals(app.state.obj.a, 1, 'objStore state is good')
t.equals(app.state.arr[0], 1, 'arrStore state is good')
t.equals(app.state.reset.a, 3, 'resetStore state is good')
t.equals(app.state.global.navigations, 0, 'globalStore state is good')
t.end()
})
t.test('events/actions', t => {
t.doesNotThrow(objStore.actions.increment, 'object increment works')
t.doesNotThrow(arrStore.actions.increment, 'array increment works')
t.equals(app.state.obj.a, 2, 'arrStore state updated')
t.equals(app.state.arr[0], 2, 'arrStore state updated')
t.end()
})
t.test('global navigation', t => {
t.notOk(globalStore.actions.navigate, 'global event not namespaced')
app.emit(app.state.events.NAVIGATE)
t.equals(app.state.global.navigations, 1, 'globalStore state updated')
t.end()
})
t.test('reset', t => {
t.doesNotThrow(objStore.actions.reset, 'object reset works')
t.doesNotThrow(arrStore.actions.reset, 'array reset works')
t.doesNotThrow(resetStore.actions.reset, 'custom reset works')
t.equals(app.state.obj.a, 1, 'object reset state is good')
t.equals(app.state.arr[0], 1, 'array reset state is good')
t.equals(app.state.reset.a, 9, 'custom reset state is good')
t.end()
})
t.test('reset with render', t => {
t.doesNotThrow(() => objStore.actions.reset({ render: true }), 'object reset render works')
t.doesNotThrow(() => arrStore.actions.reset({ render: true }), 'array reset render works')
t.doesNotThrow(() => resetStore.actions.reset({ render: true }), 'custom reset render works')
t.equals(app.state.obj.a, 1, 'object reset state is good')
t.equals(app.state.arr[0], 1, 'array reset state is good')
t.equals(app.state.reset.a, 81, 'custom reset state is good')
t.end()
})
t.test('does not throw on missing state.events', t => {
var store = createStore({
storeName: 'nope',
initialState: { a: 1 },
events: { increment: ({ store }) => (store.a++) }
})
t.doesNotThrow(() => store({}, nanobus()), 'works fine if state has no events object')
t.end()
})
t.end()
})