Skip to content

Commit

Permalink
Working on the tests for the Fight component
Browse files Browse the repository at this point in the history
  • Loading branch information
dhAlcojor committed Aug 19, 2024
1 parent e4bda30 commit 8befa1c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/app/fight/fight.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { FightComponent } from './fight.component'
import { provideRouter } from '@angular/router'
import { provideHttpClient } from '@angular/common/http'
import { provideHttpClientTesting } from '@angular/common/http/testing'
import { waitFor } from '../utils'

describe('FightComponent', () => {
let component: FightComponent
Expand Down Expand Up @@ -41,4 +42,28 @@ describe('FightComponent', () => {
expect(leftHealthBar.textContent).toContain('/ 100')
expect(rightHealthBar.textContent).toContain('/ 100')
})

it('should reset the game', async () => {
component.leftCharacterInitialHealth = 100
component.rightCharacterInitialHealth = 100
fixture.detectChanges()

try {
const resetButton = await waitFor('#resetButton')
resetButton.click()
fixture.detectChanges()
} catch (e) {
console.error(e)
}

const leftHealthBar = fightEl.querySelector(
'.left-health-bar',
) as HTMLElement
const rightHealthBar = fightEl.querySelector(
'.right-health-bar',
) as HTMLElement

expect(leftHealthBar.textContent).toContain('/ 100')
expect(rightHealthBar.textContent).toContain('/ 100')
}, 30000)
})
19 changes: 19 additions & 0 deletions src/app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,22 @@ export function getRandomFromRange(min: number, max: number): number {
}
return Math.floor(Math.random() * (max - min + 1)) + min
}

export function waitFor(
selector: string,
timeout: number = 30000,
): Promise<HTMLElement> {
const start = Date.now()
return new Promise((resolve, reject) => {
const interval = setInterval(() => {
const el = document.querySelector(selector) as HTMLElement
if (el) {
clearInterval(interval)
resolve(el)
} else if (Date.now() - start > timeout) {
clearInterval(interval)
reject(new Error('Timeout exceeded'))
}
}, 100)
})
}

0 comments on commit 8befa1c

Please sign in to comment.