diff --git a/test/useRive.test.tsx b/test/useRive.test.tsx index 6c1917c..c1d80a1 100644 --- a/test/useRive.test.tsx +++ b/test/useRive.test.tsx @@ -4,7 +4,7 @@ import { renderHook, act } from '@testing-library/react-hooks'; import useRive from '../src/hooks/useRive'; import * as rive from '@rive-app/canvas'; -import { render } from '@testing-library/react'; +import { render, waitFor } from '@testing-library/react'; jest.mock('@rive-app/canvas', () => ({ Rive: jest.fn().mockImplementation(() => ({ @@ -65,8 +65,10 @@ describe('useRive', () => { await act(async () => { result.current.setCanvasRef(canvasSpy); - controlledRiveloadCb(); }); + await waitFor(() => { + controlledRiveloadCb(); + }) expect(result.current.rive).toBe(baseRiveMock); expect(result.current.canvas).toBe(canvasSpy); }); @@ -93,7 +95,11 @@ describe('useRive', () => { await act(async () => { result.current.setCanvasRef(canvasSpy); result.current.setContainerRef(containerSpy); + }); + await waitFor(() => { controlledRiveloadCb(); + }) + await act(async () => { jest.spyOn(containerSpy, 'clientWidth', 'get').mockReturnValue(500); jest.spyOn(containerSpy, 'clientHeight', 'get').mockReturnValue(500); containerSpy.dispatchEvent(new Event('resize')); @@ -125,8 +131,10 @@ describe('useRive', () => { await act(async () => { result.current.setCanvasRef(canvasSpy); - controlledRiveloadCb(); }); + await waitFor(() => { + controlledRiveloadCb(); + }) unmount(); @@ -153,6 +161,8 @@ describe('useRive', () => { await act(async () => { result.current.setCanvasRef(canvasSpy); result.current.setContainerRef(containerSpy); + }); + await waitFor(() => { controlledRiveloadCb(); }); @@ -188,6 +198,8 @@ describe('useRive', () => { await act(async () => { result.current.setCanvasRef(canvasSpy); result.current.setContainerRef(containerSpy); + }); + await waitFor(() => { controlledRiveloadCb(); }); @@ -222,6 +234,8 @@ describe('useRive', () => { await act(async () => { result.current.setCanvasRef(canvasSpy); result.current.setContainerRef(containerSpy); + }); + await waitFor(() => { controlledRiveloadCb(); }); @@ -260,6 +274,8 @@ describe('useRive', () => { await act(async () => { result.current.setContainerRef(containerSpy); result.current.setCanvasRef(canvasSpy); + }); + await waitFor(() => { controlledRiveloadCb(); }); @@ -300,6 +316,8 @@ describe('useRive', () => { await act(async () => { result.current.setCanvasRef(canvasSpy); + }); + await waitFor(() => { controlledRiveloadCb(); }); @@ -336,6 +354,8 @@ describe('useRive', () => { await act(async () => { result.current.setCanvasRef(canvasSpy); + }); + await waitFor(() => { controlledRiveloadCb(); }); @@ -379,6 +399,8 @@ describe('useRive', () => { await act(async () => { result.current.setCanvasRef(canvasSpy); + }); + await waitFor(() => { controlledRiveloadCb(); }); @@ -428,6 +450,8 @@ describe('useRive', () => { await act(async () => { result.current.setCanvasRef(canvasSpy); + }); + await waitFor(() => { controlledRiveloadCb(); }); @@ -456,6 +480,8 @@ describe('useRive', () => { await act(async () => { result.current.setCanvasRef(canvasSpy); result.current.setContainerRef(containerSpy); + }); + await waitFor(() => { controlledRiveloadCb(); }); @@ -463,7 +489,7 @@ describe('useRive', () => { expect(canvasSpy).toHaveStyle('width: 100px'); }); - it('updates the canvas dimensions and size if there is a new canvas size calculation', async () => { + it.only('updates the canvas dimensions and size if there is a new canvas size calculation', async () => { const params = { src: 'file-src', }; @@ -483,12 +509,20 @@ describe('useRive', () => { await act(async () => { result.current.setCanvasRef(canvasSpy); result.current.setContainerRef(containerSpy); + }); + await waitFor(() => { controlledRiveloadCb(); - jest.spyOn(containerSpy, 'clientWidth', 'get').mockReturnValue(200); - jest.spyOn(containerSpy, 'clientHeight', 'get').mockReturnValue(200); - containerSpy.dispatchEvent(new Event('resize')); }); + await act(async () => { + jest.spyOn(containerSpy, 'clientWidth', 'get').mockReturnValue(200); + jest.spyOn(containerSpy, 'clientHeight', 'get').mockReturnValue(200); + console.log('PRE DISPATCHED'); + containerSpy.dispatchEvent(new Event('resize')); + console.log('DISPATCHED'); + }); + + console.log('EXPECTING'); expect(canvasSpy).toHaveAttribute('width', '400'); expect(canvasSpy).toHaveAttribute('height', '400'); }); @@ -516,7 +550,13 @@ describe('useRive', () => { await act(async () => { result.current.setCanvasRef(canvasSpy); result.current.setContainerRef(containerSpy); + }); + + await waitFor(() => { controlledRiveloadCb(); + }); + + await act(async () => { jest.spyOn(containerSpy, 'clientWidth', 'get').mockReturnValue(500); jest.spyOn(containerSpy, 'clientHeight', 'get').mockReturnValue(500); containerSpy.dispatchEvent(new Event('resize'));