Coder Social home page Coder Social logo

angular-redux's People

Contributors

angular-cli avatar seeschweiler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

angular-redux's Issues

Unit-tests for actions in angular redux

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).

actual_service_with_code_coverage

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.