From 988d60788c139a6d77f044bad15f20d427bb0ab9 Mon Sep 17 00:00:00 2001 From: Joe Date: Tue, 30 Jan 2024 15:26:58 -0700 Subject: [PATCH] reset to initial state --- .../hero-detail/hero-detail.component.spec.ts | 60 ---------- src/app/hero.service.spec.ts | 37 ------ src/app/hero/hero.component.shallow.spec.ts | 32 ------ src/app/heroes/heroes.component.deep.spec.ts | 108 ------------------ .../heroes/heroes.component.shallow.spec.ts | 57 --------- src/app/heroes/heroes.component.spec.ts | 41 ------- src/app/message.service.spec.ts | 33 ------ src/app/strength/strength.pipe.spec.ts | 15 --- 8 files changed, 383 deletions(-) delete mode 100644 src/app/hero-detail/hero-detail.component.spec.ts delete mode 100644 src/app/hero.service.spec.ts delete mode 100644 src/app/hero/hero.component.shallow.spec.ts delete mode 100644 src/app/heroes/heroes.component.deep.spec.ts delete mode 100644 src/app/heroes/heroes.component.shallow.spec.ts delete mode 100644 src/app/heroes/heroes.component.spec.ts delete mode 100644 src/app/message.service.spec.ts delete mode 100644 src/app/strength/strength.pipe.spec.ts diff --git a/src/app/hero-detail/hero-detail.component.spec.ts b/src/app/hero-detail/hero-detail.component.spec.ts deleted file mode 100644 index 4953ebed..00000000 --- a/src/app/hero-detail/hero-detail.component.spec.ts +++ /dev/null @@ -1,60 +0,0 @@ -import { TestBed, ComponentFixture, fakeAsync, tick, flush, async } from "@angular/core/testing"; -import { HeroDetailComponent } from "./hero-detail.component"; -import { ActivatedRoute } from "@angular/router"; -import { HeroService } from "../hero.service"; -import { Location } from '@angular/common'; -import { of } from "rxjs"; -import { FormsModule } from "@angular/forms"; - -describe('HeroDetailComponent', () => { - let fixture: ComponentFixture; - let mockActivatedRoute, mockHeroService, mockLocation; - - beforeEach(() => { - mockActivatedRoute = { - snapshot: { paramMap: { get: () => { return '3'; }}} - } - mockHeroService = jasmine.createSpyObj(['getHero', 'updateHero']); - mockLocation = jasmine.createSpyObj(['back']); - - TestBed.configureTestingModule({ - imports: [FormsModule], - declarations: [HeroDetailComponent], - providers: [ - {provide: ActivatedRoute, useValue: mockActivatedRoute}, - {provide: HeroService, useValue: mockHeroService}, - {provide: Location, useValue: mockLocation}, - ] - }); - fixture = TestBed.createComponent(HeroDetailComponent); - - mockHeroService.getHero.and.returnValue(of({id: 3, name: 'SuperDude', strength: 100})); - }); - - it('should render hero name in a h2 tag', () => { - fixture.detectChanges(); - - expect(fixture.nativeElement.querySelector('h2').textContent).toContain('SUPERDUDE'); - }); - - // it('should call updateHero when save is called', fakeAsync(() => { - // mockHeroService.updateHero.and.returnValue(of({})); - // fixture.detectChanges(); - - // fixture.componentInstance.save(); - // flush(); - - // expect(mockHeroService.updateHero).toHaveBeenCalled(); - // })) - - // it('should call updateHero when save is called', async(() => { - // mockHeroService.updateHero.and.returnValue(of({})); - // fixture.detectChanges(); - - // fixture.componentInstance.save(); - - // fixture.whenStable().then(() => { - // expect(mockHeroService.updateHero).toHaveBeenCalled(); - // }); - // })) -}) \ No newline at end of file diff --git a/src/app/hero.service.spec.ts b/src/app/hero.service.spec.ts deleted file mode 100644 index ad616c9b..00000000 --- a/src/app/hero.service.spec.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { TestBed, inject } from "@angular/core/testing"; -import { HeroService } from "./hero.service"; -import { MessageService } from "./message.service"; -import { HttpClientTestingModule, HttpTestingController } from "@angular/common/http/testing"; - -describe('HeroService', () => { - let mockMessageService; - let httpTestingController: HttpTestingController; - let service: HeroService; - - beforeEach(() => { - mockMessageService = jasmine.createSpyObj(['add']); - - TestBed.configureTestingModule({ - imports: [ HttpClientTestingModule ], - providers: [ - HeroService, - {provide: MessageService, useValue: mockMessageService} - ] - }); - - httpTestingController = TestBed.get(HttpTestingController); - service = TestBed.get(HeroService); - }); - - describe('getHero', () => { - - it('should call get with the correct URL', () => { - - service.getHero(4).subscribe(); - - const req = httpTestingController.expectOne('api/heroes/4'); - req.flush({id: 4, name: 'SuperDude', strength: 100}); - httpTestingController.verify(); - }); - }) -}) \ No newline at end of file diff --git a/src/app/hero/hero.component.shallow.spec.ts b/src/app/hero/hero.component.shallow.spec.ts deleted file mode 100644 index a7106540..00000000 --- a/src/app/hero/hero.component.shallow.spec.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { TestBed, ComponentFixture } from "@angular/core/testing"; -import { HeroComponent } from "./hero.component"; -import { NO_ERRORS_SCHEMA } from "@angular/core"; -import { By } from "@angular/platform-browser"; - -describe('HeroComponent (shallow tests)', () => { - let fixture: ComponentFixture; - - beforeEach(() => { - TestBed.configureTestingModule({ - declarations: [HeroComponent], - schemas: [NO_ERRORS_SCHEMA] - }); - fixture = TestBed.createComponent(HeroComponent); - }); - - it('should have the correct hero', () => { - fixture.componentInstance.hero = { id: 1, name: 'SuperDude', strength: 3}; - - expect(fixture.componentInstance.hero.name).toEqual('SuperDude'); - }); - - it('should render the hero name in an anchor tag', () => { - fixture.componentInstance.hero = { id: 1, name: 'SuperDude', strength: 3}; - fixture.detectChanges(); - - let deA = fixture.debugElement.query(By.css('a')); - expect(deA.nativeElement.textContent).toContain('SuperDude'); - - // expect(fixture.nativeElement.querySelector('a').textContent).toContain('SuperDude'); - }) -}) \ No newline at end of file diff --git a/src/app/heroes/heroes.component.deep.spec.ts b/src/app/heroes/heroes.component.deep.spec.ts deleted file mode 100644 index 536f2d36..00000000 --- a/src/app/heroes/heroes.component.deep.spec.ts +++ /dev/null @@ -1,108 +0,0 @@ -import { ComponentFixture, TestBed } from "@angular/core/testing"; -import { HeroesComponent } from "./heroes.component"; -import { NO_ERRORS_SCHEMA, Component, Input, Directive } from "@angular/core"; -import { HeroService } from "../hero.service"; -import { of } from "rxjs"; -import { Hero } from "../hero"; -import { By } from "@angular/platform-browser"; -import { HeroComponent } from "../hero/hero.component"; - -@Directive({ - selector: '[routerLink]', - host: { '(click)': 'onClick()' } -}) -export class RouterLinkDirectiveStub { - @Input('routerLink') linkParams: any; - navigatedTo: any = null; - - onClick() { - this.navigatedTo = this.linkParams; - } -} - -describe('HeroesComponent (deep tests)', () => { - let fixture: ComponentFixture; - let mockHeroService; - let HEROES; - - beforeEach(() => { - HEROES = [ - {id:1, name: 'SpiderDude', strength: 8}, - {id:2, name: 'Wonderful Woman', strength: 24}, - {id:3, name: 'SuperDude', strength: 55} - ] - mockHeroService = jasmine.createSpyObj(['getHeroes', 'addHero', 'deleteHero']); - - TestBed.configureTestingModule({ - declarations: [ - HeroesComponent, - HeroComponent, - RouterLinkDirectiveStub - ], - providers: [ - { provide: HeroService, useValue: mockHeroService } - ], - // schemas: [NO_ERRORS_SCHEMA] - }) - - fixture = TestBed.createComponent(HeroesComponent); - }); - - it('should render each hero as a HeroComponent', () => { - mockHeroService.getHeroes.and.returnValue(of(HEROES)); - - // run ngOnInit - fixture.detectChanges(); - - const heroComponentDEs = fixture.debugElement.queryAll(By.directive(HeroComponent)); - expect(heroComponentDEs.length).toEqual(3); - for(let i=0; i < heroComponentDEs.length; i++) { - expect(heroComponentDEs[i].componentInstance.hero).toEqual(HEROES[i]); - } - }); - - it(`should call heroService.deleteHero when the Hero Component's - delete button is clicked`, () => { - spyOn(fixture.componentInstance, 'delete'); - mockHeroService.getHeroes.and.returnValue(of(HEROES)); - - fixture.detectChanges(); - - const heroComponents = fixture.debugElement.queryAll(By.directive(HeroComponent)); - // (heroComponents[0].componentInstance).delete.emit(undefined); - heroComponents[0].triggerEventHandler('delete', null); - - expect(fixture.componentInstance.delete).toHaveBeenCalledWith(HEROES[0]); - }); - - it('should add a new hero to the hero list when the add button is clicked', () => { - mockHeroService.getHeroes.and.returnValue(of(HEROES)); - fixture.detectChanges(); - const name = "Mr. Ice"; - mockHeroService.addHero.and.returnValue(of({id: 5, name: name, strength: 4})); - const inputElement = fixture.debugElement.query(By.css('input')).nativeElement; - const addButton = fixture.debugElement.queryAll(By.css('button'))[0]; - - inputElement.value = name; - addButton.triggerEventHandler('click', null); - fixture.detectChanges(); - - const heroText = fixture.debugElement.query(By.css('ul')).nativeElement.textContent; - expect(heroText).toContain(name); - }); - - it('should have the correct route for the first hero', () => { - mockHeroService.getHeroes.and.returnValue(of(HEROES)); - fixture.detectChanges(); - const heroComponents = fixture.debugElement.queryAll(By.directive(HeroComponent)); - - let routerLink = heroComponents[0] - .query(By.directive(RouterLinkDirectiveStub)) - .injector.get(RouterLinkDirectiveStub); - - heroComponents[0].query(By.css('a')).triggerEventHandler('click', null); - - expect(routerLink.navigatedTo).toBe('/detail/1'); - }) - -}); \ No newline at end of file diff --git a/src/app/heroes/heroes.component.shallow.spec.ts b/src/app/heroes/heroes.component.shallow.spec.ts deleted file mode 100644 index 9cb37248..00000000 --- a/src/app/heroes/heroes.component.shallow.spec.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { ComponentFixture, TestBed } from "@angular/core/testing"; -import { HeroesComponent } from "./heroes.component"; -import { NO_ERRORS_SCHEMA, Component, Input } from "@angular/core"; -import { HeroService } from "../hero.service"; -import { of } from "rxjs"; -import { Hero } from "../hero"; -import { By } from "@angular/platform-browser"; - -describe('HeroesComponent (shallow tests)', () => { - let fixture: ComponentFixture; - let mockHeroService; - let HEROES; - - @Component({ - selector: 'app-hero', - template: '
', - }) - class FakeHeroComponent { - @Input() hero: Hero; - // @Output() delete = new EventEmitter(); - } - - beforeEach(() => { - HEROES = [ - {id:1, name: 'SpiderDude', strength: 8}, - {id:2, name: 'Wonderful Woman', strength: 24}, - {id:3, name: 'SuperDude', strength: 55} - ] - mockHeroService = jasmine.createSpyObj(['getHeroes', 'addHero', 'deleteHero']); - - TestBed.configureTestingModule({ - declarations: [ - HeroesComponent, - FakeHeroComponent - ], - providers: [ - { provide: HeroService, useValue: mockHeroService } - ], - // schemas: [NO_ERRORS_SCHEMA] - }) - fixture = TestBed.createComponent(HeroesComponent); - }); - - it('should set heroes correctly from the service', () => { - mockHeroService.getHeroes.and.returnValue(of(HEROES)) - fixture.detectChanges(); - - expect(fixture.componentInstance.heroes.length).toBe(3); - }); - - it('should create one li for each hero', () => { - mockHeroService.getHeroes.and.returnValue(of(HEROES)) - fixture.detectChanges(); - - expect(fixture.debugElement.queryAll(By.css('li')).length).toBe(3); - }) -}) \ No newline at end of file diff --git a/src/app/heroes/heroes.component.spec.ts b/src/app/heroes/heroes.component.spec.ts deleted file mode 100644 index adaa0d4d..00000000 --- a/src/app/heroes/heroes.component.spec.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { HeroesComponent } from "./heroes.component"; -import { of } from "rxjs"; - -describe('HeroesComponent', () => { - let component: HeroesComponent; - let HEROES; - let mockHeroService; - - beforeEach(() => { - HEROES = [ - {id:1, name: 'SpiderDude', strength: 8}, - {id:2, name: 'Wonderful Woman', strength: 24}, - {id:3, name: 'SuperDude', strength: 55} - ] - - mockHeroService = jasmine.createSpyObj(['getHeroes', 'addHero', 'deleteHero']) - - component = new HeroesComponent(mockHeroService); - }) - - describe('delete', () => { - - it('should remove the indicated hero from the heroes list', () => { - mockHeroService.deleteHero.and.returnValue(of(true)) - component.heroes = HEROES; - - component.delete(HEROES[2]); - - expect(component.heroes.length).toBe(2); - }) - - it('should call deleteHero', () => { - mockHeroService.deleteHero.and.returnValue(of(true)) - component.heroes = HEROES; - - component.delete(HEROES[2]); - - expect(mockHeroService.deleteHero).toHaveBeenCalledWith(HEROES[2]); - }) - }) -}) \ No newline at end of file diff --git a/src/app/message.service.spec.ts b/src/app/message.service.spec.ts deleted file mode 100644 index 7803b57d..00000000 --- a/src/app/message.service.spec.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { MessageService } from "./message.service"; - -describe('MessageService', () => { - let service: MessageService - - beforeEach(() => { - - }) - - it('should have no messages to start', () => { - service = new MessageService(); - - expect(service.messages.length).toBe(0); - }) - - it('should add a message when add is called', () => { - service = new MessageService(); - - service.add('message1'); - - expect(service.messages.length).toBe(1); - }) - - it('should remove all messages when clear is called', () => { - service = new MessageService(); - service.add('message1'); - - service.clear(); - - expect(service.messages.length).toBe(0); - }) - -}) \ No newline at end of file diff --git a/src/app/strength/strength.pipe.spec.ts b/src/app/strength/strength.pipe.spec.ts deleted file mode 100644 index 241a1f26..00000000 --- a/src/app/strength/strength.pipe.spec.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { StrengthPipe } from "./strength.pipe"; - -describe('StrengthPipe', () => { - it('should display weak if strength is 5', () => { - let pipe = new StrengthPipe(); - - expect(pipe.transform(5)).toEqual('5 (weak)'); - }) - - it('should display strong if strength is 10', () => { - let pipe = new StrengthPipe(); - - expect(pipe.transform(10)).toEqual('10 (strong)'); - }) -}) \ No newline at end of file