Skip to content

Commit 766bda7

Browse files
author
Ivan Zotov
committed
Add build script and tests
1 parent 587647e commit 766bda7

File tree

8 files changed

+1097
-29
lines changed

8 files changed

+1097
-29
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.DS_Store
2+
dist/
23
node_modules/
34
coverage/

__tests__/Modal/Navigator.js

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
import Modal from '../../src/Modal';
2+
3+
describe('Modal.Navigator', () => {
4+
it('should has a name', () => {
5+
const navigator = new Modal.Navigator('navigator');
6+
expect(navigator.name).toBe('navigator');
7+
});
8+
9+
it('should has an empty scenes object', () => {
10+
const navigator = new Modal.Navigator('navigator');
11+
expect(navigator.scenes).toEqual({});
12+
});
13+
14+
it('should has an empty history array', () => {
15+
const navigator = new Modal.Navigator('navigator');
16+
expect(navigator.history).toEqual([]);
17+
});
18+
19+
describe('.addScenes', () => {
20+
it('should add scenes by names', () => {
21+
const navigator = new Modal.Navigator('navigator');
22+
const first = new Modal.Scene('first');
23+
const second = new Modal.Scene('second');
24+
navigator.addScenes(first, second);
25+
expect(navigator.scenes).toEqual({ first, second });
26+
});
27+
});
28+
29+
describe('.current', () => {
30+
it('should return last item from history', () => {
31+
const navigator = new Modal.Navigator('navigator');
32+
navigator.history = ['first', 'second'];
33+
expect(navigator.current()).toBe('second');
34+
});
35+
});
36+
37+
describe('.go', () => {
38+
it('should reject if no scene exists', async () => {
39+
const navigator = new Modal.Navigator('navigator');
40+
navigator.addScenes(new Modal.Scene('scene'));
41+
42+
expect.assertions(1);
43+
try {
44+
await navigator.go('anything');
45+
} catch (e) {
46+
expect(e).toBeUndefined();
47+
}
48+
});
49+
50+
it('should not add scene if it is already in history', async () => {
51+
const navigator = new Modal.Navigator('navigator');
52+
navigator.addScenes(new Modal.Scene('scene'));
53+
expect(navigator.history).toEqual([]);
54+
await navigator.go('scene');
55+
expect(navigator.history).toEqual(['scene']);
56+
await navigator.go('scene');
57+
expect(navigator.history).toEqual(['scene']);
58+
});
59+
60+
it('should invoke show on scene with provided duration', async () => {
61+
const navigator = new Modal.Navigator('navigator');
62+
const scene = new Modal.Scene('scene');
63+
navigator.addScenes(scene);
64+
scene.show = jest.fn();
65+
await navigator.go('scene', 100);
66+
expect(scene.show).toBeCalledWith(100);
67+
});
68+
69+
it('should add scene into history', async () => {
70+
const navigator = new Modal.Navigator('navigator');
71+
navigator.addScenes(new Modal.Scene('scene'));
72+
expect(navigator.history).toEqual([]);
73+
await navigator.go('scene');
74+
expect(navigator.history).toEqual(['scene']);
75+
});
76+
77+
it('should add scene after animation is done', async () => {
78+
const navigator = new Modal.Navigator('navigator');
79+
navigator.addScenes(new Modal.Scene('scene'));
80+
expect.assertions(2);
81+
const promise = navigator.go('scene');
82+
expect(navigator.history).toEqual([]);
83+
await promise;
84+
expect(navigator.history).toEqual(['scene']);
85+
});
86+
});
87+
88+
describe('.back', () => {
89+
it('should resolve if history is empty', async () => {
90+
const navigator = new Modal.Navigator('navigator');
91+
expect.assertions(1);
92+
expect(navigator.history).toEqual([]);
93+
try {
94+
await navigator.back();
95+
} catch (e) {
96+
expect(e).toEqual(expect.anything());
97+
}
98+
});
99+
100+
it('should reject if no such scene exists', async () => {
101+
const navigator = new Modal.Navigator('navigator');
102+
navigator.history = ['anything'];
103+
expect.assertions(1);
104+
105+
try {
106+
await navigator.back();
107+
} catch (e) {
108+
expect(e).toBeUndefined();
109+
}
110+
});
111+
112+
it('should invoke scene hide with provided duration', async () => {
113+
const navigator = new Modal.Navigator('navigator');
114+
const scene = new Modal.Scene('scene');
115+
navigator.addScenes(scene);
116+
await navigator.go('scene');
117+
scene.hide = jest.fn();
118+
expect.assertions(1);
119+
await navigator.back(123);
120+
expect(scene.hide).toBeCalledWith(123);
121+
});
122+
123+
it('should remove scene after animation is done', async () => {
124+
const navigator = new Modal.Navigator('navigator');
125+
navigator.addScenes(new Modal.Scene('scene'));
126+
await navigator.go('scene');
127+
expect.assertions(2);
128+
const promise = navigator.back();
129+
expect(navigator.history).toEqual(['scene']);
130+
await promise;
131+
expect(navigator.history).toEqual([]);
132+
});
133+
});
134+
135+
describe('.reset', () => {
136+
it('should call hide for all scenes with duration 0', async () => {
137+
const navigator = new Modal.Navigator('navigator');
138+
const scene1 = new Modal.Scene('scene1');
139+
const scene2 = new Modal.Scene('scene2');
140+
navigator.addScenes(scene1, scene2);
141+
await navigator.go('scene1');
142+
await navigator.go('scene2');
143+
scene1.hide = jest.fn();
144+
scene2.hide = jest.fn();
145+
await navigator.reset();
146+
expect(scene1.hide).toBeCalledWith(0);
147+
expect(scene2.hide).toBeCalledWith(0);
148+
});
149+
150+
it('should clean up history after all scenes are hidden', async () => {
151+
const navigator = new Modal.Navigator('navigator');
152+
const scene1 = new Modal.Scene('scene1');
153+
const scene2 = new Modal.Scene('scene2');
154+
navigator.addScenes(scene1, scene2);
155+
await navigator.go('scene1');
156+
await navigator.go('scene2');
157+
const promise = navigator.reset();
158+
expect(navigator.history).toEqual(['scene1', 'scene2']);
159+
await promise;
160+
expect(navigator.history).toEqual([]);
161+
});
162+
});
163+
});

