-
Notifications
You must be signed in to change notification settings - Fork 37
/
useStateMachine.test.tsx
126 lines (105 loc) · 3.35 KB
/
useStateMachine.test.tsx
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
import { mocked } from 'jest-mock';
import { renderHook } from '@testing-library/react-hooks';
import useStateMachineInput from '../src/hooks/useStateMachineInput';
import { Rive, StateMachineInput } from '@rive-app/canvas';
jest.mock('@rive-app/canvas', () => ({
Rive: jest.fn().mockImplementation(() => ({
on: 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('useStateMachineInput', () => {
it('returns null if there is null rive object passed', () => {
const { result } = renderHook(() => useStateMachineInput(null));
expect(result.current).toBeNull();
});
it('returns null if there is no state machine name', () => {
const riveMock = getRiveMock();
mocked(Rive).mockImplementation(() => riveMock);
const { result } = renderHook(() =>
useStateMachineInput(riveMock, '', 'testInput')
);
expect(result.current).toBeNull();
});
it('returns null if there is no state machine input name', () => {
const riveMock = getRiveMock();
const { result } = renderHook(() =>
useStateMachineInput(riveMock, 'smName', '')
);
expect(result.current).toBeNull();
});
it('returns null if there are no inputs for the state machine', () => {
const riveMock = getRiveMock({ smiInputs: [] });
mocked(Rive).mockImplementation(() => riveMock);
const { result } = renderHook(() =>
useStateMachineInput(riveMock as Rive, 'smName', '')
);
expect(result.current).toBeNull();
});
it('returns null if the input has no association to the inputs of the state machine', () => {
const smInput = {
name: 'boolInput',
} as StateMachineInput;
const riveMock = getRiveMock({ smiInputs: [smInput] });
mocked(Rive).mockImplementation(() => riveMock);
const { result } = renderHook(() =>
useStateMachineInput(riveMock, 'smName', 'numInput')
);
expect(result.current).toBeNull();
});
it('returns a selected input if the input requested is part of the state machine', () => {
const smInput = {
name: 'boolInput',
} as StateMachineInput;
const riveMock = getRiveMock({ smiInputs: [smInput] });
mocked(Rive).mockImplementation(() => riveMock);
const { result } = renderHook(() =>
useStateMachineInput(riveMock, 'smName', 'boolInput')
);
expect(result.current).toBe(smInput);
});
it('returns a selected input with an initial value if the input requested is part of the state machine', () => {
const smInput = {
name: 'boolInput',
value: false,
} as StateMachineInput;
const riveMock = getRiveMock({ smiInputs: [smInput] });
mocked(Rive).mockImplementation(() => riveMock);
const { result } = renderHook(() =>
useStateMachineInput(riveMock, 'smName', 'boolInput', true)
);
expect(result.current).toStrictEqual({
...smInput,
value: true,
});
});
});