Skip to content

Instantly share code, notes, and snippets.

@aj-dev
Last active December 12, 2016 14:14
Show Gist options
  • Save aj-dev/ffefafda7e44f0e2229d5692162bf940 to your computer and use it in GitHub Desktop.
Save aj-dev/ffefafda7e44f0e2229d5692162bf940 to your computer and use it in GitHub Desktop.
`CardsController` unit tests
describe('CardsController', function () {
'use strict';
var $controller, controller, cardsMock;
var cardServiceMock = jasmine.createSpyObj('cardService', [
'getColumns',
'updateColumnState',
'getOrderTitles',
'orderBy',
'getSortOptions',
'updateOptionState'
]);
function services($injector) {
cardsMock = $injector.get('cardsMock');
$controller = $injector.get('$controller');
}
function instantiateController() {
controller = $controller('CardsController', {
cardService: cardServiceMock
}, {
cards: cardsMock.cards,
profile: {
permissions: ['CARD:CREATE']
}
});
}
beforeEach(function () {
module('evbox');
inject(services);
instantiateController();
});
describe('when controller is initialised', function () {
beforeEach(function () {
cardServiceMock.getColumns.and.returnValue([{}, {}]);
controller.$onInit();
});
it('has default state', function () {
expect(controller.state).toEqual({
viewMode: 'list',
canRegisterCard: true
});
});
describe('when view mode is changed', function () {
it('updates the state', function () {
controller.setMode('grid');
expect(controller.state.viewMode).toBe('grid');
});
});
describe('card reordering', function () {
beforeEach(function () {
cardServiceMock.orderBy.and.callFake(function (cards) {
return cards;
});
});
afterEach(function () {
cardServiceMock.orderBy.calls.reset();
});
it('reorders via column headers in list view', function () {
var column = {
property: 'holder',
isDesc: false
};
controller.orderBy(column);
expect(cardServiceMock.updateColumnState).toHaveBeenCalledWith(controller.columns, column);
expect(cardServiceMock.orderBy).toHaveBeenCalledWith(controller.cards, column.property, column.isDesc);
});
it('reorders via drop-down in grid view', function () {
var option = {
property: 'reference',
isDesc: true
};
controller.selectOrderBy(option);
expect(cardServiceMock.updateColumnState).toHaveBeenCalledWith(controller.columns, {property: option.property, isDesc: !option.isDesc});
expect(cardServiceMock.orderBy).toHaveBeenCalledWith(controller.cards, option.property, option.isDesc);
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment