From b370e5a8a1c0cc7b852c3b9d629a84a6a09c22a6 Mon Sep 17 00:00:00 2001 From: Trezy Date: Tue, 3 Sep 2024 15:11:52 -0500 Subject: [PATCH] test: test `` unmounts --- test/e2e/components/Application.test.tsx | 90 +++++++++++++++++++----- vitest.setup.ts | 12 ++-- vitest.workspace.ts | 4 +- 3 files changed, 79 insertions(+), 27 deletions(-) diff --git a/test/e2e/components/Application.test.tsx b/test/e2e/components/Application.test.tsx index 672e3442..74345a8e 100644 --- a/test/e2e/components/Application.test.tsx +++ b/test/e2e/components/Application.test.tsx @@ -3,31 +3,83 @@ import { expect, it, vi, -} from 'vitest'; +} from 'vitest' +import { type Application as PixiApplication } from 'pixi.js' import { render } from '@testing-library/react' +import { useEffect } from 'react' import { Application } from '../../../src/components/Application' -import { wait } from '../../utils/wait'; +import { useApplication } from '../../../src/hooks/useApplication' -describe('Application', () => -{ - describe('onInit', () => { - it('runs the callback', async () => { - const onInitSpy = vi.fn() +describe('Application', () => { + it('runs the `onInit` callback', async () => { + const onInitSpy = vi.fn() - const TestComponent = function() { - return ( - - ) - } + const TestComponent = () => ( + + ) - render(( - - )) + render() - await wait(1000) + await expect.poll(() => onInitSpy.mock.calls.length).toEqual(1) + }); - expect(onInitSpy.mock.calls.length).to.equal(1) - }); + it('unmounts after init', async () => { + let testApp: PixiApplication | null = null + + const TestChildComponent = () => { + const { app } = useApplication() + + useEffect(() => { + testApp = app + + return () => { + testApp = app + } + }, [app]) + + return null + } + + const TestComponent = () => ( + + + + ) + + const { unmount } = render() + + await expect.poll(() => Boolean(testApp?.renderer)).toBeTruthy() + + unmount() + + await expect.poll(() => !testApp?.renderer).toBeFalsy() + await expect.poll(() => !testApp?.stage).toBeFalsy() }) -}); + + it('unmounts during init', async () => { + let testApp: PixiApplication | null = null + + const TestChildComponent = () => { + testApp = useApplication().app + + return null + } + + const TestComponent = () => ( + + + + ) + + const { unmount } = render() + + await expect.poll(() => !testApp?.renderer).toBeFalsy() + await expect.poll(() => !testApp?.stage).toBeFalsy() + + unmount() + + await expect.poll(() => !testApp?.renderer).toBeFalsy() + await expect.poll(() => !testApp?.stage).toBeFalsy() + }); +}) diff --git a/vitest.setup.ts b/vitest.setup.ts index b06f1468..f097bb90 100644 --- a/vitest.setup.ts +++ b/vitest.setup.ts @@ -1,8 +1,10 @@ import { afterEach } from 'vitest'; import '@testing-library/jest-dom/vitest'; -import { cleanup } from '@testing-library/react'; +import { + cleanup, + configure, +} from '@testing-library/react'; -afterEach(() => -{ - cleanup(); -}); +configure({ reactStrictMode: true }); + +afterEach(() => cleanup()); diff --git a/vitest.workspace.ts b/vitest.workspace.ts index 07b989c6..a8ca1632 100644 --- a/vitest.workspace.ts +++ b/vitest.workspace.ts @@ -20,9 +20,7 @@ export default defineWorkspace([ }, globals: true, include: ['test/e2e/**/*.test.ts(x)'], - setupFiles: [ - './vitest.setup.ts' - ], + setupFiles: ['./vitest.setup.ts'], }, }, ]);