test: Throw custom error instead of relying on runtime error (#24946)

This commit is contained in:
Sebastian Silbermann 2022-07-21 21:46:57 +02:00 committed by GitHub
parent 9bd0dd4c18
commit 6b28bc9c5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 8 deletions

View File

@ -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');
});
});
});