Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests for Reducer, small fixes and comments. #597

Merged
merged 1 commit into from
Apr 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/Reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@
*
*/

import { PUSH_ACTION, POP_ACTION2, JUMP_ACTION, REPLACE_ACTION, RESET_ACTION,
POP_ACTION, REFRESH_ACTION } from './Actions';
import {
PUSH_ACTION,
POP_ACTION2,
JUMP_ACTION,
REPLACE_ACTION,
RESET_ACTION,
POP_ACTION,
REFRESH_ACTION,
} from './Actions';

import assert from 'assert';
import { getInitialState } from './State';

// WARN: it is not working correct. rewrite it.
function checkPropertiesEqual(action, lastAction) {
let isEqual = true;
for (const key of Object.keys(action)) {
Expand Down
2 changes: 1 addition & 1 deletion test/Actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('Actions', () => {

// set callback which will extract generated action
let latestAction = null;
Actions.callback = scene => { latestAction = scene; };
Actions.callback = action => { latestAction = action; };

// test generated actions
Actions.conversations({ param1: 'Hello world' });
Expand Down
109 changes: 109 additions & 0 deletions test/Reducer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import React from 'react-native';
import { expect } from 'chai';

import Scene from '../src/Scene';
import Actions from '../src/Actions';

import createReducer from '../src/Reducer';
import getInitialState from '../src/State';

// TODO: this function is from Reducer!!! Export it from Reducer or move to some sort of helpers.
export function getCurrent(state) {
if (!state.children) {
return state;
}
return getCurrent(state.children[state.index]);
}

let id = 0;
const guid = () => id++;
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="login">
<Scene key="loginModal1" component="Login1" />
<Scene key="loginModal2" component="Login2" />
</Scene>
</Scene>);

describe('createReducer', () => {
it('initilize state correct and reducer works', () => {
// create scenes and initialState
// For creating scenes we use external modules.
// TODO: Think about fully isolated test.
const scenes = Actions.create(scenesData);
const initialState = getInitialState(scenes); // TODO: write test for this.

const reducer = createReducer({ initialState, scenes });

let latestState;
let currentScene;
// Testing reducer.
// Normally actions came from Actions module, but we will generate it manually.
latestState = reducer(latestState, {
key: 'conversations',
type: 'push',
param1: 'Hello world',
});
currentScene = getCurrent(latestState);
expect(currentScene.name).equal('conversations');
expect(currentScene.sceneKey).equal('conversations');
expect(currentScene.type).equal('push');
expect(currentScene.param1).equal('Hello world');

latestState = reducer(latestState, {
key: 'login', // we go to `login` but first renderable child is `loginModal1`
type: 'push',
param2: 'Hello world2',
});
currentScene = getCurrent(latestState);
expect(currentScene.name).equal('loginModal1');
expect(currentScene.sceneKey).equal('loginModal1');
expect(currentScene.type).equal('push');
expect(currentScene.param2).equal('Hello world2');

latestState = reducer(latestState, {
key: 'privacyPolicy',
type: 'push',
param3: 'Accept policy',
});
currentScene = getCurrent(latestState);
expect(currentScene.name).equal('privacyPolicy');
expect(currentScene.sceneKey).equal('privacyPolicy');
expect(currentScene.type).equal('push');
expect(currentScene.param3).equal('Accept policy');

latestState = reducer(latestState, {
key: 'cubeBar', // we go to cubeBar but first renderable child is `conversations`
type: 'push',
param1: 'Conversations new param',
});
currentScene = getCurrent(latestState);
expect(currentScene.name).equal('conversations');
expect(currentScene.sceneKey).equal('conversations');
expect(currentScene.type).equal('push');
expect(currentScene.param1).equal('Conversations new param');
});
});