Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ const setup = () => {
};

describe('createStreamingBatchedFunction()', () => {
beforeAll(() => {
jest.useFakeTimers();
});

afterAll(() => {
jest.useRealTimers();
});
test('returns a function', () => {
const { fetchStreaming } = setup();
const fn = createStreamingBatchedFunction({
Expand Down Expand Up @@ -86,8 +93,8 @@ describe('createStreamingBatchedFunction()', () => {
expect(fetchStreaming).toHaveBeenCalledTimes(0);
fn({ baz: 'quix' });
expect(fetchStreaming).toHaveBeenCalledTimes(0);
jest.advanceTimersByTime(6);

await new Promise((r) => setTimeout(r, 6));
expect(fetchStreaming).toHaveBeenCalledTimes(1);
});

Expand All @@ -102,7 +109,7 @@ describe('createStreamingBatchedFunction()', () => {
});

expect(fetchStreaming).toHaveBeenCalledTimes(0);
await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);
expect(fetchStreaming).toHaveBeenCalledTimes(0);
});

Expand All @@ -117,7 +124,7 @@ describe('createStreamingBatchedFunction()', () => {
});

fn({ foo: 'bar' });
await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

expect(fetchStreaming.mock.calls[0][0]).toMatchObject({
url: '/test',
Expand All @@ -138,7 +145,7 @@ describe('createStreamingBatchedFunction()', () => {
fn({ foo: 'bar' });
fn({ baz: 'quix' });

await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);
const { body } = fetchStreaming.mock.calls[0][0];
expect(JSON.parse(body)).toEqual({
batch: [{ foo: 'bar' }, { baz: 'quix' }],
Expand All @@ -159,13 +166,10 @@ describe('createStreamingBatchedFunction()', () => {

expect(fetchStreaming).toHaveBeenCalledTimes(0);
fn({ foo: 'bar' });
await flushPromises();
expect(fetchStreaming).toHaveBeenCalledTimes(0);
fn({ baz: 'quix' });
await flushPromises();
expect(fetchStreaming).toHaveBeenCalledTimes(0);
fn({ full: 'yep' });
await flushPromises();
expect(fetchStreaming).toHaveBeenCalledTimes(1);
});

Expand All @@ -185,7 +189,7 @@ describe('createStreamingBatchedFunction()', () => {
of(fn({ foo: 'bar' }, abortController.signal));
fn({ baz: 'quix' });

await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);
const { body } = fetchStreaming.mock.calls[0][0];
expect(JSON.parse(body)).toEqual({
batch: [{ baz: 'quix' }],
Expand All @@ -205,7 +209,6 @@ describe('createStreamingBatchedFunction()', () => {
fn({ a: '1' });
fn({ b: '2' });
fn({ c: '3' });
await flushPromises();

expect(fetchStreaming.mock.calls[0][0]).toMatchObject({
url: '/test',
Expand All @@ -230,11 +233,9 @@ describe('createStreamingBatchedFunction()', () => {
fn({ a: '1' });
fn({ b: '2' });
fn({ c: '3' });
await flushPromises();
expect(fetchStreaming).toHaveBeenCalledTimes(1);
fn({ d: '4' });
await flushPromises();
await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);
expect(fetchStreaming).toHaveBeenCalledTimes(2);
});
});
Expand All @@ -252,7 +253,7 @@ describe('createStreamingBatchedFunction()', () => {

const promise1 = fn({ a: '1' });
const promise2 = fn({ b: '2' });
await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

expect(await isPending(promise1)).toBe(true);
expect(await isPending(promise2)).toBe(true);
Expand All @@ -273,7 +274,7 @@ describe('createStreamingBatchedFunction()', () => {
const promise1 = fn({ a: '1' });
const promise2 = fn({ b: '2' });
const promise3 = fn({ c: '3' });
await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

expect(await isPending(promise1)).toBe(true);
expect(await isPending(promise2)).toBe(true);
Expand Down Expand Up @@ -315,7 +316,7 @@ describe('createStreamingBatchedFunction()', () => {
const promise1 = fn({ a: '1' });
const promise2 = fn({ b: '2' });
const promise3 = fn({ c: '3' });
await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

stream.next(
JSON.stringify({
Expand Down Expand Up @@ -364,7 +365,7 @@ describe('createStreamingBatchedFunction()', () => {
const promise1 = fn({ a: '1' });
const promise2 = fn({ b: '2' });
const promise3 = fn({ c: '3' });
await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

stream.next(
JSON.stringify({
Expand Down Expand Up @@ -404,7 +405,7 @@ describe('createStreamingBatchedFunction()', () => {
});

const promise = fn({ a: '1' });
await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

expect(await isPending(promise)).toBe(true);

Expand Down Expand Up @@ -436,7 +437,7 @@ describe('createStreamingBatchedFunction()', () => {
const promise2 = of(fn({ a: '2' }));
const promise3 = of(fn({ a: '3' }));

await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

stream.next(
JSON.stringify({
Expand All @@ -445,7 +446,7 @@ describe('createStreamingBatchedFunction()', () => {
}) + '\n'
);

await new Promise((r) => setTimeout(r, 1));
jest.advanceTimersByTime(1);

stream.next(
JSON.stringify({
Expand All @@ -454,7 +455,7 @@ describe('createStreamingBatchedFunction()', () => {
}) + '\n'
);

await new Promise((r) => setTimeout(r, 1));
jest.advanceTimersByTime(1);

stream.next(
JSON.stringify({
Expand All @@ -463,7 +464,7 @@ describe('createStreamingBatchedFunction()', () => {
}) + '\n'
);

await new Promise((r) => setTimeout(r, 1));
jest.advanceTimersByTime(1);

const [result1] = await promise1;
const [, error2] = await promise2;
Expand All @@ -488,13 +489,14 @@ describe('createStreamingBatchedFunction()', () => {
const abortController = new AbortController();
const promise = fn({ a: '1' }, abortController.signal);
const promise2 = fn({ a: '2' }, abortController.signal);
await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

expect(await isPending(promise)).toBe(true);
expect(await isPending(promise2)).toBe(true);

abortController.abort();
await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);
await flushPromises();

expect(await isPending(promise)).toBe(false);
expect(await isPending(promise2)).toBe(false);
Expand All @@ -518,12 +520,13 @@ describe('createStreamingBatchedFunction()', () => {
const abortController = new AbortController();
const promise = fn({ a: '1' }, abortController.signal);
const promise2 = fn({ a: '2' });
await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

expect(await isPending(promise)).toBe(true);

abortController.abort();
await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);
await flushPromises();

expect(await isPending(promise)).toBe(false);
const [, error] = await of(promise);
Expand All @@ -536,7 +539,7 @@ describe('createStreamingBatchedFunction()', () => {
}) + '\n'
);

await new Promise((r) => setTimeout(r, 1));
jest.advanceTimersByTime(1);

const [result2] = await of(promise2);
expect(result2).toEqual({ b: '2' });
Expand All @@ -557,11 +560,11 @@ describe('createStreamingBatchedFunction()', () => {
const promise1 = of(fn({ a: '1' }));
const promise2 = of(fn({ a: '2' }));

await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

stream.complete();

await new Promise((r) => setTimeout(r, 1));
jest.advanceTimersByTime(1);

const [, error1] = await promise1;
const [, error2] = await promise2;
Expand All @@ -588,7 +591,7 @@ describe('createStreamingBatchedFunction()', () => {
const promise1 = of(fn({ a: '1' }));
const promise2 = of(fn({ a: '2' }));

await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

stream.next(
JSON.stringify({
Expand All @@ -598,7 +601,7 @@ describe('createStreamingBatchedFunction()', () => {
);
stream.complete();

await new Promise((r) => setTimeout(r, 1));
jest.advanceTimersByTime(1);

const [, error1] = await promise1;
const [result1] = await promise2;
Expand Down Expand Up @@ -626,13 +629,13 @@ describe('createStreamingBatchedFunction()', () => {
const promise1 = of(fn({ a: '1' }));
const promise2 = of(fn({ a: '2' }));

await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

stream.error({
message: 'something went wrong',
});

await new Promise((r) => setTimeout(r, 1));
jest.advanceTimersByTime(1);

const [, error1] = await promise1;
const [, error2] = await promise2;
Expand All @@ -659,7 +662,7 @@ describe('createStreamingBatchedFunction()', () => {
const promise1 = of(fn({ a: '1' }));
const promise2 = of(fn({ a: '2' }));

await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

stream.next(
JSON.stringify({
Expand All @@ -669,7 +672,7 @@ describe('createStreamingBatchedFunction()', () => {
);
stream.error('oops');

await new Promise((r) => setTimeout(r, 1));
jest.advanceTimersByTime(1);

const [, error1] = await promise1;
const [result1] = await promise2;
Expand Down Expand Up @@ -697,7 +700,7 @@ describe('createStreamingBatchedFunction()', () => {
const promise1 = of(fn({ a: '1' }));
const promise2 = of(fn({ a: '2' }));

await new Promise((r) => setTimeout(r, 6));
jest.advanceTimersByTime(6);

stream.next(
JSON.stringify({
Expand All @@ -708,7 +711,7 @@ describe('createStreamingBatchedFunction()', () => {

stream.next('Not a JSON\n');

await new Promise((r) => setTimeout(r, 1));
jest.advanceTimersByTime(1);

const [, error1] = await promise1;
const [result1] = await promise2;
Expand Down