__tests__/Modal/Scene.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import Value from '@navigationjs/core';
2+
import Scene from '../../src/Modal/Scene';
3+
4+
describe('Modal Scene', () => {
5+
it('should has a name', () => {
6+
const scene = new Scene('main');
7+
expect(scene.name).toBe('main');
8+
});
9+
10+
it('should has value active', () => {
11+
const scene = new Scene('main');
12+
expect(scene.active.name).toBe('active');
13+
});
14+
15+
describe('.show', () => {
16+
it('should change active value', async () => {
17+
const scene = new Scene('main');
18+
expect(scene.active.value).toBe(0);
19+
await scene.show();
20+
expect(scene.active.value).toBe(1);
21+
});
22+
23+
it('should invoke .to method on value with provided duration', async () => {
24+
const scene = new Scene('main');
25+
scene.active.to = jest.fn();
26+
await scene.show(123);
27+
expect(scene.active.to).toBeCalledWith(1, 123);
28+
});
29+
});
30+
31+
describe('.hide', () => {
32+
it('should change active value', async () => {
33+
const scene = new Scene('main');
34+
await scene.active.to(1);
35+
expect(scene.active.value).toBe(1);
36+
await scene.hide();
37+
expect(scene.active.value).toBe(0);
38+
});
39+
40+
it('should invoke .to method on value with provided duration', async () => {
41+
const scene = new Scene('main');
42+
scene.active.to = jest.fn();
43+
await scene.hide(123);
44+
expect(scene.active.to).toBeCalledWith(0, 123);
45+
});
46+
});
47+
});

babel.config.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
module.exports = {
2-
plugins: ['@babel/plugin-proposal-class-properties'],
3-
};
2+
presets: [
3+
[
4+
'@babel/preset-env',
5+
{
6+
targets: {
7+
node: 'current',
8+
},
9+
},
10+
],
11+
'@babel/preset-react'
12+
],
13+
plugins: ['@babel/plugin-proposal-class-properties']
14+
}

package.json

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@navigationjs/react",
33
"version": "0.1.0",
44
"description": "Small and well-tested React navigation",
5-
"main": "index.js",
5+
"main": "dist/index.js",
66
"author": "Ivan Zotov <[email protected]> (http://ivanzotov.com/)",
77
"license": "MIT",
88
"private": false,
@@ -20,24 +20,36 @@
2020
],
2121
"scripts": {
2222
"test": "jest",
23+
"build": "yarn babel src/ --out-dir dist/",
2324
"debug": "node --inspect-brk node_modules/.bin/jest --runInBand",
2425
"format": "prettier --write '{__tests__,src}/**/*.{js,json}' './*.{js,json}'"
2526
},
27+
"files": [
28+
"src",
29+
"dist"
30+
],
2631
"peerDependencies": {
2732
"react": "*"
2833
},
2934
"devDependencies": {
35+
"@babel/cli": "^7.7.5",
3036
"@babel/core": "^7.7.5",
3137
"@babel/plugin-proposal-class-properties": "^7.7.4",
38+
"@babel/preset-env": "^7.7.6",
39+
"@babel/preset-react": "^7.7.4",
3240
"@testing-library/react": "^9.4.0",
41+
"babel-jest": "^24.9.0",
3342
"eslint": "^6.6.0",
3443
"jest": "^24.9.0",
3544
"prettier": "^1.16.4",
3645
"react": "^16.11.0",
37-
"react-test-renderer": "^16.11.0"
46+
"react-test-renderer": "^16.11.0",
47+
"rollup": "^1.27.9",
48+
"rollup-plugin-babel": "^4.3.3",
49+
"rollup-plugin-node-resolve": "^5.2.0",
50+
"rollup-plugin-terser": "^5.1.3"
3851
},
3952
"jest": {
40-
"preset": "@testing-library/react",
4153
"testEnvironment": "node"
4254
},
4355
"dependencies": {

src/Modal/Wrap.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React, { Component } from 'react';
22
import navigation, { toId } from '@navigationjs/core';
3-
import './styles';
43

54
export default class Wrap extends Component {
65
onValue = () => {

index.js renamed to src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import navigation, {
55
toId,
66
fromId,
77
} from '@navigationjs/core';
8-
import Modal from './src/Modal';
8+
import Modal from './Modal';
99

1010
export { Navigation, Base, Modal, Value, toId, fromId };
1111
export default navigation;

0 commit comments

Comments
 (0)