This repository has been archived by the owner on Apr 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
114 lines (99 loc) · 3.31 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
const expect = require('chai').expect
const sinon = require('sinon')
const { createStore, applyMiddleware } = require('redux')
const { enqueue, dequeue, queueMiddleware, queueMap, clearQueue } = require('./dist')
const INCREMENT = 'INCREMENT'
const DECREMENT = 'DECREMENT'
function counter(state = 0, action) {
switch (action.type) {
case INCREMENT:
return state + 1
case DECREMENT:
return state + 2
default:
return state
}
}
describe('redux-q', () => {
beforeEach(() => {
store = createStore(
counter,
applyMiddleware(
queueMiddleware
)
)
})
afterEach(() => {
delete queueMap[INCREMENT]
delete queueMap[DECREMENT]
})
describe('enqueue', () => {
it('should queue callbacks in the queueMap', () => {
const func = () => {}
enqueue(func, INCREMENT)
expect(queueMap[INCREMENT].length).to.equal(1)
expect(queueMap[INCREMENT][0]).to.equal(func)
})
it('should queue arrays of callbacks in the queueMap', () => {
const func = () => {}
enqueue([func, func], INCREMENT)
expect(queueMap[INCREMENT].length).to.equal(2)
expect(queueMap[INCREMENT][0]).to.equal(func)
expect(queueMap[INCREMENT][1]).to.equal(func)
})
})
describe('dequeue', () => {
const func = () => {console.log('func1')}
const func2 = () => {console.log('func2')}
const func3 = () => {console.log('func3')}
const func4 = () => {console.log('func1')}
it('should dequeue the specified callback from the queueMap', () => {
queueMap[INCREMENT] = [func, func2]
dequeue(func, INCREMENT)
expect(queueMap[INCREMENT][0]).to.equal(func2)
})
it('should dequeue arrays of callbacks from the queueMap', () => {
queueMap[INCREMENT] = [func, func2, func3, func4]
dequeue([func, func2], INCREMENT)
expect(queueMap[INCREMENT]).to.deep.equal([func3, func4]);
})
})
describe('clearQueue', () => {
const func = () => {console.log('func1')}
const func2 = () => {console.log('func2')}
it('should clear the queue of a given action', () => {
queueMap[INCREMENT] = [func, func2]
clearQueue(INCREMENT)
expect(queueMap[INCREMENT].length).to.equal(0)
})
})
describe('enqueueMiddleware', () => {
it('should call all enqueued functions once', () => {
let first = sinon.spy()
let second = sinon.spy()
enqueue([first, second], INCREMENT)
store.dispatch({ type: INCREMENT })
store.dispatch({ type: DECREMENT })
store.dispatch({ type: INCREMENT })
expect(first.calledOnce).to.equal(true)
expect(second.calledOnce).to.equal(true)
})
it('should clear the queue after calling all callbacks', () => {
let callback = sinon.spy()
enqueue([callback, callback, callback], DECREMENT)
store.dispatch({ type: DECREMENT })
expect(callback.calledThrice).to.equal(true)
expect(queueMap[DECREMENT].length).to.equal(0)
})
it('should process callbacks as a FIFO queue', () => {
var calls = []
const first = () => calls.push('first')
const second = () => calls.push('second')
enqueue(first, INCREMENT)
enqueue(second, INCREMENT)
store.dispatch({ type: INCREMENT })
expect(calls[0]).to.equal('first')
expect(calls[1]).to.equal('second')
})
})
})