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

Remove global from window helpers #764

Merged
merged 3 commits into from
Jun 7, 2018
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
10 changes: 6 additions & 4 deletions src/helper/window.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
var objectHelper = require('./object');

function redirect(url) {
global.window.location = url;
getWindow().location = url;
}

function getDocument() {
return global.window.document;
return getWindow().document;
}

function getWindow() {
return global.window;
return window;
}

function getOrigin() {
var location = global.window.location;
var location = getWindow().location;
var origin = location.origin;

if (!origin) {
origin = objectHelper.getOriginFromUrl(location.href);
}

return origin;
}

Expand Down
8 changes: 4 additions & 4 deletions test/helper/popup-handler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ var expect = require('expect.js');
var stub = require('sinon').stub;
var WinChan = require('winchan');

var qs = require('qs');
var PopupHandler = require('../../src/helper/popup-handler');

describe('helpers popupHandler', function() {
Expand Down Expand Up @@ -155,7 +154,7 @@ describe('helpers popupHandler', function() {
popup.kill();
});

it('should open the window once', function(done) {
it('should open the window once and return the same instance', function(done) {
var counter = 0;
global.window.open = function(url, name, windowFeatures) {
counter++;
Expand All @@ -172,9 +171,10 @@ describe('helpers popupHandler', function() {
};

var handler = new PopupHandler();

var popup = handler.preload({});
var popup = handler.preload({});
var popup2 = handler.preload({});

expect(popup).to.be(popup2);

popup.kill();
});
Expand Down
6 changes: 6 additions & 0 deletions test/helper/window.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ describe('helpers window', function() {
global.window.document = { body: {} };
});

afterEach(function() {
delete global.window;
});

it('should redirect', function() {
windowHelper.redirect('http://example.com');
expect(global.window.location).to.be('http://example.com');
Expand All @@ -22,11 +26,13 @@ describe('helpers window', function() {
var _window = windowHelper.getWindow();
expect(_window).to.eql({ document: { body: {} }, location: '' });
});

describe('getOrigin', function() {
it('should use window.location.origin when available', function() {
global.window = { location: { origin: 'origin' } };
expect(windowHelper.getOrigin()).to.be('origin');
});

it('should build current origin when location.origin is not available', function() {
global.window = { location: { href: 'http://hostname:30/foobar' } };
expect(windowHelper.getOrigin()).to.be('http://hostname:30');
Expand Down
10 changes: 1 addition & 9 deletions test/setup.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
before(function() {
function atob(str) {
return new Buffer(str, 'base64').toString('binary');
}
function btoa(str) {
return Buffer.from(str, 'binary').toString('base64');
}
var windowOrGlobal = typeof window !== 'undefined' ? window : global;
windowOrGlobal.atob = atob;
windowOrGlobal.btoa = btoa;
// provide your initial setup here
});
4 changes: 3 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var fs = require('fs');
var webpack = require('webpack');
var CustomVarLibraryNamePlugin = require('webpack-custom-var-library-name-plugin');

var path = require('path');
Expand Down Expand Up @@ -57,6 +56,9 @@ module.exports = {
stylus: {
preferPathResolver: 'webpack'
},
node: {
global: false
},
plugins: [
new CustomVarLibraryNamePlugin({
name: nameOverrides
Expand Down
3 changes: 3 additions & 0 deletions webpack.prod.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ module.exports = {
modules: true,
reasons: true
},
node: {
global: false
},
plugins: [
new CustomVarLibraryNamePlugin({
name: nameOverrides
Expand Down