describe('PizzaService', () => {
describe('getPizza', () => {
it('should return Salami Pizza when no param given', () => {
const pizzaService = new PizzaService();
const salamiPizza = new Pizza('Salami');
const actual = service.getPizza();
expect(actual).to.eql(salamiPizza);
});
});
});
describe('PizzaService', () => {
describe('getAsyncPizza', () => {
it('should return Salami Pizza asynchronously', () => {
const pizzaService = new PizzaService();
const salamiPizza = new Pizza('Salami');
const promise = service.getAsyncPizza();
promise.then(actual => {
expect(actual).to.eql(salamiPizza);
});
});
});
});
describe('PizzaService', () => {
describe('getAsyncPizza', () => {
it('should return Salami Pizza asynchronously', () => {
let actual;
const pizzaService = new PizzaService();
const salamiPizza = new Pizza('Salami');
service.getAsyncPizza()
.then(pizza => actual = pizza);
$rootScope.$digest;
expect(actual).to.eql(salamiPizza);
});
});
});
describe('PizzaService', () => {
describe('getAsyncPizza', () => {
it('should return Salami Pizza asynchronously', (done) => {
const pizzaService = new PizzaService();
const salamiPizza = new Pizza('Salami');
const promise = service.getAsyncPizza();
promise.then((actual) => {
expect(actual).to.eql(salamiPizza);
done();
}).catch(done);
});
});
});
describe('pizza.service.module', () => {
beforeEach(angular.mock.module('pizza.service.module'));
let PizzaService;
let SomeService;
beforeEach(angular.mock.inject((
_PizzaService_,
_SomeService_
) => {
PizzaService = _PizzaService_;
SomeService = _SomeService_;
});
...
});
describe('calculate', () => {
it('should call SomeService.add two times', () => {
SomeService.add = sinon.spy();
PizzaService.calculate();
sinon.assert.calledTwice(SomeService.add);
});
});
Anything non-Angular is injected by Webpack which we will cover later!
describe('pizza.service.module', () => {
beforeEach(angular.mock.module('pizza.service.module'));
let $compile;
let $rootScope;
beforeEach(angular.mock.inject((
_$compile_,
_$rootScope_
) => {
$compile = _$compile_;
$rootScope = $rootScope;
}));
...
});
it('should store name of Pizza in Scope', () => {
const $scope = $rootScope.$new();
const element = $compile(
` `
)($scope);
$rootScope.$digest();
expect(element.isolateScope().myValue).to.equal('Pizza Salami');
});
it('should show Pizza Title', () => {
const element = $compile(
` `
)($rootScope.$new());
$rootScope.$digest();
expect(element.find('h3').text()).to.equal('Pizza Salami');
});
it('should hide Pizza Title on click', () => {
const element = $compile(
` `
)($rootScope.$new());
$rootScope.$digest();
element.find('h3').click();
$rootScope.$digest();
expect(element.find('h3')).to.be.undefined;
});
We have a TestHelper which does this compiling for us
const result = TestHelper.compileDirective(
$rootScope.$new(),
` `
);
/*{
element,
compiledElement,
directiveScope,
}*/
it('should hide Pizza Title on click', () => {
const actual = TestHelper.compileDirective(
$rootScope.$new(),
` `
);
$rootScope.$digest();
actual.compiledElement.find('h3').click();
$rootScope.$digest();
expect(actual.compiledElement.find('h3')).to.be.undefined;
});