-
-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for the
useApplication
hook
- Loading branch information
Showing
2 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,5 +24,5 @@ export default defineWorkspace([ | |
'./vitest.setup.ts' | ||
], | ||
}, | ||
} | ||
}, | ||
]); |