From a19b60d8226f43cad0f879a67f464bd61140731f Mon Sep 17 00:00:00 2001 From: SooryaSoorya Date: Wed, 29 Apr 2020 14:25:47 +0300 Subject: [PATCH] test cases modifies, gh-page config added --- package.json | 7 +- src/app/app.component.spec.ts | 13 +++ src/app/loader/loader.component.spec.ts | 20 +++++ .../movie-detail.component.spec.ts | 29 ++++++ src/app/movie/movie.component.spec.ts | 89 ++++++++++++++++--- src/app/movie/movie.component.ts | 3 +- src/app/nav-menu/nav-menu.component.html | 2 +- src/app/nav-menu/nav-menu.component.spec.ts | 53 +++++++++-- src/app/no-data/no-data.component.spec.ts | 18 ++++ .../search-bar/search-bar.component.spec.ts | 21 +++++ src/app/sidebar/sidebar.component.spec.ts | 21 +++++ 11 files changed, 253 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index 44cc47e..1d6f46c 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,15 @@ { "name": "ng-movie-app", "version": "0.0.0", + "homepage": "https://SooryaSoorya.github.io/ng-movie-app", "scripts": { "ng": "ng", "start": "ng serve", "build": "ng build", "test": "ng test", "lint": "ng lint", - "e2e": "ng e2e" + "e2e": "ng e2e", + "deploy": "gh-pages -d dist" }, "private": true, "dependencies": { @@ -28,10 +30,11 @@ "@angular/cli": "9.1.1", "@angular/compiler-cli": "9.1.2", "@angular/language-service": "9.1.2", - "@types/node": "^12.11.1", "@types/jasmine": "3.5.0", "@types/jasminewd2": "2.0.3", + "@types/node": "^12.11.1", "codelyzer": "^5.1.2", + "gh-pages": "^2.2.0", "jasmine-core": "3.5.0", "jasmine-spec-reporter": "4.2.1", "karma": "4.3.0", diff --git a/src/app/app.component.spec.ts b/src/app/app.component.spec.ts index 0bc5cd2..2bd02ea 100644 --- a/src/app/app.component.spec.ts +++ b/src/app/app.component.spec.ts @@ -10,6 +10,7 @@ import { MovieService } from "../app/movie/services/movie.services"; describe("AppComponent", () => { beforeEach(async(() => { + const a = setup().default(); TestBed.configureTestingModule({ imports: [ BrowserAnimationsModule, @@ -34,3 +35,15 @@ describe("AppComponent", () => { expect(app.title).toEqual("ng-movie-app"); }); }); + +function setup() { + const builder = { + default() { + return builder; + }, + build() { + return new AppComponent(); + } + } + return builder; +} \ No newline at end of file diff --git a/src/app/loader/loader.component.spec.ts b/src/app/loader/loader.component.spec.ts index 35adf49..82afcc8 100644 --- a/src/app/loader/loader.component.spec.ts +++ b/src/app/loader/loader.component.spec.ts @@ -7,6 +7,7 @@ describe('LoaderComponent', () => { let fixture: ComponentFixture; beforeEach(async(() => { + const a = setup().default(); TestBed.configureTestingModule({ declarations: [ LoaderComponent ] }) @@ -22,4 +23,23 @@ describe('LoaderComponent', () => { it('should create', () => { expect(component).toBeTruthy(); }); + it('when ngOnInit is called it should', () => { + + const { build } = setup().default(); + const c = build(); + + c.ngOnInit(); + }); }); + +function setup() { + const builder = { + default() { + return builder; + }, + build() { + return new LoaderComponent(); + } + } + return builder; +} \ No newline at end of file diff --git a/src/app/movie-detail/movie-detail.component.spec.ts b/src/app/movie-detail/movie-detail.component.spec.ts index 48c5430..7d2cd40 100644 --- a/src/app/movie-detail/movie-detail.component.spec.ts +++ b/src/app/movie-detail/movie-detail.component.spec.ts @@ -10,6 +10,7 @@ describe("MovieDetailComponent", () => { let fixture: ComponentFixture; beforeEach(async(() => { + const a = setup().default(); TestBed.configureTestingModule({ imports: [HttpClientTestingModule], declarations: [MovieDetailComponent], @@ -32,4 +33,32 @@ describe("MovieDetailComponent", () => { it("should create", () => { expect(component).toBeTruthy(); }); + it("when ngOnInit is called it should", () => { + const { build } = setup().default(); + const c = build(); + c.ngOnInit(); + }); + it("when getMovieDetails is called it should", () => { + const { build } = setup().default(); + const param = { id: "1" }; + const c = build(); + c.getMovieDetails(param["id"]); + }); }); + +function setup() { + const activatedRoute = jasmine.createSpyObj(ActivatedRoute); + const httpClientSpy = jasmine.createSpyObj("HttpClient", ["get"]); + const movieService = new MovieService(httpClientSpy); + const builder = { + movieService, + activatedRoute, + default() { + return builder; + }, + build() { + return new MovieDetailComponent(movieService, activatedRoute); + }, + }; + return builder; +} diff --git a/src/app/movie/movie.component.spec.ts b/src/app/movie/movie.component.spec.ts index 55eb3c2..f9b0b3c 100644 --- a/src/app/movie/movie.component.spec.ts +++ b/src/app/movie/movie.component.spec.ts @@ -1,29 +1,98 @@ -import { async, ComponentFixture, TestBed } from '@angular/core/testing'; -import { HttpClientTestingModule } from "@angular/common/http/testing"; +import { + async, + ComponentFixture, + TestBed, + getTestBed, + fakeAsync, + tick, +} from "@angular/core/testing"; +import { + HttpClientTestingModule, + HttpTestingController, +} from "@angular/common/http/testing"; +import { HttpParams } from "@angular/common/http"; -import { MovieComponent } from './movie.component'; -import { MovieService } from "../movie/services/movie.services"; +import { MovieComponent } from "./movie.component"; +import { MovieService } from "./services/movie.services"; +import { API_CONIG } from ".././shared/constants"; -describe('MovieComponent', () => { +describe("MovieComponent", () => { let component: MovieComponent; let fixture: ComponentFixture; + let injector; + let movieService: { + fetchFaults: () => { + (): any; + new (): any; + subscribe: { (arg0: (_: any) => Promise): void; new (): any }; + }; + }; + let httpMock: HttpTestingController; beforeEach(async(() => { + const a = setup().default(); TestBed.configureTestingModule({ imports: [HttpClientTestingModule], - declarations: [ MovieComponent ], - providers: [MovieService] - }) - .compileComponents(); + declarations: [MovieComponent], + providers: [MovieService], + }).compileComponents(); + + injector = getTestBed(); + movieService = injector.get(MovieService); + httpMock = injector.get(HttpTestingController); })); + afterEach(() => { + httpMock.verify(); + }); + beforeEach(() => { fixture = TestBed.createComponent(MovieComponent); component = fixture.componentInstance; fixture.detectChanges(); }); - it('should create', () => { + it("should create", () => { expect(component).toBeTruthy(); }); + + it("when ngOnInit is called it should", () => { + const { build } = setup().default(); + const c = build(); + c.ngOnInit(); + }); + + it("should get the faults", fakeAsync(() => { + const results = { data: [] }; + const moviesEndpoint = `${API_CONIG.API_ENDPOINT}?apikey=${API_CONIG.API_KEY}`; + const dummyHosts = [ + {_items: 'Hosts list'} + ]; + const dummyParams = new HttpParams() + .append('where', JSON.stringify({'_is_template': false})) + .append('max_results', JSON.stringify(25)); + + const req = httpMock.expectOne( + (req) => req.method === "GET" && req.url === moviesEndpoint + ); + expect(req.request.params.get("where")).toEqual('{"_is_template":false}'); + req.flush(dummyHosts); + })); + + }); + +function setup() { + const httpClientSpy = jasmine.createSpyObj("HttpClient", ["get"]); + const movieService = new MovieService(httpClientSpy); + const builder = { + movieService, + default() { + return builder; + }, + build() { + return new MovieComponent(movieService); + }, + }; + return builder; +} diff --git a/src/app/movie/movie.component.ts b/src/app/movie/movie.component.ts index 588f05e..5808f22 100644 --- a/src/app/movie/movie.component.ts +++ b/src/app/movie/movie.component.ts @@ -37,11 +37,10 @@ export class MovieComponent implements OnInit { } onNavMenuChange(movies: Movie[]) { - console.log("onNavMenuChange", movies); this.movies = movies; } - showLoading(loading: boolean) { + public showLoading(loading: boolean) { this.loading = loading; } diff --git a/src/app/nav-menu/nav-menu.component.html b/src/app/nav-menu/nav-menu.component.html index 695d305..47ce398 100644 --- a/src/app/nav-menu/nav-menu.component.html +++ b/src/app/nav-menu/nav-menu.component.html @@ -2,7 +2,7 @@