Last active
August 30, 2019 03:24
-
-
Save trodrigues/7414091 to your computer and use it in GitHub Desktop.
Angular.js directive test example with service mocking
This file contains 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
describe('Directive test', function () { | |
var container, scope; | |
var aStub; | |
beforeEach(function () { | |
aStub = sinon.stub(); | |
module('testmodule', function ($provide) { | |
$provide.value('someService', aStub); | |
}); | |
inject(function ($rootScope, $compile) { | |
scope = $rootScope.$new(); | |
// controller for this directive uses the stubbed service | |
// to determine if a button should be shown | |
container = $('<some-directive></some-directive>'); | |
$compile(container)(scope); | |
scope.$digest(); | |
}); | |
}); | |
afterEach(function () { | |
container.remove(); | |
}); | |
it('do not show a button depending on service', function () { | |
aStub.returns(false); | |
scope.$apply(); | |
expect(container.find('.button').hasClass('ng-hide')).toBe(true); | |
}); | |
it('show a button depending on service', function () { | |
aStub.returns(true); | |
scope.$apply(); | |
expect(container.find('.button').hasClass('ng-hide')).toBe(false); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment