-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.js
149 lines (148 loc) · 3.69 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
var test = require('tape')
var Store = require('./')
test('Store', t => {
test('should exits', t => {
t.ok(Store, 'exists')
t.end()
})
test('store', t => {
var store = Store()
t.test('should exist', t => {
t.ok(store, 'exists')
t.end()
})
t.test('api', t => {
t.test('should expose subscribe', t => {
t.ok(store.subscribe, 'has subscribe')
t.end()
})
t.test('should expose unsubscribe', t => {
t.ok(store.unsubscribe, 'has unsubscribe')
t.end()
})
t.test('should expose register', t => {
t.ok(store.register, 'has register')
t.end()
})
t.test('should expose dispatch', t => {
t.ok(store.dispatch, 'has dispatch')
t.end()
})
t.test('should be a function', t => {
t.ok(typeof store === 'function', 'can be called for state')
t.end()
})
})
t.test('usage', t => {
t.test('should subscribe listener', t => {
function a () {}
t.ok(store.subscribe(a), 'can subscribe')
t.end()
})
t.test('should unsubscribe listener', t => {
function a () {}
store.subscribe(a)
t.ok(store.unsubscribe(a), 'can unsubscribe')
t.end()
})
t.test('should register reducer', t => {
function a (state, action) {
t.ok(true, 'registered a reducer')
t.end()
}
a.key = 'a'
store.register(a)
})
t.test('should register multiple reducers', t => {
t.plan(3)
var store = Store()
function d (state, action) {
console.log('REDUCER 1')
t.ok(true, 'called')
}
d.key = 'd'
function e (state, action) {
console.log('REDUCER 2')
t.ok(true, 'called')
}
e.key = 'e'
function f (state, action) {
console.log('REDUCER 3')
t.ok(true, 'called')
}
f.key = 'f'
store.register(d, e, f)
})
t.test('should be able to dispatch action', t => {
var store = Store({
a: 1,
b: 2
})
console.log('STORE', store())
function l (state) {
console.log('NOTIFIED', state)
t.ok(true, 'called')
t.deepEqual({a: 1, b: 4}, state, 'all working')
}
function b (state, action) {
if (action === 'NOTIFY') {
return 4
}
}
b.key = 'b'
store.register(b)
store.subscribe(l)
store.dispatch('NOTIFY')
t.end()
})
t.test('should return default state', t => {
var store = Store()
function a (state, action) {
return 1
}
a.key = 'a'
function b (state, action) {
return 2
}
b.key = 'b'
store.register(a, b)
t.deepEqual(store(), {a: 1, b: 2}, 'returns default state')
t.end()
})
t.test('should accept initialState', t => {
var store = Store({
a: {
active: [1, 2, 3]
},
b: {
cursor: {
id: 'yolo'
}
}
})
function a (state, action) {
state = state || { active: [] }
return state
}
a.key = 'a'
function b (state, action) {
state = state || { cursor: {} }
return state
}
b.key = 'b'
store.register(a, b)
t.deepEqual(store(), {
a: {
active: [1, 2, 3]
},
b: {
cursor: {
id: 'yolo'
}
} }, 'Populated with initialState')
t.end()
})
})
})
t.end()
})