-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathuseRiveProperty.test.ts
More file actions
237 lines (195 loc) · 7.73 KB
/
useRiveProperty.test.ts
File metadata and controls
237 lines (195 loc) · 7.73 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import { renderHook, act } from '@testing-library/react-native';
import { useRiveProperty } from '../useRiveProperty';
import type { ViewModelInstance } from '../../specs/ViewModel.nitro';
describe('useRiveProperty', () => {
const createMockProperty = (initialValue: string) => {
let currentValue = initialValue;
let listener: ((value: string) => void) | null = null;
return {
get value() {
return currentValue;
},
set value(newValue: string) {
currentValue = newValue;
listener?.(newValue);
},
addListener: jest.fn((callback: (value: string) => void) => {
listener = callback;
// Emit the current value immediately on subscribe, matching native behaviour:
// iOS legacy emits synchronously; experimental backend emits via valueStream.
callback(currentValue);
return () => {
listener = null;
};
}),
dispose: jest.fn(),
};
};
const createMockViewModelInstance = (
propertyMap: Record<string, ReturnType<typeof createMockProperty>>
) => {
return {
enumProperty: jest.fn((path: string) => propertyMap[path]),
numberProperty: jest.fn((path: string) => propertyMap[path]),
stringProperty: jest.fn((path: string) => propertyMap[path]),
booleanProperty: jest.fn((path: string) => propertyMap[path]),
} as unknown as ViewModelInstance;
};
it('should return initial value delivered via listener (not from a sync read)', () => {
// Hooks always start undefined; the listener emits the current value immediately
// on subscribe (synchronously for legacy, via stream for experimental).
const mockProperty = createMockProperty('Tea');
const mockInstance = createMockViewModelInstance({
'favDrink/type': mockProperty,
});
const { result } = renderHook(() =>
useRiveProperty<any, string>(mockInstance, 'favDrink/type', {
getProperty: (vmi, path) => (vmi as any).enumProperty(path),
})
);
// The mock's addListener emits 'Tea' synchronously — React batches it with the
// effect, so the value is available after renderHook (which wraps in act()).
const [value] = result.current;
expect(value).toBe('Tea');
});
it('should update value when property changes', () => {
const mockProperty = createMockProperty('Tea');
const mockInstance = createMockViewModelInstance({
'favDrink/type': mockProperty,
});
const { result } = renderHook(() =>
useRiveProperty<any, string>(mockInstance, 'favDrink/type', {
getProperty: (vmi, path) => (vmi as any).enumProperty(path),
})
);
act(() => {
mockProperty.value = 'Coffee';
});
const [value] = result.current;
expect(value).toBe('Coffee');
});
it('should return undefined when viewModelInstance is null', () => {
const { result } = renderHook(() =>
useRiveProperty<any, string>(null, 'favDrink/type', {
getProperty: (vmi, path) => (vmi as any).enumProperty(path),
})
);
const [value] = result.current;
expect(value).toBeUndefined();
});
it('should return error when property is not found on a valid instance', () => {
const mockInstance = createMockViewModelInstance({});
const { result } = renderHook(() =>
useRiveProperty<any, string>(mockInstance, 'nonexistent/path', {
getProperty: (vmi, path) => (vmi as any).enumProperty(path),
})
);
const [, , error] = result.current;
expect(error).toBeInstanceOf(Error);
expect(error?.message).toContain('nonexistent/path');
});
it('should not crash when setValue is called on an invalid property', () => {
const mockInstance = createMockViewModelInstance({});
const { result } = renderHook(() =>
useRiveProperty<any, string>(mockInstance, 'nonexistent/path', {
getProperty: (vmi, path) => (vmi as any).enumProperty(path),
})
);
// Error already set by useEffect (property not found on valid instance)
expect(result.current[2]).toBeInstanceOf(Error);
// Calling setValue should be a no-op, not throw
act(() => {
const [, setValue] = result.current;
setValue('Hello');
});
// Error unchanged — still the original "not found" error
expect(result.current[2]).toBeInstanceOf(Error);
expect(result.current[2]?.message).toContain('nonexistent/path');
});
it('should not error when setValue is called before instance is ready', () => {
// Start with undefined instance (simulates async file loading)
const { result } = renderHook(
(props: { instance: ViewModelInstance | undefined }) =>
useRiveProperty<any, string>(props.instance, 'text', {
getProperty: (vmi, path) => (vmi as any).stringProperty(path),
}),
{ initialProps: { instance: undefined } }
);
// setValue should be a no-op, not set an error
act(() => {
const [, setValue] = result.current;
setValue('Hello');
});
const [, , error] = result.current;
expect(error).toBeNull();
});
it('should apply value after instance becomes available', () => {
const mockProperty = createMockProperty('initial');
const mockInstance = createMockViewModelInstance({
text: mockProperty,
});
// Start with undefined instance
const { result, rerender } = renderHook(
(props: { instance: ViewModelInstance | undefined }) =>
useRiveProperty<any, string>(props.instance, 'text', {
getProperty: (vmi, path) => (vmi as any).stringProperty(path),
}),
{ initialProps: { instance: undefined } }
);
const setValueBeforeReady = result.current[1];
// setValue before ready — should be a no-op
act(() => {
setValueBeforeReady('Hello');
});
expect(result.current[2]).toBeNull();
// Instance becomes available
rerender({ instance: mockInstance });
// setValue identity must change so useEffect deps re-fire automatically
const setValueAfterReady = result.current[1];
expect(setValueAfterReady).not.toBe(setValueBeforeReady);
// Now setValue should work
act(() => {
setValueAfterReady('Hello');
});
expect(mockProperty.value).toBe('Hello');
expect(result.current[2]).toBeNull();
});
it('should update value when path changes', () => {
const teaProperty = createMockProperty('Tea');
const coffeeProperty = createMockProperty('Coffee');
const mockInstance = createMockViewModelInstance({
'drinks/tea': teaProperty,
'drinks/coffee': coffeeProperty,
});
const { result, rerender } = renderHook(
(props: { path: string }) =>
useRiveProperty<any, string>(mockInstance, props.path, {
getProperty: (vmi, p) => (vmi as any).enumProperty(p),
}),
{ initialProps: { path: 'drinks/tea' } }
);
expect(result.current[0]).toBe('Tea');
rerender({ path: 'drinks/coffee' });
expect(result.current[0]).toBe('Coffee');
});
it('should update value when viewModelInstance changes', () => {
const instance1Property = createMockProperty('Instance1Value');
const instance2Property = createMockProperty('Instance2Value');
const mockInstance1 = createMockViewModelInstance({
'prop/path': instance1Property,
});
const mockInstance2 = createMockViewModelInstance({
'prop/path': instance2Property,
});
const { result, rerender } = renderHook(
(props: { instance: ViewModelInstance }) =>
useRiveProperty<any, string>(props.instance, 'prop/path', {
getProperty: (vmi, p) => (vmi as any).enumProperty(p),
}),
{ initialProps: { instance: mockInstance1 } }
);
expect(result.current[0]).toBe('Instance1Value');
rerender({ instance: mockInstance2 });
expect(result.current[0]).toBe('Instance2Value');
});
});