Last active
January 1, 2018 20:38
-
-
Save Integralist/5772010 to your computer and use it in GitHub Desktop.
Mocking a Window object for unit-testing purposes
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
var mocks = { | |
resizeCalled: false, | |
createFakeWindow: function(width, height) { | |
var module = this; | |
return { | |
document: { | |
documentElement: { | |
clientWidth: width, | |
clientHeight: height | |
} | |
}, | |
history: { | |
back: function(){} | |
}, | |
location: { | |
replace: function(){} | |
}, | |
resizeTo: function(width, height) { | |
this.document.documentElement = { | |
clientWidth: width, | |
clientHeight: height | |
}; | |
} | |
}; | |
}, | |
fireResizeEvent: function() { | |
this.handler(); | |
}, | |
news: { | |
$: function(element) { | |
return { | |
on: function(event, handler) { | |
if (event === 'resize') { | |
mocks.resizeCalled = true; | |
} | |
mocks.handler = handler; | |
} | |
}; | |
} | |
} | |
}; | |
it('should bind to `resize` event on `init`', function(){ | |
var fakeWindow = mocks.createFakeWindow(320, 480); | |
deviceInspector.init(fakeWindow, mocks.news); | |
expect(mocks.resizeCalled).toBeTruthy(); | |
}); | |
it('should only publish `device` event when device type changes', function(){ | |
var fakeWindow = mocks.createFakeWindow(1008, 1024), | |
device = deviceInspector.init(fakeWindow, mocks.news); | |
fakeWindow.resizeTo(960, 1024); | |
mocks.fireResizeEvent(); | |
fakeWindow.resizeTo(320, 480); | |
mocks.fireResizeEvent(); | |
}); | |
it('should take the user back to the previous page in their history', function(){ | |
var fakeWindow = mocks.createFakeWindow(), | |
fakeWindowHistoryBackSpy = sinon.spy(fakeWindow.history, 'back'), | |
fakeWindowLocationReplaceSpy = sinon.spy(fakeWindow.location, 'replace'); | |
pictureViewer.init(fakeWindow); | |
$('.picture-viewer__button--back').trigger('click'); | |
expect(fakeWindowHistoryBackSpy).toHaveBeenCalledOnce(); | |
expect(fakeWindowLocationReplaceSpy).toHaveBeenCalledOnce(); | |
}); |
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 bindEvents() { | |
news.$(global).on('resize', handleResize); | |
} | |
function init(windowMock, newsMock) { | |
global = windowMock || window; | |
news = newsMock || newsModule; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment