/
applyNavigatorChangesTest.js
130 lines (110 loc) · 4.71 KB
/
applyNavigatorChangesTest.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
/* global describe, it, beforeEach */
var assert = require('assert');
var applyNavigatorChanges = require('../lib/applyNavigatorChanges');
var NavigatorMock = require('./NavigatorMock');
describe('applyNavigatorChanges', function() {
var routeA = 'routeA';
var routeB = 'routeB';
var routeC = 'routeC';
var routeD = 'routeD';
var navigatorMock;
beforeEach(function() {
navigatorMock = new NavigatorMock();
});
it('should be a function', function() {
assert.equal('function', typeof applyNavigatorChanges);
});
it('should throw an error if prev route stack is null or empty', function() {
assert.throws(function() {
applyNavigatorChanges();
}, /Error: Prev route stack must be an array with at least one entry!/);
assert.throws(function() {
applyNavigatorChanges([]);
}, /Error: Prev route stack must be an array with at least one entry!/);
});
it('should throw an error if next route stack is null or empty', function() {
assert.throws(function() {
applyNavigatorChanges([ routeA ]);
}, /Error: Next route stack must be an array with at least one entry!/);
assert.throws(function() {
applyNavigatorChanges([ routeA ], []);
}, /Error: Next route stack must be an array with at least one entry!/);
});
it('should throw an error if navigator is null', function() {
assert.throws(function() {
applyNavigatorChanges([ routeA ], [ routeB ], null);
}, /Error: Navigator must be defined!/);
});
it('must not call any navigator method if route stack is equal (===)', function() {
var prevRoutes = [routeA];
var nextRoutes = prevRoutes;
var expectedCallHistory = [];
applyNavigatorChanges(prevRoutes, nextRoutes, navigatorMock);
assert.deepEqual(navigatorMock.callHistory, expectedCallHistory);
});
it('must not call any navigator method if nothing changes', function() {
var prevRoutes = [routeA];
var nextRoutes = [routeA];
var expectedCallHistory = [];
applyNavigatorChanges(prevRoutes, nextRoutes, navigatorMock);
assert.deepEqual(navigatorMock.callHistory, expectedCallHistory);
});
it('should push a route if one was added', function() {
var prevRoutes = [routeA];
var nextRoutes = [routeA, routeB];
var expectedCallHistory = [ 'push routeB' ];
applyNavigatorChanges(prevRoutes, nextRoutes, navigatorMock);
assert.deepEqual(navigatorMock.callHistory, expectedCallHistory);
});
it('should replace and push a route if one was added', function() {
var prevRoutes = [routeA, routeB];
var nextRoutes = [routeA, routeC, routeD];
var expectedCallHistory = [ 'replaceAtIndex routeC 1', 'push routeD' ];
applyNavigatorChanges(prevRoutes, nextRoutes, navigatorMock);
assert.deepEqual(navigatorMock.callHistory, expectedCallHistory);
});
it('should pop a route if one was removed', function() {
var prevRoutes = [routeA, routeB];
var nextRoutes = [routeA];
var expectedCallHistory = [ 'pop' ];
applyNavigatorChanges(prevRoutes, nextRoutes, navigatorMock);
assert.deepEqual(navigatorMock.callHistory, expectedCallHistory);
});
it('should replace and pop a route if one was removed', function() {
var prevRoutes = [routeA, routeB, routeC];
var nextRoutes = [routeA, routeD];
var expectedCallHistory = [ 'replaceAtIndex routeD 1', 'pop' ];
applyNavigatorChanges(prevRoutes, nextRoutes, navigatorMock);
assert.deepEqual(navigatorMock.callHistory, expectedCallHistory);
});
it('should replace a route if all other are keeped', function() {
var prevRoutes = [routeA, routeB, routeC];
var nextRoutes = [routeA, routeD, routeC];
var expectedCallHistory = [ 'replaceAtIndex routeD 1' ];
applyNavigatorChanges(prevRoutes, nextRoutes, navigatorMock);
assert.deepEqual(navigatorMock.callHistory, expectedCallHistory);
});
it('should replace the route stack if the route changes', function() {
var prevRoutes = [routeA];
var nextRoutes = [routeB, routeC];
var expectedCallHistory = [ 'immediatelyResetRouteStack routeB,routeC' ];
applyNavigatorChanges(prevRoutes, nextRoutes, navigatorMock);
assert.deepEqual(navigatorMock.callHistory, expectedCallHistory);
navigatorMock.resetMock();
prevRoutes = [routeA, routeB];
nextRoutes = [routeC];
expectedCallHistory = [ 'immediatelyResetRouteStack routeC' ];
applyNavigatorChanges(prevRoutes, nextRoutes, navigatorMock);
assert.deepEqual(navigatorMock.callHistory, expectedCallHistory);
});
it('should replace the route stack if optional compareRoute function returns false', function() {
var prevRoutes = [routeA];
var nextRoutes = [routeA];
var compareRoute = function() {
return false;
};
var expectedCallHistory = [ 'immediatelyResetRouteStack routeA' ];
applyNavigatorChanges(prevRoutes, nextRoutes, navigatorMock, compareRoute);
assert.deepEqual(navigatorMock.callHistory, expectedCallHistory);
});
});