Skip to content

Commit

Permalink
extract the parts of test set-up that I think I'll need for enzyme tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alex committed Nov 30, 2017
1 parent b4359db commit 97742e6
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 52 deletions.
2 changes: 1 addition & 1 deletion tests/garbage-in-local-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

const expect = require('chai').expect;

require('./helpers/integration-setup');
require('./helpers/integration-global');
const findOnClickMethodOfElement = require('./helpers/find-on-click');

global.testVars.storedCoordinates = 'THIS IS NOT JSON';
Expand Down
25 changes: 25 additions & 0 deletions tests/helpers/integration-global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* eslint no-console: 0 */

'use strict';

require('raf/polyfill');

const integerationHelper = require('./integration-helper');

// thanks https://github.com/jesstelford/react-testing-mocha-jsdom
const jsdom = require('jsdom');
const dom = new jsdom.JSDOM('<!doctype html><html><body><div id="root"></div></body></html>');
global.document = dom.window.document;
global.window = dom.window;

global.navigator = {
userAgent: 'node.js', //thanks http://stackoverflow.com/a/37084875/5203563
geolocation: integerationHelper.props.geolocation,
};

global.setInterval = integerationHelper.props.setInterval;

global.localStorage = integerationHelper.props.localStorage;

global.testVars = integerationHelper.testVars;
global.testVars.dom = dom;
43 changes: 43 additions & 0 deletions tests/helpers/integration-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* eslint no-console: 0 */

'use strict';

const assert = require('assert');

const testVars = {};

const geolocation = {
getCurrentPosition: function(callback, error) {
assert.ok(!testVars.locationCallback, 'clear vars before calling again');
assert.ok(!testVars.locationErrorCallback, 'clear vars before calling again');
testVars.locationCallback = callback;
testVars.locationErrorCallback = error;
},
};

const setInterval = function(callback, interval) {
assert.ok(!testVars.timerInterval, 'clear vars before calling again');
assert.ok(!testVars.timerCallback, 'clear vars before calling again');
testVars.timerCallback = callback;
testVars.timerInterval = interval;
};

const localStorage = {
getItem: function(key) {
assert.equal(key, 'coordinates', 'unexpected local storage key');
return testVars.storedCoordinates;
},
setItem: function(key, value) {
assert.equal(key, 'coordinates', 'unexpected local storage key');
testVars.storedCoordinates = value;
},
};

module.exports = {
testVars: testVars,
props: {
geolocation: geolocation,
localStorage: localStorage,
setInterval: setInterval,
},
};
48 changes: 0 additions & 48 deletions tests/helpers/integration-setup.js

This file was deleted.

2 changes: 1 addition & 1 deletion tests/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const EXPECTED_DEFAULT_POSITION = '51°47′60″N 0°0′0″W';
const expect = require('chai').expect;

require('./helpers/integration-setup');
require('./helpers/integration-global');
const findOnClickMethodOfElement = require('./helpers/find-on-click');

require('../src/root');
Expand Down
2 changes: 1 addition & 1 deletion tests/no-local-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

const expect = require('chai').expect;

require('./helpers/integration-setup');
require('./helpers/integration-global');
const findOnClickMethodOfElement = require('./helpers/find-on-click');

const NO_WRITE_MESSAGE = 'access denied for writing';
Expand Down
2 changes: 1 addition & 1 deletion tests/saved-coordinates.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const mount = require('enzyme').mount;

// for now let's use the same set-up and just pass in the global variables
// some of it will be redundant (like the dom)
require('./helpers/integration-setup');
require('./helpers/integration-global');

const Sut = require('../src/clock-app');

Expand Down

0 comments on commit 97742e6

Please sign in to comment.