I'm trying to test an angular component using $componentController
I'm only trying to test inputs and outputs of public code, so in my controller the " this.data
" property is highest priority. Starting with input of this.data
is easy as it's just testing the class constructor prop, which is by default set to undefined
.
The output is the problem, it has a lot of code to call through before getting the expected value.
How do I attach to $q.all.resolve() and test the this.data
's mapped output? The expectation is that expect(ctrl.data).toEqual(expectedData)
.
Controller.js:
class Controller {
/**
* @ngInject
*/
constructor(
someService, $q, $scope) {
this.ngScope_ = $scope;
this.someService_ = someService;
this.ngQ_ = $q;
this.data = undefined;
}
$onInit() {
this.getData_();
}
getData_() {
let requests = [1,2];
const REQUEST_TYPE = 'SOME_REQUEST';
const outEachApproval = (d) => d[0].approvals;
const requestsAsArr = requests.split(',');
const promiseArr = requestsAsArr.map(
(i) => this.someService.getAllDataByType(
REQUEST_TYPE, i));
this.ngQ_.all(promiseArr).then((data) => {
this.data = data.map(outEachApproval); // How the hell do I test this.data at this point in time in the unit test?
});
}
Controller_test.js
describe('someModule', ()=> {
let $componentController,
ctrl,
$rootScope,
$q;
const mockData = [[{approvals: [{name: 'hey'}]},{approvals: [{name: 'hey'}]}]];
const expectedData = [[{name:'hey'}],[{name:'hey'}]];
beforeEach(() => {
module(someModule.name);
inject((_$componentController_, _$rootScope_, _$q_) => {
$componentController = _$componentController_;
$q = _$q_;
$rootScope = _$rootScope_;
});
ctrl = $componentController(COMPONENT_NAME,
{
$scope: $rootScope.$new(),
someService: {
getAllDataByType: () => {
return Promise.resolve(mockData);
}
}
}, {});
});
describe('this.data input', ()=> {
it('should be undefined', () => {
expect(ctrl.data).toBeUndefined();
});
});
describe('this.data output', ()=> {
it('should be equal to expectedData after init', (done) => {
ctrl.$onInit();
expect(ctrl.data).toEqual(expectedData);
ctrl.ngScope_.$apply();
done();
});
});
});
I see several ways to deal with it:
First let your someService
mock to be available in your tests:
let someServiceMock = {
getAllDataByType: () => {
//we can leave this empty
}
};
...
beforeEach(() => {
...
ctrl = $componentController(COMPONENT_NAME,
{
$scope: $rootScope.$new(),
someService: someServiceMock
}, {});
});
Then in your tests simply spy on it:
describe('this.data output', ()=> {
it('should be equal to expectedData after init', (done) => {
//as we use $q.all(), we need to create array of promises
let deferred1 = $q.defer();
...
let deferredN = $q.defer();
let arrayOfPromises = [deferred1.promise, ... ,deferredN.promise];
//then we spy on needed method and mock its return value
spyOn(someServiceMock, 'getAllDataByType').and.returnValue(arrayOfPromises);
ctrl.$onInit();
expect(someServiceMock.getAllDataByType).toHaveBeenCalled();
//now we resolve our promises with any data we want
let resolveData1 = /*Promise#1 mocked data*/;
deferred1.resolve(resolveData1)
let resolveData2 = /*Promise#2 mocked data*/;
deferred2.resolve(resolveData2)
...
let resolveDataN = /*Promise#N mocked data*/;
deferredN.resolve(resolveDataN)
//$q.all() returns array, so expectedData would be array
let expectedData = [
resolveData1,
resolveData2,
...
resolveDataN
];
//then we need to apply changes
$rootScope.$apply();
expect(ctrl.data).toEqual(expectedData);
ctrl.ngScope_.$apply();
done();
});
});
Here is a simple plunker to play with multiple $q.defer()
instances.
Btw, if requests in someServiceMock.getAllDataByType()
are implemented via angular $http
, then you can mock them with $httpBackend
service.
Create a mock for $q
service in your component controller:
let $qMock = {
all: () => {
//we can leave this empty
}
}
...
beforeEach(() => {
...
ctrl = $componentController(COMPONENT_NAME,
{
$scope: $rootScope.$new(),
someService: {
getAllDataByType: () => {
return Promise.resolve(mockData);
}
},
$q: $qMock
}, {});
});
Then in your tests simply spy on it:
describe('this.data output', ()=> {
it('should be equal to expectedData after init', (done) => {
let deferred = $q.defer();
spyOn($qMock, 'all').and.returnValue(deferred.promise);
ctrl.$onInit();
expect($qMock.all).toHaveBeenCalled();
deferred.resolve(expectedData);
//now we need to apply changes
$rootScope.$apply();
expect(ctrl.data).toEqual(expectedData);
ctrl.ngScope_.$apply();
done();
});
});
This test is simpler, but notice that it doesn't depend on what someService.getAllDataByType()
will return.
Some useful articles on subject:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.