Repository: auth0/auth0.js
Pull request #764 (2018-05-31 12:28:25)

Remove global from window helpers
Hello,

* I removed `global` from window helpers to make it Angular 6 compatible.
* Also, configured webpack to prevent issue `global` in the future (it does it automatically when encounters this variable).
* And some minor code cleanup.

I left current testing approach intact (manually mock `window` object). I think in the future it can be replaced with [JSDOM](https://github.com/jsdom/jsdom) or similar implementation

Related issues #759 #755 #754 #753 #748 

luisrudge: Thanks for the PR. Do you know why this line lost coverage? https://codecov.io/gh/auth0/auth0.js/compare/5a531f5013e77b0185e645954e5b505ab61b16db...2966805bd0100ede52a9aef1e81dd21ba5347d68/changes
fetis: I assume it's because of this change https://github.com/auth0/auth0.js/pull/764/files#diff-fa3d40d414115b7a8cf9ef99f7282b22R173, which I took as someone accidentally duplicated the line.s

But I think it should be restored in a more explicit way, because popup_handler may return the current instance
luisrudge: ahaha, yeah.. good catch! That's probably it, considering it's testing `should open the window once`
fetis: I've fixed it. Show go back to normal now.

Patch:
diff --git a/src/helper/window.js b/src/helper/window.js
index 70e7a8e..dccd1e9 100644
--- a/src/helper/window.js
+++ b/src/helper/window.js
@@ -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;
 }
 
diff --git a/webpack.config.js b/webpack.config.js
index de3843d..9848c18 100644
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -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');
@@ -57,6 +56,9 @@ module.exports = {
   stylus: {
     preferPathResolver: 'webpack'
   },
+  node: {
+    global: false
+  },
   plugins: [
     new CustomVarLibraryNamePlugin({
       name: nameOverrides
diff --git a/webpack.prod.config.js b/webpack.prod.config.js
index 47f7900..90653d2 100644
--- a/webpack.prod.config.js
+++ b/webpack.prod.config.js
@@ -57,6 +57,9 @@ module.exports = {
     modules: true,
     reasons: true
   },
+  node: {
+    global: false
+  },
   plugins: [
     new CustomVarLibraryNamePlugin({
       name: nameOverrides


Test patch:
diff --git a/test/helper/popup-handler.test.js b/test/helper/popup-handler.test.js
index bc6ee4c..be1ae7e 100644
--- a/test/helper/popup-handler.test.js
+++ b/test/helper/popup-handler.test.js
@@ -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() {
@@ -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++;
@@ -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();
     });
diff --git a/test/helper/window.test.js b/test/helper/window.test.js
index 183752a..407d1e0 100644
--- a/test/helper/window.test.js
+++ b/test/helper/window.test.js
@@ -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');
@@ -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');
diff --git a/test/setup.js b/test/setup.js
index 8eae22e..d99f7ea 100644
--- a/test/setup.js
+++ b/test/setup.js
@@ -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
 });