Skip to content

Instantly share code, notes, and snippets.

@Integralist
Last active January 1, 2018 20:38
Show Gist options
  • Save Integralist/5772010 to your computer and use it in GitHub Desktop.
Save Integralist/5772010 to your computer and use it in GitHub Desktop.

Revisions

  1. Integralist revised this gist Jul 16, 2013. 2 changed files with 21 additions and 1 deletion.
    22 changes: 21 additions & 1 deletion DeviceInspectorSpec.js → 1. Example Spec.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    var mocks = {
    resizeCalled: false,

    @@ -13,6 +12,14 @@ var mocks = {
    }
    },

    history: {
    back: function(){}
    },

    location: {
    replace: function(){}
    },

    resizeTo: function(width, height) {
    this.document.documentElement = {
    clientWidth: width,
    @@ -58,4 +65,17 @@ it('should only publish `device` event when device type changes', function(){

    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();
    });
    File renamed without changes.
  2. Integralist revised this gist Jun 13, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion DeviceInspectorSpec.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@


    var mocks = {
    resizeCalled: false,

  3. Integralist created this gist Jun 13, 2013.
    62 changes: 62 additions & 0 deletions DeviceInspectorSpec.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@


    var mocks = {
    resizeCalled: false,

    createFakeWindow: function(width, height) {
    var module = this;

    return {
    document: {
    documentElement: {
    clientWidth: width,
    clientHeight: height
    }
    },

    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();
    });
    8 changes: 8 additions & 0 deletions deviceInspector.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    function bindEvents() {
    news.$(global).on('resize', handleResize);
    }

    function init(windowMock, newsMock) {
    global = windowMock || window;
    news = newsMock || newsModule;
    }