Skip to content

Commit 79ca848

Browse files
committed
test: add test cases for useStateMachineInputs hook
1 parent f25dc49 commit 79ca848

1 file changed

Lines changed: 161 additions & 0 deletions

File tree

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
import { mocked } from 'jest-mock';
2+
import { renderHook } from '@testing-library/react-hooks';
3+
4+
import useStateMachineInputs from '../src/hooks/useStateMachineInputs';
5+
import { Rive, StateMachineInput } from '@rive-app/canvas';
6+
7+
jest.mock('@rive-app/canvas', () => ({
8+
Rive: jest.fn().mockImplementation(() => ({
9+
on: jest.fn(),
10+
off: jest.fn(),
11+
stop: jest.fn(),
12+
stateMachineInputs: jest.fn(),
13+
})),
14+
Layout: jest.fn(),
15+
Fit: {
16+
Cover: 'cover',
17+
},
18+
Alignment: {
19+
Center: 'center',
20+
},
21+
EventType: {
22+
Load: 'load',
23+
},
24+
StateMachineInputType: {
25+
Number: 1,
26+
Boolean: 2,
27+
Trigger: 3,
28+
},
29+
}));
30+
31+
function getRiveMock({
32+
smiInputs,
33+
}: {
34+
smiInputs?: null | StateMachineInput[];
35+
} = {}) {
36+
const riveMock = new Rive({
37+
canvas: undefined as unknown as HTMLCanvasElement,
38+
});
39+
if (smiInputs) {
40+
riveMock.stateMachineInputs = jest.fn().mockReturnValue(smiInputs);
41+
}
42+
43+
return riveMock;
44+
}
45+
46+
describe('useStateMachineInputs', () => {
47+
it('returns empty array if there is null rive object passed', () => {
48+
const { result } = renderHook(() => useStateMachineInputs(null));
49+
expect(result.current).toEqual([]);
50+
});
51+
52+
it('returns empty array if there is no state machine name', () => {
53+
const riveMock = getRiveMock();
54+
mocked(Rive).mockImplementation(() => riveMock);
55+
56+
const { result } = renderHook(() =>
57+
useStateMachineInputs(riveMock, '', [{ name: 'testInput' }])
58+
);
59+
expect(result.current).toEqual([]);
60+
});
61+
62+
it('returns empty array if there are no input names provided', () => {
63+
const riveMock = getRiveMock();
64+
mocked(Rive).mockImplementation(() => riveMock);
65+
66+
const { result } = renderHook(() =>
67+
useStateMachineInputs(riveMock, 'smName', [])
68+
);
69+
expect(result.current).toEqual([]);
70+
});
71+
72+
it('returns empty array if there are no inputs for the state machine', () => {
73+
const riveMock = getRiveMock({ smiInputs: [] });
74+
mocked(Rive).mockImplementation(() => riveMock);
75+
76+
const { result } = renderHook(() =>
77+
useStateMachineInputs(riveMock, 'smName', [{ name: 'testInput' }])
78+
);
79+
expect(result.current).toEqual([]);
80+
});
81+
82+
it('returns only the inputs that exist in the state machine', () => {
83+
const smInputs = [
84+
{ name: 'input1' } as StateMachineInput,
85+
{ name: 'input2' } as StateMachineInput,
86+
];
87+
const riveMock = getRiveMock({ smiInputs: smInputs });
88+
mocked(Rive).mockImplementation(() => riveMock);
89+
90+
const { result } = renderHook(() =>
91+
useStateMachineInputs(riveMock, 'smName', [
92+
{ name: 'input1' },
93+
{ name: 'nonexistent' },
94+
{ name: 'input2' },
95+
])
96+
);
97+
expect(result.current).toEqual([smInputs[0], smInputs[1]]);
98+
});
99+
100+
it('sets initial values on the inputs when provided', () => {
101+
const smInputs = [
102+
{ name: 'boolInput', value: false } as StateMachineInput,
103+
{ name: 'numInput', value: 0 } as StateMachineInput,
104+
];
105+
const riveMock = getRiveMock({ smiInputs: smInputs });
106+
mocked(Rive).mockImplementation(() => riveMock);
107+
108+
const { result } = renderHook(() =>
109+
useStateMachineInputs(riveMock, 'smName', [
110+
{ name: 'boolInput', initialValue: true },
111+
{ name: 'numInput', initialValue: 42 },
112+
])
113+
);
114+
115+
expect(result.current[0].value).toBe(true);
116+
expect(result.current[1].value).toBe(42);
117+
});
118+
119+
it('does not set initial values if not provided', () => {
120+
const smInputs = [
121+
{ name: 'boolInput', value: false } as StateMachineInput,
122+
{ name: 'numInput', value: 0 } as StateMachineInput,
123+
];
124+
const riveMock = getRiveMock({ smiInputs: smInputs });
125+
mocked(Rive).mockImplementation(() => riveMock);
126+
127+
const { result } = renderHook(() =>
128+
useStateMachineInputs(riveMock, 'smName', [
129+
{ name: 'boolInput' },
130+
{ name: 'numInput' },
131+
])
132+
);
133+
134+
expect(result.current[0].value).toBe(false);
135+
expect(result.current[1].value).toBe(0);
136+
});
137+
138+
it('preserves the order of inputs as specified in inputNames', () => {
139+
const smInputs = [
140+
{ name: 'input1' } as StateMachineInput,
141+
{ name: 'input2' } as StateMachineInput,
142+
{ name: 'input3' } as StateMachineInput,
143+
];
144+
const riveMock = getRiveMock({ smiInputs: smInputs });
145+
mocked(Rive).mockImplementation(() => riveMock);
146+
147+
const { result } = renderHook(() =>
148+
useStateMachineInputs(riveMock, 'smName', [
149+
{ name: 'input3' },
150+
{ name: 'input1' },
151+
{ name: 'input2' },
152+
])
153+
);
154+
155+
expect(result.current.map((input) => input.name)).toEqual([
156+
'input3',
157+
'input1',
158+
'input2',
159+
]);
160+
});
161+
});

0 commit comments

Comments
 (0)