Skip to content

Commit

Permalink
test: add e2e test framework
Browse files Browse the repository at this point in the history
  • Loading branch information
trezy committed Sep 2, 2024
1 parent 6eb39be commit 1a63350
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/hooks/useTick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function useTick<T>(
{
callback = options.callback;
context = options.context;
isEnabled = options.isEnabled;
isEnabled = options.isEnabled ?? true;
priority = options.priority;
}

Expand Down
2 changes: 1 addition & 1 deletion src/typedefs/UseTickOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface UseTickOptions<T>
context?: T

/** @description Whether this callback is currently enabled. */
isEnabled: true,
isEnabled?: true,

/** @description The priority of this callback compared to other callbacks on the ticker. */
priority?: number
Expand Down
61 changes: 61 additions & 0 deletions test/e2e/hooks/useTick.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {
describe,
expect,
it,
vi,
} from 'vitest';
import {
render,
renderHook,
} from '@testing-library/react'
import { Ticker } from 'pixi.js';

import { Application } from '../../../src/components/Application'
import { useTick } from '../../../src/hooks/useTick'
import { wait } from '../../utils/wait'

describe('useTick', () =>
{
describe('with a function', () => {
it('runs the callback', async () => {
const useTickSpy = vi.fn()

const TestComponent = () => {
useTick(useTickSpy)

return null
}

render(<TestComponent />, { wrapper: Application })

await wait(100)

expect(useTickSpy.mock.lastCall?.[0]).to.be.instanceOf(Ticker)
});
})

describe('with an options hash', () => {
it('runs the callback', async () => {
const useTickSpy = vi.fn()

const TestComponent = () => {
useTick({ callback: useTickSpy })

return null
}

render(<TestComponent />, { wrapper: Application })

await wait(100)

expect(useTickSpy.mock.lastCall?.[0]).to.be.instanceOf(Ticker)
});
})

it('throws when not in a React Pixi tree', () =>
{
const result = () => renderHook(() => useTick(() => {}))

expect(result).to.throw(Error, /no context found/i)
});
});
7 changes: 7 additions & 0 deletions test/utils/wait.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function wait(waitMS: number, rejectOnComplete: boolean = false)
{
return new Promise((resolve, reject) =>
{
setTimeout(rejectOnComplete ? reject : resolve, waitMS);
});
}

0 comments on commit 1a63350

Please sign in to comment.