|
| 1 | +/****************************************** |
| 2 | +Unit tests for the Combatant component. We need to test... |
| 3 | +
|
| 4 | + * template |
| 5 | + * stars output |
| 6 | + * combatantdetail child input |
| 7 | + * battleservice usage |
| 8 | +
|
| 9 | +/*******************************************/ |
| 10 | + |
| 11 | +// --------------- Child component stubs ----------------- |
| 12 | + |
| 13 | +import { Component, Input } from '@angular/core'; |
| 14 | + |
| 15 | +@Component({ |
| 16 | + selector: 'combatantdetail', |
| 17 | + template: '' |
| 18 | +}) |
| 19 | +class FakeCombatantDetail { |
| 20 | + @Input() data: CombatantInfo |
| 21 | +} |
| 22 | + |
| 23 | +// --------------- Service mocks --------------- |
| 24 | + |
| 25 | +import * as sinon from 'sinon'; |
| 26 | + |
| 27 | +const fakeBattleServiceObservable = { |
| 28 | + subscribe: sinon.stub() |
| 29 | +}; |
| 30 | + |
| 31 | +const fakeBattleService = { |
| 32 | + battleInfoForUser: sinon.stub().returns(fakeBattleServiceObservable) |
| 33 | +}; |
| 34 | + |
| 35 | +// --------------- Test config --------------- |
| 36 | + |
| 37 | +import { FormsModule } from '@angular/forms'; |
| 38 | +import { CommonModule } from '@angular/common'; |
| 39 | + |
| 40 | +import { CombatantComponent } from '../components/combatant'; |
| 41 | +import { BattleService } from '../services/battleservice'; |
| 42 | + |
| 43 | +const testModuleConfig = { |
| 44 | + imports: [FormsModule, CommonModule], |
| 45 | + declarations: [CombatantComponent, FakeCombatantDetail], |
| 46 | + providers: [{provide: BattleService, useValue: fakeBattleService}] |
| 47 | +} |
| 48 | + |
| 49 | +// --------------- Test suite --------------- |
| 50 | + |
| 51 | +import { TestBed, getTestBed, ComponentFixture } from '@angular/core/testing'; |
| 52 | +import { By } from '@angular/platform-browser'; |
| 53 | +import { expect } from 'chai'; |
| 54 | +import { DebugElement } from '@angular/core'; |
| 55 | +import { CombatantInfo, CombatantRepoInfo } from '../types'; |
| 56 | + |
| 57 | +let fixture: ComponentFixture<CombatantComponent> |
| 58 | +let instance: CombatantComponent |
| 59 | +let debugElement: DebugElement |
| 60 | +let nativeElement: HTMLElement; |
| 61 | +let starsListener = sinon.stub(); |
| 62 | + |
| 63 | +describe('CombatantComponent', () => { |
| 64 | + before(() => TestBed.configureTestingModule(testModuleConfig)); |
| 65 | + after(() => getTestBed().resetTestingModule()); |
| 66 | + |
| 67 | + beforeEach(() => { |
| 68 | + sinon.resetHistory(); |
| 69 | + fixture = TestBed.createComponent(CombatantComponent); |
| 70 | + debugElement = fixture.debugElement; |
| 71 | + instance = debugElement.componentInstance; |
| 72 | + instance.stars.subscribe(starsListener); // To be able to test the stars output |
| 73 | + nativeElement = debugElement.nativeElement; |
| 74 | + fixture.detectChanges(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should instantiate ok', () => { |
| 78 | + expect(instance).to.exist; |
| 79 | + }); |
| 80 | + |
| 81 | + it('should start with no indicators, no details and a disabled button', () => { |
| 82 | + expect(nativeElement.querySelector('.qa-load-indicator')).to.not.exist; |
| 83 | + expect(nativeElement.querySelector('.qa-error-indicator')).to.not.exist; |
| 84 | + expect(nativeElement.querySelector('combatantdetail')).to.not.exist; |
| 85 | + expect(nativeElement.querySelector('.qa-load-button[disabled]')).to.exist; |
| 86 | + }); |
| 87 | + |
| 88 | + it('should not call service if we click button when field is empty', () => { |
| 89 | + nativeElement.querySelector(".qa-load-button").dispatchEvent(new Event('click')); |
| 90 | + fixture.detectChanges(); |
| 91 | + |
| 92 | + expect(fakeBattleService.battleInfoForUser.called).to.be.false; |
| 93 | + }); |
| 94 | + |
| 95 | + it('should not emit anything to stars output', () => { |
| 96 | + expect(starsListener.called).to.be.false; |
| 97 | + }); |
| 98 | + |
| 99 | + describe('calling the service', () => { |
| 100 | + const fieldContent = Math.random.toString(); |
| 101 | + |
| 102 | + beforeEach(() => { |
| 103 | + const field: HTMLInputElement = debugElement.query(By.css('.qa-github-input')).nativeElement; |
| 104 | + field.value = fieldContent; |
| 105 | + field.dispatchEvent(new Event('input')); |
| 106 | + fixture.detectChanges(); |
| 107 | + nativeElement.querySelector(".qa-load-button").dispatchEvent(new Event('click')); |
| 108 | + fixture.detectChanges(); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should call service with form content when button clicked', ()=> { |
| 112 | + expect(fakeBattleService.battleInfoForUser.called).to.be.true; |
| 113 | + expect(fakeBattleService.battleInfoForUser.firstCall.args[0]).to.equal(fieldContent); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should subscribe to success and fail for the provided observable', () => { |
| 117 | + expect(fakeBattleServiceObservable.subscribe.called).to.be.true; |
| 118 | + expect(fakeBattleServiceObservable.subscribe.firstCall.args[0]).to.be.a('function'); |
| 119 | + expect(fakeBattleServiceObservable.subscribe.firstCall.args[1]).to.be.a('function'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should show a loading indicator', ()=> { |
| 123 | + expect(nativeElement.querySelector('.qa-load-indicator')).to.exist; |
| 124 | + }); |
| 125 | + |
| 126 | + it('should disable the button again', ()=> { |
| 127 | + expect(nativeElement.querySelector('.qa-load-button[disabled]')).to.exist; |
| 128 | + }); |
| 129 | + |
| 130 | + it('should emit null to stars output', ()=> { |
| 131 | + expect(starsListener.callCount).to.equal(1); |
| 132 | + expect(starsListener.firstCall.args[0]).to.equal(null); |
| 133 | + }); |
| 134 | + |
| 135 | + describe('success', () => { |
| 136 | + type RecursivePartial<T> = { [P in keyof T]?: RecursivePartial<T[P]>; }; // https://stackoverflow.com/a/47914631 |
| 137 | + const fakeReply: RecursivePartial<CombatantInfo> = { |
| 138 | + repos: { |
| 139 | + stars: Math.ceil(Math.random() * 1000) |
| 140 | + } |
| 141 | + }; |
| 142 | + |
| 143 | + beforeEach(() => { |
| 144 | + const serviceSuccessCallback = fakeBattleServiceObservable.subscribe.firstCall.args[0]; |
| 145 | + serviceSuccessCallback(fakeReply); |
| 146 | + fixture.detectChanges(); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should emit the received count to stars output', () => { |
| 150 | + expect(starsListener.callCount).to.equal(2); // first was when loading, second is the success |
| 151 | + expect(starsListener.lastCall.args[0]).to.equal(fakeReply.repos.stars); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should clear the contents of the field', () => { |
| 155 | + expect(debugElement.query(By.css('.qa-github-input')).nativeElement.value).to.equal(''); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should stop showing a loading indicator', () => { |
| 159 | + expect(nativeElement.querySelector('.qa-load-indicator')).to.not.exist; |
| 160 | + }); |
| 161 | + |
| 162 | + it('should send data to combatantdetail child', () => { |
| 163 | + expect(nativeElement.querySelector('combatantdetail')).to.exist; |
| 164 | + const detailInstance: FakeCombatantDetail = debugElement.query(By.css('combatantdetail')).componentInstance; |
| 165 | + expect(detailInstance.data).to.equal(fakeReply); |
| 166 | + }); |
| 167 | + }); |
| 168 | + |
| 169 | + describe('fail', () => { |
| 170 | + const fakeError = new Error('KABOOM'); |
| 171 | + |
| 172 | + beforeEach(() => { |
| 173 | + const serviceFailCallback = fakeBattleServiceObservable.subscribe.firstCall.args[1]; |
| 174 | + serviceFailCallback(fakeError); |
| 175 | + fixture.detectChanges(); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should show an error indicator instead of the loading indicator', () => { |
| 179 | + expect(nativeElement.querySelector('.qa-load-indicator')).to.not.exist; |
| 180 | + expect(nativeElement.querySelector('.qa-error-indicator')).to.exist; |
| 181 | + }); |
| 182 | + |
| 183 | + it('should emit null to stars output', ()=> { |
| 184 | + expect(starsListener.callCount).to.equal(2); // first was when loading, second is the fail |
| 185 | + expect(starsListener.lastCall.args[0]).to.equal(null); |
| 186 | + }); |
| 187 | + }); |
| 188 | + }); |
| 189 | +}); |
0 commit comments