Skip to content

Commit

Permalink
test: add tests for the useApplication hook
Browse files Browse the repository at this point in the history
  • Loading branch information
trezy committed Sep 2, 2024
1 parent 5aa7ccc commit 93da609
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
67 changes: 67 additions & 0 deletions test/e2e/hooks/useApplication.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import type { ReactNode } from 'react'
import {
describe,
expect,
it,
} from 'vitest';
import {
render,
renderHook,
} from '@testing-library/react'
import { Application as PixiApplication } from 'pixi.js';

import { Application } from '../../../src/components/Application.ts'
import { useApplication } from '../../../src/hooks/useApplication.ts'

describe('useApplication', () =>
{
it('returns the nearest application', async () =>
{
let initApp: PixiApplication | null = null
let testApp: PixiApplication | null = null

const TestComponentWrapper = (props: {
children?: ReactNode,
}) => {
const { children } = props

const handleInit = (app: PixiApplication) => (initApp = app)

return (
<Application onInit={handleInit}>
{children}
</Application>
)
}

const TestComponent = () => {
const { app } = useApplication()

if (app) {
testApp = app
}

return null
}

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

await new Promise<void>(resolve => {
let intervalID = setInterval(() => {
if (initApp) {
clearInterval(intervalID)
setTimeout(resolve, 10)
}
}, 10)
})

expect(testApp).to.equal(initApp)
})

it('throws when not in a React Pixi tree', () =>
{
expect(() => renderHook(() => useApplication())).to.throw(Error, /no context found/i)
});
});
2 changes: 1 addition & 1 deletion vitest.workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ export default defineWorkspace([
'./vitest.setup.ts'
],
},
}
},
]);

0 comments on commit 93da609

Please sign in to comment.