Skip to content

Commit

Permalink
tests for rapid yielding iterables value batching for useAsyncIter
Browse files Browse the repository at this point in the history
…and `<Iterate>`
  • Loading branch information
shtaif committed Dec 27, 2024
1 parent 679cb23 commit d2ea95b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
36 changes: 36 additions & 0 deletions spec/tests/Iterate.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,42 @@ describe('`Iterate` component', () => {
expect(channelReturnSpy).toHaveBeenCalledOnce();
}
});

it(
gray(
'When given a rapid yielding iterable, consecutive values are batched into a single render that takes only the last value'
),
async () => {
const iter = (async function* () {
yield* ['a', 'b', 'c'];
})();
let timesRerendered = 0;
let lastRenderFnInput: undefined | IterationResult<string>;

const rendered = render(
<Iterate value={iter}>
{next => {
timesRerendered++;
lastRenderFnInput = next;
return <div id="test-created-elem">Render count: {timesRerendered}</div>;
}}
</Iterate>
);

await act(() => {});

expect(lastRenderFnInput).toStrictEqual({
value: 'c',
pendingFirst: false,
done: true,
error: undefined,
});
expect(timesRerendered).toStrictEqual(2);
expect(rendered.container.innerHTML).toStrictEqual(
'<div id="test-created-elem">Render count: 2</div>'
);
}
);
});

const simulatedError = new Error('🚨 Simulated Error 🚨');
27 changes: 27 additions & 0 deletions spec/tests/useAsyncIter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,33 @@ describe('`useAsyncIter` hook', () => {
expect(channelReturnSpy).toHaveBeenCalledOnce();
}
});

it(
gray(
'When given a rapid yielding iterable, consecutive values are batched into a single render that takes only the last value'
),
async () => {
let timesRerendered = 0;
const iter = (async function* () {
yield* ['a', 'b', 'c'];
})();

const renderedHook = renderHook(() => {
timesRerendered++;
return useAsyncIter(iter);
});

await act(() => {});

expect(timesRerendered).toStrictEqual(2);
expect(renderedHook.result.current).toStrictEqual({
value: 'c',
pendingFirst: false,
done: true,
error: undefined,
});
}
);
});

const simulatedError = new Error('🚨 Simulated Error 🚨');

0 comments on commit d2ea95b

Please sign in to comment.