Skip to content

Instantly share code, notes, and snippets.

@demisx
Last active January 28, 2018 03:56
Show Gist options
  • Save demisx/10b4c90fc675014eefe3 to your computer and use it in GitHub Desktop.
Save demisx/10b4c90fc675014eefe3 to your computer and use it in GitHub Desktop.

Revisions

  1. demisx revised this gist Jan 21, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions angularjs1-unit-test.md
    Original file line number Diff line number Diff line change
    @@ -126,3 +126,4 @@ escribe('pageMetaService', function() {
    ## Models
    ## Directives
    Test directive controllers used with BindToController option https://github.com/angular/angular.js/issues/9425#issuecomment-69093402
  2. demisx revised this gist Jan 6, 2015. 1 changed file with 53 additions and 0 deletions.
    53 changes: 53 additions & 0 deletions angularjs1-unit-test.md
    Original file line number Diff line number Diff line change
    @@ -67,6 +67,59 @@ describe('ViewAdDetailController', function() {
    ```
    ## Services
    ```js
    escribe('pageMetaService', function() {
    var pageMetaService;
    var stateMock;

    beforeEach(function() {
    stateMock = {
    current: {
    name: '',
    data: {
    doc: {
    htmlTitle: ''
    }
    }
    }
    };

    module('components.pageMeta', function($provide) {
    $provide.value('$state', stateMock);
    });

    inject(function(_pageMetaService_) {
    pageMetaService = _pageMetaService_;
    });
    });

    describe('htmlTitle()', function() {
    it('returns page html title when present', function() {
    var htmlTitle = "Some html title";

    stateMock.current.data.doc.htmlTitle = htmlTitle;
    expect(pageMetaService.htmlTitle()).to.eql(htmlTitle);
    });

    it('returns empty string when html title is undefined', function() {
    delete stateMock.current.data.doc.htmlTitle;
    expect(pageMetaService.htmlTitle()).to.eql('');
    });
    });

    describe('bodyId()', function() {
    it('returns bodyId add hyphenated state name', function() {
    stateMock.current.name = 'app.area.list';
    expect(pageMetaService.bodyId()).to.eql('app-area-list');
    });

    it('returns empty string when state name is undefined', function() {
    delete stateMock.current.name;
    expect(pageMetaService.bodyId()).to.eql('');
    });
    });
    });
    ```
    ## States
  3. demisx revised this gist Jan 6, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion angularjs1-unit-test.md
    Original file line number Diff line number Diff line change
    @@ -33,7 +33,7 @@ describe('state', function() {
    });

    it('is activated when visiting state URL', function () {
    h.goToUrl('/classifieds/1');
    h.goToUrl('classifieds/1');
    expect($state.current.name).to.eql(stateName);
    });
    });
  4. demisx revised this gist Jan 6, 2015. 1 changed file with 59 additions and 1 deletion.
    60 changes: 59 additions & 1 deletion angularjs1-unit-test.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,62 @@
    ## Controllers
    ## UI States
    ```js
    describe('state', function() {
    var stateName ='app.classified';
    var $state;
    var h;

    beforeEach(function() {
    module('unitTestTemplates');
    module('helpers.unit.state');
    module('states.app');
    module('states.app.classified', function($provide) {
    $provide.value('Classified', {
    get: sinon.stub().returns('something')
    });
    });

    inject(function(_$state_, _stateTestHelpers_) {
    $state = _$state_;
    h = _stateTestHelpers_;
    });
    });

    context(stateName, function() {
    it('resolves classified property', function() {
    expect(h.resolve('classified').forStateAndView(stateName))
    .to.eql('something');
    });

    it('maps to correct state URL', function() {
    expect($state.href(stateName, { classifiedId: 1 }))
    .to.eql('#/classifieds/1');
    });

    it('is activated when visiting state URL', function () {
    h.goToUrl('/classifieds/1');
    expect($state.current.name).to.eql(stateName);
    });
    });

    context('title@app view', function() {
    it('controller defines `classified` instance property', function() {
    ctrlFn = h.getController().forStateAndView(stateName, 'title@app');
    expect(new ctrlFn({}, $state)).to.have.ownProperty('classified');
    });

    it('changeClassified() transitions state to another classified', function() {
    $state.go = sinon.spy();
    ctrlFn = h.getController().forStateAndView(stateName, 'title@app');
    ctrl = new ctrlFn({}, $state);
    ctrl.classified.current.id = 1;

    ctrl.changeClassified();
    expect($state.go).calledWith(stateName, { classifiedId: 1 });
    });
    });
    });
    ```
    ## Application controllers

    ```js
    describe('ViewAdDetailController', function() {
  5. demisx revised this gist Jan 5, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion sinon-stub.js
    Original file line number Diff line number Diff line change
    @@ -8,4 +8,4 @@ stub.withArgs("Kapow").throws();
    stub.withArgs(opts).yieldsTo("call", ["Howdy"]);

    stub("Hello"); // "World"
    stub(options); // "Howdy"
    stub(opts); // "Howdy"
  6. demisx revised this gist Jan 5, 2015. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions sinon-mock.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    var opts = { call: function (msg) { console.log(msg); } };
    var mock = sinon.mock(opts);

    // You state your success criteria upfront
    mock.expects("call").once().withExactArgs("Hello World");
    /* ... twice, atMost, never, exactly, on, etc ... */

    opts.call("Hello World");

    mock.verify();
    mock.restore();
  7. demisx revised this gist Jan 5, 2015. 2 changed files with 11 additions and 0 deletions.
    File renamed without changes.
    11 changes: 11 additions & 0 deletions sinon-stub.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    var stub = sinon.stub();
    var opts = { call: function (msg) { console.log(msg); } };

    // We can control how the sinon.stub() will behave based on how it’s called!
    stub.withArgs("Hello").returns("World");
    stub.withArgs("Wuz").returns("Zup?");
    stub.withArgs("Kapow").throws();
    stub.withArgs(opts).yieldsTo("call", ["Howdy"]);

    stub("Hello"); // "World"
    stub(options); // "Howdy"
  8. demisx revised this gist Jan 5, 2015. 1 changed file with 19 additions and 0 deletions.
    19 changes: 19 additions & 0 deletions misc.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    // Sinon spy function
    var callback = sinon.spy();
    callback();
    callback.called;
    callback.callCount;
    callback.calledWith(arg1);
    callback.threw();
    callback.returned(obj);
    callback.calledBefore(spy);
    callback.calledAfter(spy);

    // Sinon spy method
    sinon.spy($, "ajax");
    $.ajax({ / ... / });
    var call = $.ajax.getCall(0);
    call.args;
    call.exception;
    call.returnValue;
    $.ajax.restore();
  9. demisx revised this gist Jan 5, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion angularjs1-unit-test.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    ## Controllers

    ```js
    describe(function() {
    describe('ViewAdDetailController', function() {
    it('does something', function() {
    ...
    });
  10. demisx revised this gist Jan 5, 2015. 1 changed file with 10 additions and 2 deletions.
    12 changes: 10 additions & 2 deletions angularjs1-unit-test.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,17 @@
    # Controllers
    ## Controllers

    ```js
    describe(function() {
    it('does something', function() {
    ...
    });
    }
    ```
    ```
    ## Services
    ## States
    ## Models
    ## Directives
  11. demisx revised this gist Jan 5, 2015. 2 changed files with 9 additions and 1 deletion.
    9 changes: 9 additions & 0 deletions angularjs1-unit-test.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    # Controllers

    ```js
    describe(function() {
    it('does something', function() {
    ...
    });
    }
    ```
    1 change: 0 additions & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    // Controllers
  12. demisx created this gist Jan 5, 2015.
    1 change: 1 addition & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    // Controllers