Skip to content

Commit

Permalink
test: Throw custom error instead of relying on runtime error (#24946)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Jul 21, 2022
1 parent 9bd0dd4 commit 6b28bc9
Showing 1 changed file with 14 additions and 8 deletions.
Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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');
});
});
});

0 comments on commit 6b28bc9

Please sign in to comment.