diff --git a/package.json b/package.json index 0e3210ef..e47fd4e0 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "buffer": "./node_modules/buffer/index.js" }, "jest": { - "preset": "jest-react-native" + "preset": "jest-react-native", + "setupFiles": "./test/setup.js" } } diff --git a/test/__mocks__/react-native-google-analytics-bridge.js b/test/__mocks__/react-native-google-analytics-bridge.js new file mode 100644 index 00000000..3eaca96e --- /dev/null +++ b/test/__mocks__/react-native-google-analytics-bridge.js @@ -0,0 +1,5 @@ + +module.exports = { + setTrackerId: () => {}, + trackEvent: () => {}, +}; diff --git a/test/lib/scanner/metadata/index.test.js b/test/lib/scanner/metadata/index.test.js deleted file mode 100644 index 3b580f17..00000000 --- a/test/lib/scanner/metadata/index.test.js +++ /dev/null @@ -1,93 +0,0 @@ - -/** - * Dependencies - */ - -const metadata = require('../../../../lib/scanner/metadata'); -const assert = require('assert'); - -jest.useFakeTimers(); - -describe('metadata', () => { - - describe('serverside', function() { - beforeEach(function() { - global.Request = jest.fn((url, config) => { - return Object.assign({}, config, { url }); - }); - - global.Headers = jest.fn(config => config); - - global.fetch = jest.fn(() => { - this.lastFetch = new Deferred; - return this.lastFetch.promise; - }); - }); - - afterEach(function() { - - }); - - describe('batches', function() { - beforeEach(function() { - this.calls = [ - metadata('http://bbc.co.uk/news'), - metadata('http://google.com'), - ]; - - // tick past batch window - jest.runTimersToTime(200); - }); - - it('batches requests', function() { - var request = global.fetch.mock.calls[0][0]; - var body = JSON.parse(request.body); - var urls = body.objects; - - assert.equal(urls[0].url, 'http://bbc.co.uk/news'); - assert.equal(urls[1].url, 'http://google.com'); - }); - - describe('error', function() { - beforeEach(function() { - var result = Promise.resolve([ - { - error: 'bad thing', - }, - { - title: 'Google', - }, - ]); - - this.lastFetch.resolve({ - json: () => result, - }); - - // wait till initial call has resolved - return this.calls[1]; - }); - - it('resolves the successful items', function() { - return this.calls[1] - .then(result => { - assert.equal(result.title, 'Google'); - }); - }); - - it('rejects the errored items', function() { - return this.calls[0] - .catch(err => { - assert.equal(err.message, 'bad thing'); - }); - }); - }); - }); - }); - - function Deferred() { - this.promise = new Promise((resolve, reject) => { - this.resolve = resolve; - this.reject = reject; - }); - } -}); diff --git a/test/lib/store.test.js b/test/lib/store.test.js index b5eadd9d..85cc86b2 100644 --- a/test/lib/store.test.js +++ b/test/lib/store.test.js @@ -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); }); @@ -19,7 +20,7 @@ describe('store', function() { describe('first', function() { beforeEach(function() { - this.actions.updateItem(url, { + this.actions.itemFound(url, { url, id: url, distance: 3, @@ -27,80 +28,80 @@ describe('store', function() { }); 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)); }); }); }); @@ -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) { diff --git a/test/lib/views/item-scene.test.js b/test/lib/views/item-scene.test.js index 0e1355dc..334cfaff 100644 --- a/test/lib/views/item-scene.test.js +++ b/test/lib/views/item-scene.test.js @@ -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(); + const store = createStore(reducer, applyMiddleware(thunk)); + renderer.create(); }); diff --git a/test/setup.js b/test/setup.js new file mode 100644 index 00000000..a425a5bb --- /dev/null +++ b/test/setup.js @@ -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); +// }