Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

Commit

Permalink
Adds unit test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Elliott Grieco committed Oct 19, 2018
1 parent ec02f1e commit c4f9fb9
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 41 deletions.
15 changes: 9 additions & 6 deletions src/js/libraryh3lp-widget.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ angular
.controller('libraryh3lpWidgetController', ['libraryh3lpWidgetConfig', '$scope', 'libraryh3lpWidgetResultsList', function (libraryh3lpWidgetConfig, $scope, libraryh3lpWidgetResultsList) {
// controller for the h3lp widget itself
let prevResultsListLength;
this.$onInit = function () {
const ctrl = this;
ctrl.$onInit = function () {
$scope.config = libraryh3lpWidgetConfig;
prevResultsListLength = libraryh3lpWidgetResultsList.getLength();
$scope.klasses = {
'chat-bottom-padding': false
'chat-bottom-padding': !!prevResultsListLength,
};
prevResultsListLength = libraryh3lpWidgetResultsList.getLength();
};

this.$doCheck = function () {
ctrl.$doCheck = function () {
const newResultsListLength = libraryh3lpWidgetResultsList.getLength();
if (newResultsListLength !== prevResultsListLength) {
$scope.klasses['chat-bottom-padding'] = !!newResultsListLength;
Expand All @@ -52,12 +53,14 @@ angular
// controller whose pure function is to maintain updated results length in the libraryh3lpWidgetResultsList factory
const ctrl = this;
let prevResultsListLength;
this.$onInit = function () {
// Update length in the factory on initialize
ctrl.$onInit = function () {
prevResultsListLength = ctrl.parentCtrl.searchService.facetService.results.length;
libraryh3lpWidgetResultsList.updateLength(prevResultsListLength);
};

this.$doCheck = function () {
// If length change, update the factory
ctrl.$doCheck = function () {
const newResultsListLength = ctrl.parentCtrl.searchService.facetService.results.length;
if (newResultsListLength !== prevResultsListLength) {
libraryh3lpWidgetResultsList.updateLength(newResultsListLength);
Expand Down
76 changes: 76 additions & 0 deletions src/spec/libraryh3lpResultsList.controller.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const libraryh3lpWidgetConfig = __fixtures__['libraryh3lpWidgetConfig'];

describe('libraryh3lpWidgetResultsListController', () => {

beforeEach(module('libraryh3lpWidget', ($provide) => {
$provide.constant('libraryh3lpWidgetConfig', libraryh3lpWidgetConfig);
}));

let $scope, $componentController;
let controller, bindings;
let libraryh3lpWidgetResultsList;
const parentCtrl = {};
beforeEach(inject(function (_$rootScope_, _$componentController_, _libraryh3lpWidgetResultsList_) {
libraryh3lpWidgetResultsList = _libraryh3lpWidgetResultsList_;
const $rootScope = _$rootScope_;
$componentController = _$componentController_;
$scope = $rootScope.$new();

// content of results irrelevant because it's just checking for length'
angular.merge(parentCtrl, {
searchService: {
facetService: {
results: [null, null, null]
}
}
});
bindings = { parentCtrl };

controller = $componentController('prmExploreMainAfter', { $scope }, bindings);
}));

describe('$onInit', () => {
beforeEach(() => {
spyOn(libraryh3lpWidgetResultsList, 'getLength');
spyOn(libraryh3lpWidgetResultsList, 'updateLength');
controller.$onInit();
});

it('should be defined', () => {
expect(controller.$onInit).toBeDefined();
});

it(`should call libraryh3lpWidgetResultsList.updateLength with length of results`, () => {
expect(libraryh3lpWidgetResultsList.updateLength).toHaveBeenCalledWith(3);
});
});

describe(`$doCheck`, () => {
beforeEach(() => {
spyOn(libraryh3lpWidgetResultsList, 'getLength');
spyOn(libraryh3lpWidgetResultsList, 'updateLength');
controller.$onInit();

libraryh3lpWidgetResultsList.updateLength.calls.reset();
controller.$doCheck();
});

describe(`if length has not changed`, () => {
it(`shouldn't updateLength`, () => {
expect(libraryh3lpWidgetResultsList.updateLength).not.toHaveBeenCalled();
});
});

describe(`if length has changed`, () => {
it(`should updateLength with new length`, () => {
parentCtrl.searchService.facetService.results.push(null);
controller.$doCheck();
expect(libraryh3lpWidgetResultsList.updateLength).toHaveBeenCalledWith(4);

parentCtrl.searchService.facetService.results = [];
controller.$doCheck();
expect(libraryh3lpWidgetResultsList.updateLength).toHaveBeenCalledWith(0);
});
});
});
});
7 changes: 2 additions & 5 deletions src/spec/libraryh3lpWidget.component.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ describe('libraryh3lpWidget component', () => {
$provide.constant("libraryh3lpWidgetConfig", libraryh3lpWidgetConfig);
}));

let $compile, element, parentCtrl;
let $compile, element;
beforeEach(inject(function(_$compile_, $rootScope){
$compile = _$compile_;
const scope = $rootScope.$new();

scope.config = libraryh3lpWidgetConfig;
element = angular.element(`<prm-explore-main-after parentCtrl="parentCtrl" />`);
element = angular.element(`<prm-silent-login-after />`);
element = $compile(element)(scope);
scope.$digest();
}));
Expand Down Expand Up @@ -93,7 +92,5 @@ describe('libraryh3lpWidget component', () => {
expect(ngSrc).toEqual(libraryh3lpWidgetConfig.url);
});
});

});

});
66 changes: 36 additions & 30 deletions src/spec/libraryh3lpWidget.controller.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,19 @@ describe('libraryh3lpWidgetController', () => {

let $scope, $componentController;
let controller, bindings;
beforeEach(inject(function(_$rootScope_, _$componentController_,) {
let libraryh3lpWidgetResultsList;
beforeEach(inject(function (_$rootScope_, _$componentController_, _libraryh3lpWidgetResultsList_) {
libraryh3lpWidgetResultsList = _libraryh3lpWidgetResultsList_;
const $rootScope = _$rootScope_;
$componentController = _$componentController_;
$scope = $rootScope.$new();

// content of results irrelevant because it's just checking for length'
const parentCtrl = {
searchService: {
facetService: {
results: [null, null, null]
}
}
};
bindings = { parentCtrl };

controller = $componentController('prmExploreMainAfter', { $scope }, bindings);
controller = $componentController('prmSilentLoginAfter', { $scope }, bindings);
}));

describe('$onInit', () => {
beforeEach(() => {
spyOn(libraryh3lpWidgetResultsList, 'getLength').and.returnValue(0);
controller.$onInit();
});

Expand All @@ -39,33 +32,46 @@ describe('libraryh3lpWidgetController', () => {
expect($scope.config).toBeDefined();
});

it('should intialize facetsExist', () => {
expect($scope.facetsExist).toBeDefined();
it('should intialize klasses', () => {
expect($scope.klasses).toBeDefined();
});

it('should initialize facetsExist to false if no results', () => {
const noResultsBinding = angular.copy(bindings);
noResultsBinding.parentCtrl.searchService.facetService.results = [];
it(`should getLength from libraryh3lpWidgetResultsList`, () => {
expect(libraryh3lpWidgetResultsList.getLength).toHaveBeenCalled();
});

const $noFacetsScope = $scope.$new();
const noResultsController = $componentController('prmExploreMainAfter',
{ $scope: $noFacetsScope },
noResultsBinding
);
describe(`when initializing 'chat-bottom-padding'`, () => {
it(`should be false if libraryh3lpWidgetResultsList.getLength() === 0`, () => {
expect($scope.klasses['chat-bottom-padding']).toBe(false);
});

noResultsController.$onInit();
expect($noFacetsScope.facetsExist).toBe(false);
it(`should be true if libraryh3lpWidgetResultsList.getLength() > 0`, () => {
libraryh3lpWidgetResultsList.getLength.and.returnValue(10);

controller.$onInit();
expect($scope.klasses['chat-bottom-padding']).toBe(true);
});
});
});

describe('$doCheck', () => {
describe(`klasses responds to libraryh3lpWidgetResultsList.getLength() changes`, () => {
beforeEach(() => {
spyOn(libraryh3lpWidgetResultsList, 'updateLength').and.stub();
spyOn(libraryh3lpWidgetResultsList, 'getLength').and.returnValue(0);
controller.$onInit();
});

describe('if facetsExist', () => {
it('should assign facetsExists to true', () => {
expect($scope.facetsExist).toBe(true);
it(`should change klasses['chat-bottom-padding'] to true if > 0`, () => {
libraryh3lpWidgetResultsList.getLength.and.returnValue(10);
controller.$doCheck();
expect($scope.klasses['chat-bottom-padding']).toBe(true);
});

it('should have bottomPadding', () => {
expect($scope.bottomPadding['chat-bottom-padding']).toBe(true);
it(`should change klasses['chat-bottom-padding'] to false if === 0`, () => {
controller.$doCheck();
expect($scope.klasses['chat-bottom-padding']).toBe(false);
});
});
});

});
31 changes: 31 additions & 0 deletions src/spec/libraryh3lpWidgetResultsList.factory.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
describe('libraryh3lpWidgetResultsList factory', () => {
let libraryh3lpWidgetResultsList;
beforeEach(() => {
module('libraryh3lpWidget');
inject(function (_libraryh3lpWidgetResultsList_) {
libraryh3lpWidgetResultsList = _libraryh3lpWidgetResultsList_;
});
});

describe('getLength', () => {
it('has the function', () => {
expect(libraryh3lpWidgetResultsList.getLength).toBeDefined();
});

it('initialized to 0', () => {
expect(libraryh3lpWidgetResultsList.getLength()).toEqual(0);
});
});

describe('updateLength', () => {
it('has the function', () => {
expect(libraryh3lpWidgetResultsList.updateLength).toBeDefined();
});

it('updates the length', () => {
libraryh3lpWidgetResultsList.updateLength(100);

expect(libraryh3lpWidgetResultsList.getLength()).toEqual(100);
});
});
});

0 comments on commit c4f9fb9

Please sign in to comment.