Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Fix isWindow helper for the Electron environment. #247

Merged
merged 2 commits into from
Jul 5, 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
14 changes: 13 additions & 1 deletion src/dom/iswindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,17 @@
* @returns {Boolean}
*/
export default function isWindow( obj ) {
return Object.prototype.toString.apply( obj ) == '[object Window]';
const stringifiedObject = Object.prototype.toString.apply( obj );

// Returns `true` for the `window` object in browser environments.
if ( stringifiedObject == '[object Window]' ) {
return true;
}

// Returns `true` for the `window` object in the Electron environment.
if ( stringifiedObject == '[object global]' ) {
return true;
}

return false;
}
12 changes: 11 additions & 1 deletion tests/dom/iswindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,22 @@
import isWindow from '../../src/dom/iswindow';

describe( 'isWindow()', () => {
it( 'detects DOM Window', () => {
it( 'detects DOM Window in browsers', () => {
expect( isWindow( window ) ).to.be.true;
expect( isWindow( {} ) ).to.be.false;
expect( isWindow( null ) ).to.be.false;
expect( isWindow( undefined ) ).to.be.false;
expect( isWindow( new Date() ) ).to.be.false;
expect( isWindow( 42 ) ).to.be.false;
} );

it( 'detects DOM Window in the Electron environment', () => {
const global = {
get [ Symbol.toStringTag ]() {
return 'global';
}
};

expect( isWindow( global ) ).to.be.true;
} );
} );