Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonpage committed Nov 16, 2016
1 parent 353a976 commit c861cc8
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 129 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"buffer": "./node_modules/buffer/index.js"
},
"jest": {
"preset": "jest-react-native"
"preset": "jest-react-native",
"setupFiles": "./test/setup.js"
}
}
5 changes: 5 additions & 0 deletions test/__mocks__/react-native-google-analytics-bridge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

module.exports = {
setTrackerId: () => {},
trackEvent: () => {},
};
93 changes: 0 additions & 93 deletions test/lib/scanner/metadata/index.test.js

This file was deleted.

61 changes: 31 additions & 30 deletions test/lib/store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
* Dependencies
*/

const actions = require('../../lib/store/actions').actions;
const { createStore, bindActionCreators } = require('redux');
const reducer = require('../../lib/store/reducer');
const assert = require('assert');
import { createStore, bindActionCreators, applyMiddleware } from 'redux';
import actions from '../../lib/store/actions';
import reducer from '../../lib/store/reducer';
import thunk from 'redux-thunk';
import assert from 'assert';

describe('store', function() {
beforeEach(function() {
this.store = createStore(reducer);
this.store = createStore(reducer, applyMiddleware(thunk));
this.actions = bindActionCreators(actions, this.store.dispatch);
});

Expand All @@ -19,88 +20,88 @@ describe('store', function() {

describe('first', function() {
beforeEach(function() {
this.actions.updateItem(url, {
this.actions.itemFound(url, {
url,
id: url,
distance: 3,
});
});

it('sets correct distance', function() {
var item = getItem(this.store.getState().items, url);
var item = getItem(this.store.getState().items, url).value;
var expected = [3];

assert.deepEqual(item.distances, expected);
assert.equal(item.regulatedDistance, meanToNearest(expected, 2));
assert.deepEqual(item._distanceHistory, expected);
assert.equal(item.distance, meanToNearest(expected, 2));
});

describe('second', function() {
beforeEach(function() {
this.actions.updateItem(url, { distance: 4 });
this.actions.itemFound(url, { distance: 4 });
});

it('sets correct distance', function() {
var item = getItem(this.store.getState().items, url);
var item = getItem(this.store.getState().items, url).value;
var expected = [3, 4];

assert.deepEqual(item.distances, expected);
assert.equal(item.regulatedDistance, meanToNearest(expected, 2));
assert.deepEqual(item._distanceHistory, expected);
assert.equal(item.distance, meanToNearest(expected, 2));
});

describe('third', function() {
beforeEach(function() {
this.actions.updateItem(url, {
this.actions.itemFound(url, {
distance: 3,
});
});

it('sets correct distance', function() {
var item = getItem(this.store.getState().items, url);
var item = getItem(this.store.getState().items, url).value;
var expected = [3, 4, 3];

assert.deepEqual(item.distances, expected);
assert.equal(item.regulatedDistance, meanToNearest(expected, 2));
assert.deepEqual(item._distanceHistory, expected);
assert.equal(item.distance, meanToNearest(expected, 2));
});

describe('fourth', function() {
beforeEach(function() {
this.actions.updateItem(url, {
this.actions.itemFound(url, {
distance: 7,
});
});

it('sets correct distance', function() {
var item = getItem(this.store.getState().items, url);
var item = getItem(this.store.getState().items, url).value;
var expected = [3, 4, 3, 7];

assert.deepEqual(item.distances, expected);
assert.equal(item.regulatedDistance, meanToNearest(expected, 2));
assert.deepEqual(item._distanceHistory, expected);
assert.equal(item.distance, meanToNearest(expected, 2));
});

describe('fifth', function() {
beforeEach(function() {
this.actions.updateItem(url, { distance: 4 });
this.actions.itemFound(url, { distance: 4 });
});

it('sets correct distance', function() {
var item = getItem(this.store.getState().items, url);
var item = getItem(this.store.getState().items, url).value;
var expected = [3, 4, 3, 7, 4];

assert.deepEqual(item.distances, expected);
assert.equal(item.regulatedDistance, meanToNearest(expected, 2));
assert.deepEqual(item._distanceHistory, expected);
assert.equal(item.distance, meanToNearest(expected, 2));
});

describe('sixth', function() {
beforeEach(function() {
this.actions.updateItem(url, { distance: 4 });
this.actions.itemFound(url, { distance: 4 });
});

it('sets correct distance', function() {
var item = getItem(this.store.getState().items, url);
var item = getItem(this.store.getState().items, url).value;
var expected = [4, 3, 7, 4, 4];

assert.deepEqual(item.distances, expected);
assert.equal(item.regulatedDistance, meanToNearest(expected, 2));
assert.deepEqual(item._distanceHistory, expected);
assert.equal(item.distance, meanToNearest(expected, 2));
});
});
});
Expand All @@ -111,7 +112,7 @@ describe('store', function() {
});

function getItem(items, id) {
return items.find(item => item.id === id);
return items[id];
}

function meanToNearest(numbers, nearest) {
Expand Down
13 changes: 8 additions & 5 deletions test/lib/views/item-scene.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import 'react-native';
import React from 'react';
import ItemScene from '../../../lib/views/item-scene';

import ItemScene from '../../../lib/views/item/scene';
import { createStore, applyMiddleware } from 'redux';
import reducer from '../../../lib/store/reducer';
import renderer from 'react-test-renderer';
import thunk from 'redux-thunk';
import React from 'react';
import 'react-native';

it('renders without crashing', () => {
renderer.create(<ItemScene navigator={{}} item={{}}/>);
const store = createStore(reducer, applyMiddleware(thunk));
renderer.create(<ItemScene navigator={{}} item={{}} store={store}/>);
});
11 changes: 11 additions & 0 deletions test/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

// var ReactNative = require('react-native');
// ReactNative.NativeModules.GoogleAnalyticsBridge = {};
// ReactNative.NativeModules.GoogleAnalyticsBridge = {};
//

// try {
// console.log('xxx', jest.genMockFromModule('react-native-google-analytics-bridge'));
// } catch(e) {
// console.log('eee', e);
// }

0 comments on commit c861cc8

Please sign in to comment.