test: Throw custom error instead of relying on runtime error (#24946)
This commit is contained in:
parent
9bd0dd4c18
commit
6b28bc9c5a
|
@ -900,7 +900,12 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
|
|||
|
||||
it('selector can throw on update', async () => {
|
||||
const store = createExternalStore({a: 'a'});
|
||||
const selector = state => state.a.toUpperCase();
|
||||
const selector = state => {
|
||||
if (typeof state.a !== 'string') {
|
||||
throw new TypeError('Malformed state');
|
||||
}
|
||||
return state.a.toUpperCase();
|
||||
};
|
||||
|
||||
function App() {
|
||||
const a = useSyncExternalStoreWithSelector(
|
||||
|
@ -927,15 +932,18 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
|
|||
await act(() => {
|
||||
store.set({});
|
||||
});
|
||||
expect(container.textContent).toEqual(
|
||||
"Cannot read property 'toUpperCase' of undefined",
|
||||
);
|
||||
expect(container.textContent).toEqual('Malformed state');
|
||||
});
|
||||
|
||||
it('isEqual can throw on update', async () => {
|
||||
const store = createExternalStore({a: 'A'});
|
||||
const selector = state => state.a;
|
||||
const isEqual = (left, right) => left.a.trim() === right.a.trim();
|
||||
const isEqual = (left, right) => {
|
||||
if (typeof left.a !== 'string' || typeof right.a !== 'string') {
|
||||
throw new TypeError('Malformed state');
|
||||
}
|
||||
return left.a.trim() === right.a.trim();
|
||||
};
|
||||
|
||||
function App() {
|
||||
const a = useSyncExternalStoreWithSelector(
|
||||
|
@ -963,9 +971,7 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
|
|||
await act(() => {
|
||||
store.set({});
|
||||
});
|
||||
expect(container.textContent).toEqual(
|
||||
"Cannot read property 'trim' of undefined",
|
||||
);
|
||||
expect(container.textContent).toEqual('Malformed state');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue