From 565a8a8ec97aa7a63ea256fb046f5a6f8cfb75e1 Mon Sep 17 00:00:00 2001 From: Dmitry Fisenko Date: Tue, 24 Apr 2018 12:17:24 -0400 Subject: [PATCH] Add tests --- .gitignore | 3 +- jest.config.json | 1 - package.json | 10 ++--- .../modules/core/state/layout.state.spec.ts | 28 ++++++++++++ .../modules/places/state/filter.state.spec.ts | 43 +++++++++++++++++++ src/app/modules/places/state/filter.state.ts | 5 ++- 6 files changed, 80 insertions(+), 10 deletions(-) create mode 100644 src/app/modules/core/state/layout.state.spec.ts create mode 100644 src/app/modules/places/state/filter.state.spec.ts diff --git a/.gitignore b/.gitignore index 813eae6..ba28a38 100644 --- a/.gitignore +++ b/.gitignore @@ -44,4 +44,5 @@ Thumbs.db package-lock.json # application files -*version.ts \ No newline at end of file +*version.ts +junit.xml \ No newline at end of file diff --git a/jest.config.json b/jest.config.json index bb42a5d..ea29bda 100644 --- a/jest.config.json +++ b/jest.config.json @@ -9,7 +9,6 @@ "/node_modules/", "index.ts" ], - "mapCoverage": true, "testURL": "http://localhost:4200", "roots": [ "/src/" diff --git a/package.json b/package.json index d9b85a1..15f78c7 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,9 @@ "ng": "ng", "start": "ng serve", "build": "npm run version && ng build --prod", - "test": "jest", - "test.watch": "jest --watch", + "test": "jest --config jest.config.json", + "test.watch": "jest --watchAll --config jest.config.json", + "test.coverage": "jest --ci --coverage --testResultsProcessor=jest-junit --config jest.config.json", "lint": "ng lint", "e2e": "ng e2e", "version": "node ./scripts/version.js", @@ -63,6 +64,7 @@ "jasmine-spec-reporter": "~4.2.1", "jest": "^22.4.3", "jest-preset-angular": "^5.2.1", + "jest-junit": "^3.6.0", "karma": "~2.0.0", "karma-chrome-launcher": "~2.2.0", "karma-coverage-istanbul-reporter": "^1.4.2", @@ -72,9 +74,5 @@ "ts-node": "~4.1.0", "tslint": "~5.9.1", "typescript": "~2.7.2" - }, - "jest": { - "preset": "jest-preset-angular", - "setupTestFrameworkScriptFile": "/src/test.jest.ts" } } diff --git a/src/app/modules/core/state/layout.state.spec.ts b/src/app/modules/core/state/layout.state.spec.ts new file mode 100644 index 0000000..e582d24 --- /dev/null +++ b/src/app/modules/core/state/layout.state.spec.ts @@ -0,0 +1,28 @@ +import { async, TestBed } from '@angular/core/testing'; +import { NgxsModule, Store } from '@ngxs/store'; + +import { OpenSidenav, CloseSidenav } from './layout.actions'; +import { LayoutState } from './layout.state'; + +describe('Layout', () => { + let store: Store; + + beforeEach(async(() => { + TestBed.configureTestingModule({ imports: [NgxsModule.forRoot([LayoutState])] }).compileComponents(); + store = TestBed.get(Store); + })); + + it('it opens side navigation panel', () => { + store.dispatch(new OpenSidenav()); + store.selectOnce(state => state.layout.showSidenav).subscribe(showSidenav => { + expect(showSidenav).toBe(true); + }); + }); + + it('it closes side navigation panel', () => { + store.dispatch(new CloseSidenav()); + store.selectOnce(state => state.layout.showSidenav).subscribe(showSidenav => { + expect(showSidenav).toBe(false); + }); + }); +}); diff --git a/src/app/modules/places/state/filter.state.spec.ts b/src/app/modules/places/state/filter.state.spec.ts new file mode 100644 index 0000000..d334b5b --- /dev/null +++ b/src/app/modules/places/state/filter.state.spec.ts @@ -0,0 +1,43 @@ +import { async, TestBed } from '@angular/core/testing'; +import { NgxsModule, Store } from '@ngxs/store'; + +import { SetDistance, SetLocation, SetRating, SetReviews } from './filter.actions'; +import { FilterState } from './filter.state'; + +describe('Filter', () => { + let store: Store; + + beforeEach(async(() => { + TestBed.configureTestingModule({ imports: [NgxsModule.forRoot([FilterState])] }).compileComponents(); + store = TestBed.get(Store); + })); + + it('it should set location', () => { + store.dispatch(new SetLocation({ latitude: 10, longitude: 10 })); + store.selectOnce(state => state.filter.location).subscribe(location => { + expect(location.latitude).toBe(10); + expect(location.longitude).toBe(10); + }); + }); + + it('it should set distance', () => { + store.dispatch(new SetDistance(100)); + store.selectOnce(state => state.filter.distance).subscribe(distance => { + expect(distance).toBe(100); + }); + }); + + it('it should set rating', () => { + store.dispatch(new SetRating(5)); + store.selectOnce(state => state.filter.rating).subscribe(rating => { + expect(rating).toBe(5); + }); + }); + + it('it should set reviews', () => { + store.dispatch(new SetReviews(50)); + store.selectOnce(state => state.filter.reviews).subscribe(reviews => { + expect(reviews).toBe(50); + }); + }); +}); diff --git a/src/app/modules/places/state/filter.state.ts b/src/app/modules/places/state/filter.state.ts index 1367743..fb70443 100644 --- a/src/app/modules/places/state/filter.state.ts +++ b/src/app/modules/places/state/filter.state.ts @@ -1,7 +1,8 @@ -import { Location } from '@app/modules/places/models'; -import { SetDistance, SetLocation, SetRating, SetReviews } from '@app/modules/places/state/filter.actions'; import { Action, Selector, State, StateContext } from '@ngxs/store'; +import { Location } from '../models'; +import { SetDistance, SetLocation, SetRating, SetReviews } from './filter.actions'; + export interface FilterStateModel { location: Location; distance: number;