Browse Source

Fix code linting errors

pull/46/merge
Luke Childs 8 years ago
parent
commit
78ec5a6c44
  1. 29
      src/index.js
  2. 10
      test/Arguments should be able to be passed in in either order.js
  3. 6
      test/Existing falsey node globals don't get overwritten.js
  4. 6
      test/Existing node globals shouldn't get overwritten.js
  5. 12
      test/Function should overwrite DOM globals on each call.js
  6. 4
      test/Function should return window instance.js
  7. 18
      test/Function should setup browser environment.js
  8. 2
      test/Function shouldn't return the same instance.js
  9. 28
      test/Properties arg should set globals.js
  10. 8
      test/jsdom arg should set jsdom config.js
  11. 10
      test/window properties should be as expected.js

29
src/index.js

@ -4,10 +4,10 @@ const Window = require('window');
// These settings must override any custom settings to make sure we can iterate // These settings must override any custom settings to make sure we can iterate
// over the window object. // over the window object.
const defaultJsdomConfig = { const defaultJsdomConfig = {
features: { features: {
FetchExternalResources: false, FetchExternalResources: false,
ProcessExternalResources: false ProcessExternalResources: false
} }
}; };
// IIFE executed on import to return an array of global Node.js properties that // IIFE executed on import to return an array of global Node.js properties that
@ -18,18 +18,17 @@ const protectedproperties = (() => Object
)(); )();
// Sets up global browser environment // Sets up global browser environment
module.exports = function browserEnv() { const browserEnv = function () {
// Extract options from args // Extract options from args
const args = Array.from(arguments); const args = Array.from(arguments);
const properties = args.filter(arg => Array.isArray(arg))[0]; const properties = args.filter(arg => Array.isArray(arg))[0];
const userJsdomConfig = args.filter(arg => !Array.isArray(arg))[0]; const userJsdomConfig = args.filter(arg => !Array.isArray(arg))[0];
// Create window object // Create window object
const window = new Window(Object.assign({}, userJsdomConfig, defaultJsdomConfig)); const window = new Window(Object.assign({}, userJsdomConfig, defaultJsdomConfig));
// Get all global browser properties // Get all global browser properties
Object.getOwnPropertyNames(window) Object.getOwnPropertyNames(window)
// Remove protected properties // Remove protected properties
.filter(prop => protectedproperties.indexOf(prop) === -1) .filter(prop => protectedproperties.indexOf(prop) === -1)
@ -38,8 +37,12 @@ module.exports = function browserEnv() {
.filter(prop => !(properties && properties.indexOf(prop) === -1)) .filter(prop => !(properties && properties.indexOf(prop) === -1))
// Copy what's left to the Node.js global scope // Copy what's left to the Node.js global scope
.forEach(prop => global[prop] = window[prop]); .forEach(prop => {
global[prop] = window[prop];
});
// Return reference to original window object // Return reference to original window object
return window; return window;
}; };
module.exports = browserEnv;

10
test/Arguments should be able to be passed in in either order.js

@ -2,9 +2,9 @@ import test from 'ava';
import browserEnv from '../src'; import browserEnv from '../src';
test(t => { test(t => {
t.is(typeof navigator, 'undefined'); t.is(typeof navigator, 'undefined');
const returnValue = browserEnv(['navigator'], { userAgent: 'first' }); browserEnv(['navigator'], { userAgent: 'first' });
t.is(navigator.userAgent, 'first'); t.is(navigator.userAgent, 'first');
const secondReturnValue = browserEnv({ userAgent: 'second' }, ['navigator']); browserEnv({ userAgent: 'second' }, ['navigator']);
t.is(navigator.userAgent, 'second'); t.is(navigator.userAgent, 'second');
}); });

6
test/Existing falsey node globals don't get overwritten.js

@ -3,7 +3,7 @@ import test from 'ava';
// We have to require() here as imports have to be top level so we can't set // We have to require() here as imports have to be top level so we can't set
// globals first // globals first
test(t => { test(t => {
global.document = false; global.document = false;
require('../src')(); require('../src')();
t.is(document, false); t.is(document, false);
}); });

6
test/Existing node globals shouldn't get overwritten.js

@ -2,7 +2,7 @@ import test from 'ava';
import browserEnv from '../src'; import browserEnv from '../src';
test(t => { test(t => {
const origConsole = console; const origConsole = console;
browserEnv(); browserEnv();
t.is(origConsole, console); t.is(origConsole, console);
}); });

12
test/Function should overwrite DOM globals on each call.js

@ -2,10 +2,10 @@ import test from 'ava';
import browserEnv from '../src'; import browserEnv from '../src';
test(t => { test(t => {
t.is(typeof window, 'undefined'); t.is(typeof window, 'undefined');
const returnValue = browserEnv(); const returnValue = browserEnv();
t.is(returnValue, window); t.is(returnValue, window);
const secondReturnValue = browserEnv(); const secondReturnValue = browserEnv();
t.not(returnValue, window); t.not(returnValue, window);
t.is(secondReturnValue, window); t.is(secondReturnValue, window);
}); });

4
test/Function should return window instance.js

@ -2,6 +2,6 @@ import test from 'ava';
import browserEnv from '../src'; import browserEnv from '../src';
test(t => { test(t => {
const returnValue = browserEnv(); const returnValue = browserEnv();
t.is(returnValue, window); t.is(returnValue, window);
}); });

18
test/Function should setup browser environment.js

@ -2,13 +2,13 @@ import test from 'ava';
import browserEnv from '../src'; import browserEnv from '../src';
test(t => { test(t => {
t.is(typeof window, 'undefined'); t.is(typeof window, 'undefined');
t.is(typeof document, 'undefined'); t.is(typeof document, 'undefined');
t.is(typeof navigator, 'undefined'); t.is(typeof navigator, 'undefined');
t.is(typeof HTMLElement, 'undefined'); t.is(typeof HTMLElement, 'undefined');
browserEnv(); browserEnv();
t.not(typeof window, 'undefined'); t.not(typeof window, 'undefined');
t.not(typeof document, 'undefined'); t.not(typeof document, 'undefined');
t.not(typeof navigator, 'undefined'); t.not(typeof navigator, 'undefined');
t.not(typeof HTMLElement, 'undefined'); t.not(typeof HTMLElement, 'undefined');
}); });

2
test/Function shouldn't return the same instance.js

@ -2,5 +2,5 @@ import test from 'ava';
import browserEnv from '../src'; import browserEnv from '../src';
test(t => { test(t => {
t.not(browserEnv(), browserEnv()); t.not(browserEnv(), browserEnv());
}); });

28
test/Properties arg should set globals.js

@ -2,18 +2,18 @@ import test from 'ava';
import browserEnv from '../src'; import browserEnv from '../src';
test(t => { test(t => {
t.is(typeof window, 'undefined'); t.is(typeof window, 'undefined');
t.is(typeof document, 'undefined'); t.is(typeof document, 'undefined');
t.is(typeof navigator, 'undefined'); t.is(typeof navigator, 'undefined');
t.is(typeof HTMLElement, 'undefined'); t.is(typeof HTMLElement, 'undefined');
browserEnv([]); browserEnv([]);
t.is(typeof window, 'undefined'); t.is(typeof window, 'undefined');
t.is(typeof document, 'undefined'); t.is(typeof document, 'undefined');
t.is(typeof navigator, 'undefined'); t.is(typeof navigator, 'undefined');
t.is(typeof HTMLElement, 'undefined'); t.is(typeof HTMLElement, 'undefined');
browserEnv(['navigator', 'HTMLElement']); browserEnv(['navigator', 'HTMLElement']);
t.is(typeof window, 'undefined'); t.is(typeof window, 'undefined');
t.is(typeof document, 'undefined'); t.is(typeof document, 'undefined');
t.not(typeof navigator, 'undefined'); t.not(typeof navigator, 'undefined');
t.not(typeof HTMLElement, 'undefined'); t.not(typeof HTMLElement, 'undefined');
}); });

8
test/jsdom arg should set jsdom config.js

@ -2,8 +2,8 @@ import test from 'ava';
import browserEnv from '../src'; import browserEnv from '../src';
test(t => { test(t => {
const userAgent = 'Custom user agent'; const userAgent = 'Custom user agent';
t.is(typeof navigator, 'undefined'); t.is(typeof navigator, 'undefined');
browserEnv(['navigator'], { userAgent: userAgent }); browserEnv(['navigator'], { userAgent });
t.is(navigator.userAgent, userAgent); t.is(navigator.userAgent, userAgent);
}); });

10
test/window properties should be as expected.js

@ -1,10 +1,10 @@
import test from 'ava'; import test from 'ava';
import expectedProperties from './fixtures/expectedProperties';
import browserEnv from '../src'; import browserEnv from '../src';
import expectedProperties from './fixtures/expectedProperties';
test(t => { test(t => {
browserEnv(); browserEnv();
const properties = Object.getOwnPropertyNames(window); const properties = Object.getOwnPropertyNames(window);
t.is(properties.length, expectedProperties.length); t.is(properties.length, expectedProperties.length);
properties.forEach(prop => t.true(expectedProperties.indexOf(prop) > -1)); properties.forEach(prop => t.true(expectedProperties.indexOf(prop) > -1));
}); });

Loading…
Cancel
Save