diff --git a/src/Actions.js b/src/Actions.js index 0b90b0d36..ba151e531 100644 --- a/src/Actions.js +++ b/src/Actions.js @@ -29,6 +29,21 @@ function filterParam(data) { return data; } +const reservedKeys = [ + POP_ACTION, + POP_ACTION2, + REFRESH_ACTION, + REPLACE_ACTION, + JUMP_ACTION, + PUSH_ACTION, + FOCUS_ACTION, + RESET_ACTION, + 'create', + 'callback', + 'iterate', + 'current', +]; + class Actions { constructor() { this.callback = null; @@ -43,16 +58,24 @@ class Actions { const refs = refsParam; assert(root.props, 'props should be defined for stack'); const key = root.key; - assert(key, 'unique key should be defined ', root); - assert([POP_ACTION, POP_ACTION2, REFRESH_ACTION, REPLACE_ACTION, JUMP_ACTION, PUSH_ACTION, - FOCUS_ACTION, RESET_ACTION, 'create', 'callback', 'iterate', 'current'] - .indexOf(key) === -1, `${key} is not allowed as key name`); + assert(key, 'unique key should be defined '); + assert( + reservedKeys.indexOf(key) === -1, + `'${key}' is not allowed as key name. Reserved keys: [${reservedKeys.join(', ')}]`, + ); const { children, ...staticProps } = root.props; let type = root.props.type || (parentProps.tabs ? JUMP_ACTION : PUSH_ACTION); if (type === 'switch') { type = JUMP_ACTION; } - const res = { name: key, ...staticProps, key, sceneKey: key, type, parent: parentProps.key }; + const res = { + key, + name: key, + sceneKey: key, + parent: parentProps.key, + type, + ...staticProps, + }; let list = children || []; if (!(list instanceof Array)) { list = [list]; diff --git a/test/.eslintrc.json b/test/.eslintrc.json new file mode 100644 index 000000000..4668ae79f --- /dev/null +++ b/test/.eslintrc.json @@ -0,0 +1,5 @@ +{ + "env": { + "mocha": true + } +} diff --git a/test/Actions.test.js b/test/Actions.test.js index 6644fe286..387b584c3 100644 --- a/test/Actions.test.js +++ b/test/Actions.test.js @@ -1,81 +1,70 @@ import { expect } from 'chai'; -import Actions from '../src/Actions'; -import getInitialState from '../src/State'; -import React from 'react-native'; -import Scene from '../src/Scene'; -import createReducer from '../src/Reducer'; - -describe('Actions', () => { - it('should create needed actions', () => { - let id = 0; - const guid = () => id++; - const scenesData = ({ foo: guid() })}> - - - - - - - - - - ({ foo: 'what', bar: guid() })}/> - - - - - - - - - - ; - const scenes = Actions.create(scenesData); - expect(scenes.conversations.component).to.equal("Conversations"); - - let currentScene = null; - Actions.callback = scene=>{currentScene = scene}; - Actions.conversations({param1: "Hello world"}); - expect(currentScene.param1).equal("Hello world"); - expect(currentScene.key).equal("conversations"); +import React from 'react-native'; - Actions.sideMenu({param2: "Hello world2"}); - expect(currentScene.param1).equal(undefined); - expect(currentScene.param2).equal("Hello world2"); - expect(currentScene.key).equal("sideMenu"); +import Actions from '../src/Actions'; +import Scene from '../src/Scene'; - Actions.messaging({param3: "Hello world3"}); - expect(currentScene.param3).equal("Hello world3"); - expect(currentScene.key).equal("messaging"); +let id = 0; +const guid = () => id++; - const initialState = getInitialState(scenes); - expect(initialState.component).equal("Modal"); - expect(initialState.index).equal(0); +const scenesData = ( + ({ foo: guid() })} + > + + + + + + + + + + ({ foo: 'what', bar: guid() })} + /> + + + + + + + + + + ); - const reducer = createReducer({initialState, scenes}); - let state = undefined; - Actions.callback = scene=>state = reducer(state, scene); - expect(state).equal(undefined); - Actions.init(); - expect(state.key).equal("0_modal"); - expect(state.children[0].children[0].children[0].children[0].bar).equal(1); - expect(state.children[0].children[0].children[0].children[0].foo).equal('what'); +let scenes; - Actions.messaging(); - expect(currentScene.key).equal("messaging"); +describe('Actions', () => { + before(() => { + scenes = Actions.create(scenesData); + }); + it('should produce needed actions', () => { + // check scenes + expect(scenes.conversations.component).to.equal('Conversations'); - Actions.login(); - expect(state.children[1].key).equal("1_login"); - expect(state.children[1].children.length).equal(1); - expect(state.children[1].children[0].key).equal("0_loginModal1"); + // set callback which will extract generated action + let latestAction = null; + Actions.callback = scene => { latestAction = scene; }; - Actions.pop(); - expect(state.from.key).equal("1_login"); + // test generated actions + Actions.conversations({ param1: 'Hello world' }); + expect(latestAction.param1).equal('Hello world'); + expect(latestAction.key).equal('conversations'); - Actions.launch(); - expect(state.children[1].foo).equal(3); - expect(state.children[1].bar).equal(undefined); - }); + Actions.sideMenu({ param2: 'Hello world2' }); + expect(latestAction.param1).equal(undefined); + expect(latestAction.param2).equal('Hello world2'); + expect(latestAction.key).equal('sideMenu'); + Actions.messaging({ param3: 'Hello world3' }); + expect(latestAction.param3).equal('Hello world3'); + expect(latestAction.key).equal('messaging'); + }); });