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