Last active
August 28, 2025 08:28
-
-
Save Integralist/8002000 to your computer and use it in GitHub Desktop.
Mocking the `window` object in JavaScript unit tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| define([ | |
| 'module/bootstrap', // this is jQuery and PubSub libs | |
| ], function (lib) { | |
| var windowMock = { | |
| resizeSet: false, // set within the fake `lib` object below | |
| createFakeWindow: function(width, height) { | |
| return { | |
| document: { | |
| documentElement: { | |
| clientWidth: width, | |
| clientHeight: height | |
| } | |
| }, | |
| resizeTo: function(width, height) { | |
| this.document.documentElement = { | |
| clientWidth: width, | |
| clientHeight: height | |
| }; | |
| } | |
| }; | |
| }, | |
| fireResizeEvent: function() { | |
| this.handler(); // set within the fake `lib` object below | |
| }, | |
| lib: { | |
| $: function(element) { | |
| return { | |
| on: function(event, handler) { | |
| if (event === 'resize') { | |
| windowMock.resizeSet = true; | |
| windowMock.handler = handler; | |
| } | |
| } | |
| }; | |
| }, | |
| pubsub: lib.pubsub | |
| } | |
| }; | |
| return windowMock; | |
| } | |
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function createFakeWindow (width, height) { | |
| var fakeWindow = WindowMock.createFakeWindow(width, height); | |
| someObject.init(fakeWindow, WindowMock.lib); // this can be removed or changed appropriately | |
| return fakeWindow; | |
| } | |
| function resizeWindow (width, height, fakeWindow) { | |
| document.body.style.width = width + 'px'; // remember to reset this in the test tear down | |
| fakeWindow.resizeTo(width, height); | |
| WindowMock.fireResizeEvent(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| define(['module/bootstrap'], function (lib) { | |
| var $, pubsub, someObject; | |
| someObject = { | |
| // Passing mocks to production code stinks! | |
| // Would love others to suggest a better solution | |
| init: function (windowMock, libMock) { | |
| this.global = windowMock || window; | |
| this.docEl = this.global.document.documentElement || document.documentElement; | |
| this.lib = libMock || lib; | |
| $ = this.lib.$; | |
| pubsub = this.lib.pubsub; | |
| } | |
| }; | |
| return someObject; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment