|
| 1 | +/****************************************** |
| 2 | +Unit tests for the Battle component. We need to test... |
| 3 | +
|
| 4 | + * template |
| 5 | + * correct appliance of the `winner` class depending on `Combatant` outputs |
| 6 | +
|
| 7 | +/*******************************************/ |
| 8 | + |
| 9 | +// --------------- Child component stubs ----------------- |
| 10 | + |
| 11 | +import { Component, Output, EventEmitter } from '@angular/core'; |
| 12 | + |
| 13 | +@Component({ |
| 14 | + selector: 'combatant', |
| 15 | + template: '' |
| 16 | +}) |
| 17 | +class FakeCombatant { |
| 18 | + @Output() stars = new EventEmitter<number>() |
| 19 | +} |
| 20 | + |
| 21 | +// --------------- Test config --------------- |
| 22 | + |
| 23 | +import { CommonModule } from '@angular/common'; |
| 24 | + |
| 25 | +import { BattleComponent } from '../components/battle'; |
| 26 | + |
| 27 | +const testModuleConfig = { |
| 28 | + imports: [CommonModule], |
| 29 | + declarations: [BattleComponent, FakeCombatant] |
| 30 | +} |
| 31 | + |
| 32 | +// --------------- Test suite --------------- |
| 33 | + |
| 34 | +import { TestBed, getTestBed, ComponentFixture } from '@angular/core/testing'; |
| 35 | +import { By } from '@angular/platform-browser'; |
| 36 | +import { expect } from 'chai'; |
| 37 | +import { DebugElement } from '@angular/core'; |
| 38 | + |
| 39 | +let fixture: ComponentFixture<BattleComponent> |
| 40 | +let instance: BattleComponent |
| 41 | +let debugElement: DebugElement |
| 42 | +let nativeElement: HTMLElement; |
| 43 | + |
| 44 | +describe('BattleComponent', () => { |
| 45 | + before(() => TestBed.configureTestingModule(testModuleConfig)); |
| 46 | + after(() => getTestBed().resetTestingModule()); |
| 47 | + |
| 48 | + beforeEach(() => { |
| 49 | + fixture = TestBed.createComponent(BattleComponent); |
| 50 | + debugElement = fixture.debugElement; |
| 51 | + instance = debugElement.componentInstance; |
| 52 | + nativeElement = debugElement.nativeElement; |
| 53 | + fixture.detectChanges(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should instantiate ok', () => { |
| 57 | + expect(instance).to.exist; |
| 58 | + }); |
| 59 | + |
| 60 | + it('should render two combatants', () => { |
| 61 | + expect(nativeElement.querySelectorAll('combatant').length).to.equal(2); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('the winner class', () => { |
| 65 | + let combatant1: FakeCombatant, combatant2: FakeCombatant, combatantElement1: HTMLElement, combatantElement2: HTMLElement; |
| 66 | + beforeEach(() => { |
| 67 | + [combatant1, combatant2] = debugElement.queryAll(By.css('combatant')).map(el => el.componentInstance); |
| 68 | + [combatantElement1, combatantElement2] = Array.from(nativeElement.querySelectorAll('combatant')); |
| 69 | + }); |
| 70 | + it('should not be initially applied', () => { |
| 71 | + expect(nativeElement.querySelector('.winner')).to.not.exist; |
| 72 | + }); |
| 73 | + it('should not be applied after we have just one count', () => { |
| 74 | + combatant1.stars.emit(456); |
| 75 | + expect(nativeElement.querySelector('.winner')).to.not.exist; |
| 76 | + }); |
| 77 | + it('should be applied to combatant1 when count exceeds combatant2', () => { |
| 78 | + combatant1.stars.emit(5); |
| 79 | + combatant2.stars.emit(4); |
| 80 | + fixture.detectChanges(); |
| 81 | + expect(combatantElement1.classList.contains('winner')).to.be.true; |
| 82 | + expect(combatantElement2.classList.contains('winner')).to.be.false; |
| 83 | + }); |
| 84 | + it('should be applied to combatant2 when count exceeds combatant1', () => { |
| 85 | + combatant1.stars.emit(1); |
| 86 | + combatant2.stars.emit(8); |
| 87 | + fixture.detectChanges(); |
| 88 | + expect(combatantElement1.classList.contains('winner')).to.be.false; |
| 89 | + expect(combatantElement2.classList.contains('winner')).to.be.true; |
| 90 | + }); |
| 91 | + it('should not be applied when counts are equal', () => { |
| 92 | + combatant1.stars.emit(7); |
| 93 | + combatant2.stars.emit(7); |
| 94 | + expect(nativeElement.querySelector('.winner')).to.not.exist; |
| 95 | + }); |
| 96 | + it('should be removed again if opponent becomes null', () => { |
| 97 | + combatant1.stars.emit(5); |
| 98 | + combatant2.stars.emit(4); |
| 99 | + combatant2.stars.emit(null); |
| 100 | + fixture.detectChanges(); |
| 101 | + expect(nativeElement.querySelector('.winner')).to.not.exist; |
| 102 | + }); |
| 103 | + it('should move if loser gets more', () => { |
| 104 | + combatant1.stars.emit(5); |
| 105 | + combatant2.stars.emit(4); |
| 106 | + combatant2.stars.emit(6); |
| 107 | + fixture.detectChanges(); |
| 108 | + expect(combatantElement1.classList.contains('winner')).to.be.false; |
| 109 | + expect(combatantElement2.classList.contains('winner')).to.be.true; |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | +}); |
0 commit comments