This repository was archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathreduxMiddleware.spec.js
84 lines (75 loc) · 2.82 KB
/
reduxMiddleware.spec.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
import middleware, { register, unregister, registered } from '../src/reduxMiddleware';
describe('reduxMiddleware', function() {
beforeEach(() => {
registered.splice(0, registered.length);
});
describe('the middleware', () => {
it('should return a function', () => {
expect(middleware()).to.be.a('function');
});
describe('the returned function', () => {
it('should return a function', () => {
expect(middleware()()).to.be.a('function');
});
describe('the returned function', () => {
it('should return the result of the next action', () => {
const store = {};
const returnValue = {};
const next = sinon.stub().returns(returnValue);
const action = {};
const result = middleware(store)(next)(action);
expect(next).to.have.been.calledWith(action);
expect(result).to.equal(returnValue);
});
it('should call all of the registered callbacks with action', () => {
registered.push(sinon.spy(), sinon.spy(), sinon.spy());
const store = {};
const next = () => {};
const action = {};
middleware(store)(next)(action);
expect(registered.pop()).to.have.been.calledWith(action);
expect(registered.pop()).to.have.been.calledWith(action);
expect(registered.pop()).to.have.been.calledWith(action);
});
it('should not throw is the registered callback is not a function', () => {
registered.push([]);
const store = {};
const next = () => {};
const action = {};
expect(middleware(store)(next).bind(middleware, action)).to.not.throw();
});
});
});
});
describe('register', () => {
it('should add the function to the registered array', () => {
const fn = () => {};
expect(registered).to.not.include(fn);
register(fn);
expect(registered).to.include(fn);
});
it('should not add the function to the registered array if it is already there', () => {
const fn = () => {};
expect(registered).to.not.include(fn);
register(fn);
expect(registered).to.include(fn).and.have.a.lengthOf(1);
register(fn);
expect(registered).to.include(fn).and.have.a.lengthOf(1);
});
});
describe('unregister', () => {
it('should remove the function to the registered array', () => {
const fn = () => {};
registered.push(fn);
expect(registered).to.include(fn);
unregister(fn);
expect(registered).to.not.include(fn);
});
it('should not throw if the function does not exist in the array', () => {
const fn = () => {};
expect(registered).to.not.include(fn);
expect(unregister.bind(unregister, fn)).to.not.throw();
expect(registered).to.not.include(fn);
});
});
});