Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
bodymovin committed May 28, 2024
1 parent 25276ad commit 45dacb6
Showing 1 changed file with 35 additions and 16 deletions.
51 changes: 35 additions & 16 deletions test/useRive.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ jest.mock('@rive-app/canvas', () => ({
describe('useRive', () => {
let controlledRiveloadCb: () => void;
let baseRiveMock: Partial<rive.Rive>;
const mockBoundingBox = {
width: 100,
height: 100,
x: 0,
top: 0,
y: 0,
bottom: 100,
left: 0,
right: 100,
toJSON: () => '',
};

beforeEach(() => {
baseRiveMock = {
Expand Down Expand Up @@ -162,8 +173,9 @@ describe('useRive', () => {

const canvasSpy = document.createElement('canvas');
const containerSpy = document.createElement('div');
jest.spyOn(containerSpy, 'clientWidth', 'get').mockReturnValue(100);
jest.spyOn(containerSpy, 'clientHeight', 'get').mockReturnValue(100);
jest
.spyOn(containerSpy, 'getBoundingClientRect')
.mockReturnValue(mockBoundingBox);

const { result } = renderHook(() => useRive(params));

Expand Down Expand Up @@ -200,8 +212,9 @@ describe('useRive', () => {

const canvasSpy = document.createElement('canvas');
const containerSpy = document.createElement('div');
jest.spyOn(containerSpy, 'clientWidth', 'get').mockReturnValue(100);
jest.spyOn(containerSpy, 'clientHeight', 'get').mockReturnValue(100);
jest
.spyOn(containerSpy, 'getBoundingClientRect')
.mockReturnValue(mockBoundingBox);

const { result } = renderHook(() =>
useRive(params, { customDevicePixelRatio: 1 })
Expand Down Expand Up @@ -241,8 +254,9 @@ describe('useRive', () => {

const canvasSpy = document.createElement('canvas');
const containerSpy = document.createElement('div');
jest.spyOn(containerSpy, 'clientWidth', 'get').mockReturnValue(100);
jest.spyOn(containerSpy, 'clientHeight', 'get').mockReturnValue(100);
jest
.spyOn(containerSpy, 'getBoundingClientRect')
.mockReturnValue(mockBoundingBox);

const { result } = renderHook(() => useRive(params, opts));

Expand Down Expand Up @@ -284,8 +298,9 @@ describe('useRive', () => {

const canvasSpy = document.createElement('canvas');
const containerSpy = document.createElement('div');
jest.spyOn(containerSpy, 'clientWidth', 'get').mockReturnValue(100);
jest.spyOn(containerSpy, 'clientHeight', 'get').mockReturnValue(100);
jest
.spyOn(containerSpy, 'getBoundingClientRect')
.mockReturnValue(mockBoundingBox);

const { result } = renderHook(() => useRive(params, opts));

Expand Down Expand Up @@ -468,8 +483,9 @@ describe('useRive', () => {

const canvasSpy = document.createElement('canvas');
const containerSpy = document.createElement('div');
jest.spyOn(containerSpy, 'clientWidth', 'get').mockReturnValue(100);
jest.spyOn(containerSpy, 'clientHeight', 'get').mockReturnValue(100);
jest
.spyOn(containerSpy, 'getBoundingClientRect')
.mockReturnValue(mockBoundingBox);

const { result } = renderHook(() => useRive(params));

Expand Down Expand Up @@ -500,16 +516,18 @@ describe('useRive', () => {

const canvasSpy = document.createElement('canvas');
const containerSpy = document.createElement('div');
jest.spyOn(containerSpy, 'clientWidth', 'get').mockReturnValue(100);
jest.spyOn(containerSpy, 'clientHeight', 'get').mockReturnValue(100);
jest
.spyOn(containerSpy, 'getBoundingClientRect')
.mockReturnValue(mockBoundingBox);

const { result } = renderHook(() => useRive(params));

await act(async () => {
result.current.setCanvasRef(canvasSpy);
result.current.setContainerRef(containerSpy);
jest.spyOn(containerSpy, 'clientWidth', 'get').mockReturnValue(200);
jest.spyOn(containerSpy, 'clientHeight', 'get').mockReturnValue(200);
jest
.spyOn(containerSpy, 'getBoundingClientRect')
.mockReturnValue({...mockBoundingBox, width: 200, height: 200});
});
await waitFor(() => {
expect(result.current.canvas).toBe(canvasSpy);
Expand Down Expand Up @@ -559,8 +577,9 @@ describe('useRive', () => {
});

await act(async () => {
jest.spyOn(containerSpy, 'clientWidth', 'get').mockReturnValue(500);
jest.spyOn(containerSpy, 'clientHeight', 'get').mockReturnValue(500);
jest
.spyOn(containerSpy, 'getBoundingClientRect')
.mockReturnValue({...mockBoundingBox, width: 500, height: 500});
containerSpy.dispatchEvent(new Event('resize'));
});

Expand Down

0 comments on commit 45dacb6

Please sign in to comment.