forked from aksonov/react-native-router-flux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reducer.popAndReplace.test.js
175 lines (151 loc) · 5.03 KB
/
Reducer.popAndReplace.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import React from 'react';
import { expect } from 'chai';
import Scene from '../src/Scene';
import Actions from '../src/Actions';
import * as ActionConst from '../src/ActionConst';
import createReducer, { getCurrent, findElement } from '../src/Reducer';
import getInitialStateFromRoot from '../src/State';
const id = 0;
const guid = () => id + 1;
const scenesData = (
<Scene
key="root"
component="Modal"
getInitialState={() => ({ foo: guid() })}
>
<Scene key="launch" component="Launch" />
<Scene key="sideMenu" component="Drawer" initial>
<Scene component="CubeBar" key="cubeBar" type="tabs">
<Scene key="main" tabs>
<Scene key="home" component="Home" />
<Scene key="map" component="Map" />
<Scene key="myAccount" component="MyAccount" />
</Scene>
<Scene key="messaging" initial>
<Scene
key="conversations"
component="Conversations"
getInitialState={() => ({ foo: 'what', bar: guid() })}
/>
</Scene>
</Scene>
</Scene>
<Scene key="privacyPolicy" component="PrivacyPolicy" type="modal" />
<Scene key="termsOfService" component="TermsOfService" type="modal" />
<Scene key="cloneModalComponent" component="cloneComponent" clone="true" type="modal" />
<Scene key="login">
<Scene key="loginModal1" component="Login1" />
<Scene key="loginModal2" component="Login2" />
</Scene>
</Scene>);
describe('popAndReplace', () => {
let latestState;
let currentScene;
let reducer;
beforeEach(() => {
const scenes = Actions.create(scenesData);
const initialState = getInitialStateFromRoot(scenes);
reducer = createReducer({ initialState, scenes });
latestState = null;
});
it('pop and replace on root stack', () => {
latestState = reducer(latestState, {
key: 'privacyPolicy',
type: ActionConst.PUSH,
});
latestState = reducer(latestState, {
key: 'termsOfService',
type: ActionConst.PUSH,
});
latestState = reducer(latestState, {
key: 'termsOfService',
type: ActionConst.POP_AND_REPLACE,
});
expect(latestState.index).equal(1);
currentScene = getCurrent(latestState);
expect(currentScene.sceneKey).equal('termsOfService');
expect(currentScene.type).equal(ActionConst.POP_AND_REPLACE);
});
it('pop 2 and replace on root stack', () => {
latestState = reducer(latestState, {
key: 'privacyPolicy',
type: ActionConst.PUSH,
});
latestState = reducer(latestState, {
key: 'termsOfService',
type: ActionConst.PUSH,
});
latestState = reducer(latestState, {
key: 'termsOfService',
type: ActionConst.POP_AND_REPLACE,
popNum: 2,
});
expect(latestState.index).equal(0);
currentScene = getCurrent(latestState);
expect(currentScene.popNum).equal(undefined);
expect(currentScene.sceneKey).equal('termsOfService');
expect(currentScene.type).equal(ActionConst.POP_AND_REPLACE);
});
it('pop out of root stack - standing on root', () => {
try {
latestState = reducer(latestState, {
key: 'termsOfService',
type: ActionConst.POP_AND_REPLACE,
});
} catch (err) {
expect(err.message).equal('[react-native-router-flux] You are already in the root scene.');
}
});
it('pop out of root stack', () => {
try {
latestState = reducer(latestState, {
key: 'privacyPolicy',
type: ActionConst.PUSH,
});
latestState = reducer(latestState, {
key: 'termsOfService',
type: ActionConst.POP_AND_REPLACE,
popNum: 2,
});
} catch (err) {
expect(err.message).equal('[react-native-router-flux] The data is the number of scenes ' +
'you want to pop, it must be smaller than scenes stack\'s length.');
}
});
it('pop and replace on child stack', () => {
latestState = reducer(latestState, {
key: 'conversations',
type: ActionConst.PUSH,
});
latestState = reducer(latestState, {
key: 'cloneModalComponent',
type: ActionConst.PUSH,
});
latestState = reducer(latestState, {
key: 'cloneModalComponent',
type: ActionConst.POP_AND_REPLACE,
});
currentScene = getCurrent(latestState);
expect(findElement(latestState, currentScene.parent).index).equal(0);
expect(currentScene.sceneKey).equal('cloneModalComponent');
expect(currentScene.type).equal(ActionConst.POP_AND_REPLACE);
});
it('pop and replace on root stack from nested child', () => {
latestState = reducer(latestState, {
key: 'conversations',
type: ActionConst.PUSH,
});
latestState = reducer(latestState, {
key: 'login',
type: ActionConst.PUSH,
});
latestState = reducer(latestState, {
key: 'termsOfService',
type: ActionConst.POP_AND_REPLACE,
});
expect(latestState.index).equal(0);
currentScene = getCurrent(latestState);
expect(currentScene.sceneKey).equal('termsOfService');
expect(currentScene.type).equal(ActionConst.POP_AND_REPLACE);
});
});