Skip to content

More test edits #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions spec/tests/Iterate.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { it, describe, expect, afterEach, vi } from 'vitest';
import { it, describe, expect, afterEach } from 'vitest';
import { gray } from 'colorette';
import { render, cleanup as cleanupMountedReactTrees, act } from '@testing-library/react';
import { Iterate, It, type IterationResult } from '../../src/index.js';
Expand Down Expand Up @@ -503,11 +503,6 @@ describe('`Iterate` component', () => {
new IteratorChannelTestHelper<string>(),
];

const [channelReturnSpy1, channelReturnSpy2] = [
vi.spyOn(channel1, 'return'),
vi.spyOn(channel2, 'return'),
];

const buildTestContent = (value: AsyncIterable<string>) => {
return (
<Iterate value={value}>
Expand All @@ -524,8 +519,8 @@ describe('`Iterate` component', () => {
{
rendered.rerender(buildTestContent(channel1));

expect(channelReturnSpy1).not.toHaveBeenCalled();
expect(channelReturnSpy2).not.toHaveBeenCalled();
expect(channel1.return).not.toHaveBeenCalled();
expect(channel2.return).not.toHaveBeenCalled();
expect(lastRenderFnInput).toStrictEqual({
value: undefined,
pendingFirst: true,
Expand All @@ -548,8 +543,8 @@ describe('`Iterate` component', () => {
{
rendered.rerender(buildTestContent(channel2));

expect(channelReturnSpy1).toHaveBeenCalledOnce();
expect(channelReturnSpy2).not.toHaveBeenCalled();
expect(channel1.return).toHaveBeenCalledOnce();
expect(channel2.return).not.toHaveBeenCalled();
expect(lastRenderFnInput).toStrictEqual({
value: 'a',
pendingFirst: true,
Expand All @@ -572,8 +567,8 @@ describe('`Iterate` component', () => {
{
rendered.rerender(buildTestContent((async function* () {})()));

expect(channelReturnSpy1).toHaveBeenCalledOnce();
expect(channelReturnSpy2).toHaveBeenCalledOnce();
expect(channel1.return).toHaveBeenCalledOnce();
expect(channel2.return).toHaveBeenCalledOnce();
expect(lastRenderFnInput).toStrictEqual({
value: 'b',
pendingFirst: true,
Expand All @@ -589,7 +584,6 @@ describe('`Iterate` component', () => {
let lastRenderFnInput: undefined | IterationResult<string | undefined>;

const channel = new IteratorChannelTestHelper<string>();
const channelReturnSpy = vi.spyOn(channel, 'return');

const buildTestContent = (value: AsyncIterable<string>) => {
return (
Expand All @@ -607,7 +601,7 @@ describe('`Iterate` component', () => {
{
rendered.rerender(buildTestContent(channel));

expect(channelReturnSpy).not.toHaveBeenCalled();
expect(channel.return).not.toHaveBeenCalled();
expect(lastRenderFnInput).toStrictEqual({
value: undefined,
pendingFirst: true,
Expand All @@ -629,7 +623,7 @@ describe('`Iterate` component', () => {

{
rendered.unmount();
expect(channelReturnSpy).toHaveBeenCalledOnce();
expect(channel.return).toHaveBeenCalledOnce();
}
});

Expand Down
17 changes: 6 additions & 11 deletions spec/tests/iterateFormatted.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { it, describe, expect, afterEach, vi } from 'vitest';
import { it, describe, expect, afterEach } from 'vitest';
import { gray } from 'colorette';
import { render, cleanup as cleanupMountedReactTrees, act } from '@testing-library/react';
import { iterateFormatted, Iterate } from '../../src/index.js';
Expand Down Expand Up @@ -84,11 +84,6 @@ describe('`iterateFormatted` function', () => {
new IteratorChannelTestHelper<string>(),
];

const [channelReturnSpy1, channelReturnSpy2] = [
vi.spyOn(channel1, 'return'),
vi.spyOn(channel2, 'return'),
];

const rebuildTestContent = (it: AsyncIterable<string>) => (
<Iterate
value={pipe(
Expand All @@ -104,17 +99,17 @@ describe('`iterateFormatted` function', () => {
const rendered = render(<></>);

rendered.rerender(rebuildTestContent(channel1));
expect(channelReturnSpy1).not.toHaveBeenCalled();
expect(channel1.return).not.toHaveBeenCalled();

rendered.rerender(rebuildTestContent(channel1));
expect(channelReturnSpy1).not.toHaveBeenCalled();
expect(channel1.return).not.toHaveBeenCalled();

rendered.rerender(rebuildTestContent(channel2));
expect(channelReturnSpy1).toHaveBeenCalledOnce();
expect(channelReturnSpy2).not.toHaveBeenCalled();
expect(channel1.return).toHaveBeenCalledOnce();
expect(channel2.return).not.toHaveBeenCalled();

rendered.rerender(rebuildTestContent(channel2));
expect(channelReturnSpy2).not.toHaveBeenCalled();
expect(channel2.return).not.toHaveBeenCalled();
}
);

Expand Down
24 changes: 9 additions & 15 deletions spec/tests/useAsyncIter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { it, describe, expect, afterEach, vi } from 'vitest';
import { it, describe, expect, afterEach } from 'vitest';
import { gray } from 'colorette';
import { cleanup as cleanupMountedReactTrees, act, renderHook } from '@testing-library/react';
import { useAsyncIter } from '../../src/index.js';
Expand Down Expand Up @@ -339,11 +339,6 @@ describe('`useAsyncIter` hook', () => {
new IteratorChannelTestHelper<string>(),
];

const [channelReturnSpy1, channelReturnSpy2] = [
vi.spyOn(channel1, 'return'),
vi.spyOn(channel2, 'return'),
];

const renderedHook = renderHook(({ value }) => useAsyncIter(value), {
initialProps: {
value: (async function* () {})() as AsyncIterable<string>,
Expand All @@ -353,8 +348,8 @@ describe('`useAsyncIter` hook', () => {
{
renderedHook.rerender({ value: channel1 });

expect(channelReturnSpy1).not.toHaveBeenCalled();
expect(channelReturnSpy2).not.toHaveBeenCalled();
expect(channel1.return).not.toHaveBeenCalled();
expect(channel2.return).not.toHaveBeenCalled();
expect(renderedHook.result.current).toStrictEqual({
value: undefined,
pendingFirst: true,
Expand All @@ -375,8 +370,8 @@ describe('`useAsyncIter` hook', () => {
{
renderedHook.rerender({ value: channel2 });

expect(channelReturnSpy1).toHaveBeenCalledOnce();
expect(channelReturnSpy2).not.toHaveBeenCalled();
expect(channel1.return).toHaveBeenCalledOnce();
expect(channel2.return).not.toHaveBeenCalled();
expect(renderedHook.result.current).toStrictEqual({
value: 'a',
pendingFirst: true,
Expand All @@ -397,8 +392,8 @@ describe('`useAsyncIter` hook', () => {
{
renderedHook.rerender({ value: (async function* () {})() });

expect(channelReturnSpy1).toHaveBeenCalledOnce();
expect(channelReturnSpy2).toHaveBeenCalledOnce();
expect(channel1.return).toHaveBeenCalledOnce();
expect(channel2.return).toHaveBeenCalledOnce();
expect(renderedHook.result.current).toStrictEqual({
value: 'b',
pendingFirst: true,
Expand All @@ -411,7 +406,6 @@ describe('`useAsyncIter` hook', () => {

it(gray('When unmounted will close the last active iterator it held'), async () => {
const channel = new IteratorChannelTestHelper<string>();
const channelReturnSpy = vi.spyOn(channel, 'return');

const renderedHook = renderHook(({ value }) => useAsyncIter(value), {
initialProps: {
Expand All @@ -422,7 +416,7 @@ describe('`useAsyncIter` hook', () => {
{
renderedHook.rerender({ value: channel });

expect(channelReturnSpy).not.toHaveBeenCalled();
expect(channel.return).not.toHaveBeenCalled();
expect(renderedHook.result.current).toStrictEqual({
value: undefined,
pendingFirst: true,
Expand All @@ -442,7 +436,7 @@ describe('`useAsyncIter` hook', () => {

{
renderedHook.unmount();
expect(channelReturnSpy).toHaveBeenCalledOnce();
expect(channel.return).toHaveBeenCalledOnce();
}
});

Expand Down
68 changes: 29 additions & 39 deletions spec/tests/useAsyncIterMulti.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { it, describe, expect, afterEach, vi } from 'vitest';
import { it, describe, expect, afterEach } from 'vitest';
import { gray } from 'colorette';
import { range } from 'lodash-es';
import { cleanup as cleanupMountedReactTrees, act, renderHook } from '@testing-library/react';
Expand Down Expand Up @@ -352,8 +352,8 @@ describe('`useAsyncIterMulti` hook', () => {
async () => {
let channel1 = new IteratorChannelTestHelper<'a' | 'b' | 'c'>();
let channel2 = new IteratorChannelTestHelper<'a' | 'b' | 'c'>();
const channel1ReturnSpy = vi.spyOn(channel1, 'return');
const channel2ReturnSpy = vi.spyOn(channel2, 'return');
const origChannel1ReturnSpy = channel1.return;
const origChannel2ReturnSpy = channel2.return;
let timesRerendered = 0;

const renderedHook = renderHook(
Expand All @@ -366,17 +366,17 @@ describe('`useAsyncIterMulti` hook', () => {

await act(() => {});
expect(timesRerendered).toStrictEqual(1);
expect(channel1ReturnSpy).not.toHaveBeenCalled();
expect(channel2ReturnSpy).not.toHaveBeenCalled();
expect(origChannel1ReturnSpy).not.toHaveBeenCalled();
expect(origChannel2ReturnSpy).not.toHaveBeenCalled();
expect(renderedHook.result.current).toStrictEqual([
{ value: undefined, pendingFirst: true, done: false, error: undefined },
{ value: undefined, pendingFirst: true, done: false, error: undefined },
]);

await act(() => channel1.put('a'));
expect(timesRerendered).toStrictEqual(2);
expect(channel1ReturnSpy).not.toHaveBeenCalled();
expect(channel2ReturnSpy).not.toHaveBeenCalled();
expect(origChannel1ReturnSpy).not.toHaveBeenCalled();
expect(origChannel2ReturnSpy).not.toHaveBeenCalled();
expect(renderedHook.result.current).toStrictEqual([
{ value: 'a', pendingFirst: false, done: false, error: undefined },
{ value: undefined, pendingFirst: true, done: false, error: undefined },
Expand All @@ -387,8 +387,8 @@ describe('`useAsyncIterMulti` hook', () => {
renderedHook.rerender({ values: [channel1, channel2] as const });
});
expect(timesRerendered).toStrictEqual(3);
expect(channel1ReturnSpy).toHaveBeenCalled();
expect(channel2ReturnSpy).not.toHaveBeenCalled();
expect(origChannel1ReturnSpy).toHaveBeenCalledOnce();
expect(origChannel2ReturnSpy).not.toHaveBeenCalled();
expect(renderedHook.result.current).toStrictEqual([
{ value: 'a', pendingFirst: true, done: false, error: undefined },
{ value: undefined, pendingFirst: true, done: false, error: undefined },
Expand All @@ -399,8 +399,8 @@ describe('`useAsyncIterMulti` hook', () => {
renderedHook.rerender({ values: [channel1, channel2] as const });
});
expect(timesRerendered).toStrictEqual(4);
expect(channel1ReturnSpy).toHaveBeenCalled();
expect(channel2ReturnSpy).toHaveBeenCalled();
expect(origChannel1ReturnSpy).toHaveBeenCalledOnce();
expect(origChannel2ReturnSpy).toHaveBeenCalledOnce();
expect(renderedHook.result.current).toStrictEqual([
{ value: 'a', pendingFirst: true, done: false, error: undefined },
{ value: undefined, pendingFirst: true, done: false, error: undefined },
Expand All @@ -418,8 +418,6 @@ describe('`useAsyncIterMulti` hook', () => {
it(gray('When unmounted will close all the last held active iterators'), async () => {
const channel1 = new IteratorChannelTestHelper<'a' | 'b' | 'c'>();
const channel2 = new IteratorChannelTestHelper<'a' | 'b' | 'c'>();
const channel1ReturnSpy = vi.spyOn(channel1, 'return');
const channel2ReturnSpy = vi.spyOn(channel2, 'return');

const renderedHook = renderHook(props => useAsyncIterMulti(props.values), {
initialProps: {
Expand All @@ -432,8 +430,8 @@ describe('`useAsyncIterMulti` hook', () => {

{
await act(() => renderedHook.rerender({ values: [channel1, channel2] }));
expect(channel1ReturnSpy).not.toHaveBeenCalled();
expect(channel2ReturnSpy).not.toHaveBeenCalled();
expect(channel1.return).not.toHaveBeenCalled();
expect(channel2.return).not.toHaveBeenCalled();
expect(renderedHook.result.current).toStrictEqual([
{ value: undefined, pendingFirst: true, done: false, error: undefined },
{ value: undefined, pendingFirst: true, done: false, error: undefined },
Expand All @@ -448,8 +446,8 @@ describe('`useAsyncIterMulti` hook', () => {

{
renderedHook.unmount();
expect(channel1ReturnSpy).toHaveBeenCalledOnce();
expect(channel2ReturnSpy).toHaveBeenCalledOnce();
expect(channel1.return).toHaveBeenCalledOnce();
expect(channel2.return).toHaveBeenCalledOnce();
}
});

Expand All @@ -464,12 +462,6 @@ describe('`useAsyncIterMulti` hook', () => {
const channelB = new IteratorChannelTestHelper<string>();
const channelC = new IteratorChannelTestHelper<string>();

const [channelReturnSpyA, channelReturnSpyB, channelReturnSpyC] = [
channelA,
channelB,
channelC,
].map(ch => vi.spyOn(ch, 'return'));

const renderedHook = await act(() =>
renderHook(({ values }) => useAsyncIterMulti(values), {
initialProps: { values },
Expand Down Expand Up @@ -555,9 +547,9 @@ describe('`useAsyncIterMulti` hook', () => {
values.splice(0, 3);
renderedHook.rerender({ values });
});
expect(channelReturnSpyC).not.toHaveBeenCalled();
expect(channelReturnSpyB).not.toHaveBeenCalled();
expect(channelReturnSpyA).not.toHaveBeenCalled();
expect(channelC.return).not.toHaveBeenCalled();
expect(channelB.return).not.toHaveBeenCalled();
expect(channelA.return).not.toHaveBeenCalled();
expect(renderedHook.result.current).toStrictEqual([
{ value: 'c_from_iter', pendingFirst: false, done: false, error: undefined },
{ value: 'b_from_iter', pendingFirst: false, done: false, error: undefined },
Expand All @@ -568,9 +560,9 @@ describe('`useAsyncIterMulti` hook', () => {
values.shift();
renderedHook.rerender({ values });
});
expect(channelReturnSpyC).toHaveBeenCalledOnce();
expect(channelReturnSpyB).not.toHaveBeenCalled();
expect(channelReturnSpyA).not.toHaveBeenCalled();
expect(channelC.return).toHaveBeenCalledOnce();
expect(channelB.return).not.toHaveBeenCalled();
expect(channelA.return).not.toHaveBeenCalled();
expect(renderedHook.result.current).toStrictEqual([
{ value: 'b_from_iter', pendingFirst: false, done: false, error: undefined },
{ value: 'a_from_iter', pendingFirst: false, done: false, error: undefined },
Expand All @@ -580,9 +572,9 @@ describe('`useAsyncIterMulti` hook', () => {
values.shift();
renderedHook.rerender({ values });
});
expect(channelReturnSpyC).toHaveBeenCalledOnce();
expect(channelReturnSpyB).toHaveBeenCalledOnce();
expect(channelReturnSpyA).not.toHaveBeenCalled();
expect(channelC.return).toHaveBeenCalledOnce();
expect(channelB.return).toHaveBeenCalledOnce();
expect(channelA.return).not.toHaveBeenCalled();
expect(renderedHook.result.current).toStrictEqual([
{ value: 'a_from_iter', pendingFirst: false, done: false, error: undefined },
]);
Expand All @@ -591,9 +583,9 @@ describe('`useAsyncIterMulti` hook', () => {
values.shift();
renderedHook.rerender({ values });
});
expect(channelReturnSpyC).toHaveBeenCalledOnce();
expect(channelReturnSpyB).toHaveBeenCalledOnce();
expect(channelReturnSpyA).toHaveBeenCalledOnce();
expect(channelC.return).toHaveBeenCalledOnce();
expect(channelB.return).toHaveBeenCalledOnce();
expect(channelA.return).toHaveBeenCalledOnce();
expect(renderedHook.result.current).toStrictEqual([]);
}
);
Expand Down Expand Up @@ -665,8 +657,6 @@ describe('`useAsyncIterMulti` hook', () => {
async () => {
const channel1 = new IteratorChannelTestHelper<string>();
const channel2 = new IteratorChannelTestHelper<string>();
const channel1ReturnSpy = vi.spyOn(channel1, 'return');
const channel2ReturnSpy = vi.spyOn(channel2, 'return');

const renderedHook = await act(() =>
renderHook(() =>
Expand All @@ -685,8 +675,8 @@ describe('`useAsyncIterMulti` hook', () => {
for (let i = 0; i < 3; ++i) {
await act(() => renderedHook.rerender());
}
expect(channel1ReturnSpy).not.toHaveBeenCalled();
expect(channel2ReturnSpy).not.toHaveBeenCalled();
expect(channel1.return).not.toHaveBeenCalled();
expect(channel2.return).not.toHaveBeenCalled();

await act(() => channel1.put('a'));
expect(renderedHook.result.current).toStrictEqual([
Expand Down
Loading
Loading