seeschweiler / angular-redux Goto Github PK
View Code? Open in Web Editor NEWAngular Redux Demo Application
Angular Redux Demo Application
I need to write unit-test for below the following code. I am writing unit-test for an angular application.
myservice.service.ts
import { Injectable } from '@angular/core';
import { NgRedux } from '@angular-redux/store';
import { MyService } from './../../services/my/my.service';
import { MyModel as ApiMyModel } from '../../../app/services/my/my.model';
import { MyUIModel } from './MyUI.model';
import { IAppState } from '../root-reducer-index.type';
import { MyConstants } from './myConstants.constants';
@Injectable({
providedIn: 'root'
})
export class MyActions {
constructor(private _ngRedux: NgRedux<IAppState>, private
_myService: MyService) {
}
loadData() {
this._myService.loadData()
.subscribe((res) => {
const list = this.mapData(<Array<ApiMyModel>>res);
this._ngRedux.dispatch({
type: MyConstants.DATA_LOADED,
payload: {
list
}
});
});
}
private mapData(list: ApiMyModel[]): MyUIModel[] {
return list.map(i => {
let record: MyUIModel = new MyUIModel();
record.id = i.id;
record.name = i.name;
record.imgSrc = i.imgSrc;
return record;
});
}
}
myservice.service.spec.ts
import { DevToolsExtension, NgRedux } from '@angular-redux/store';
import {
MockNgRedux,
NgReduxTestingModule,
} from '@angular-redux/store/testing';
import { TestBed, getTestBed } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController } from
'@angular/common/http/testing';
import { MyModel } from '../MyModel.model';
import { MyService } from '../../../services/my/my.service';
import { of } from 'rxjs/internal/observable/of';
describe('MyService', () => {
let injector: TestBed;
let myService: MyService;
let httpMock: HttpTestingController;
let data: MyModel[] = new Array<MyModel>();
let mockNgRedux: NgRedux<any>;
let devTools: DevToolsExtension;
function createData(id: number, name: string, imgSrc: string = '') {
let data: MyModel = new MyModel();
data.id = id;
data.name = name;
data.imgSrc = imgSrc;
return data;
}
function mockData() {
data.push(createData(1, "Puma"));
data.push(createData(2, "Nike"));
data.push(createData(3, "Next"));
data.push(createData(4, "iPhone"));
}
beforeEach(() => {
TestBed.configureTestingModule({
imports: [NgReduxTestingModule, HttpClientTestingModule],
providers: [MyService]
});
injector = getTestBed();
mockNgRedux = MockNgRedux.getInstance();
myService = injector.get(MyService);
mockData();
});
describe('#loadData', () => {
it('should have fetched list', () => {
spyOn(myService, 'loadData').and.returnValue(of(data))
// Here I need to mock loaddata and this._ngRedux.dispatch and check the result and action been called.
// Also, need to check that expect(myService.loadData).toHaveBeenCalled();
});
});
});
After run unit-test command, my tests are failing and giving the following error "Expected spy loadBrands to have been called.".
Please help me here. I trying to figure out how can I write the unit-tests but did not get any success.
Also, see the attached screen shot of the code coverage report (do not confuse about the naming).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.