Skip to content
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

Remove wait-for-observables in favor of ObservableStream #12317

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Changes from 1 commit
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
Next Next commit
Use ObservableStream instead of waitFor from wait-for-observables
  • Loading branch information
jerelmiller committed Jan 28, 2025
commit e8bd435e04f4e3fc7ab1ace26b6bd296a3f3f8f5
47 changes: 28 additions & 19 deletions src/link/retry/__tests__/retryLink.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import gql from "graphql-tag";
import waitFor from "wait-for-observables";

import { ApolloLink } from "../../core/ApolloLink";
import { execute } from "../../core/execute";
import { Observable } from "../../../utilities/observables/Observable";
import { fromError } from "../../utils/fromError";
import { RetryLink } from "../retryLink";
import { ObservableStream } from "../../../testing/internal";

const query = gql`
{
Expand All @@ -23,9 +23,10 @@ describe("RetryLink", () => {
const retry = new RetryLink({ delay: { initial: 1 }, attempts: { max } });
const stub = jest.fn(() => fromError(standardError)) as any;
const link = ApolloLink.from([retry, stub]);
const stream = new ObservableStream(execute(link, { query }));

await expect(stream).toEmitError(standardError, { timeout: 1000 });

const [{ error }] = (await waitFor(execute(link, { query }))) as any;
expect(error).toEqual(standardError);
expect(stub).toHaveBeenCalledTimes(max);
});

Expand All @@ -34,9 +35,11 @@ describe("RetryLink", () => {
const data = { data: { hello: "world" } };
const stub = jest.fn(() => Observable.of(data));
const link = ApolloLink.from([retry, stub]);
const stream = new ObservableStream(execute(link, { query }));

await expect(stream).toEmitValue(data);
await expect(stream).toComplete();

const [{ values }] = (await waitFor(execute(link, { query }))) as any;
expect(values).toEqual([data]);
expect(stub).toHaveBeenCalledTimes(1);
});

Expand All @@ -50,9 +53,11 @@ describe("RetryLink", () => {
stub.mockReturnValueOnce(fromError(standardError));
stub.mockReturnValueOnce(Observable.of(data));
const link = ApolloLink.from([retry, stub]);
const stream = new ObservableStream(execute(link, { query }));

await expect(stream).toEmitValue(data);
await expect(stream).toComplete();

const [{ values }] = (await waitFor(execute(link, { query }))) as any;
expect(values).toEqual([data]);
expect(stub).toHaveBeenCalledTimes(2);
});

Expand Down Expand Up @@ -129,13 +134,14 @@ describe("RetryLink", () => {
});
const stub = jest.fn(() => fromError(standardError)) as any;
const link = ApolloLink.from([retry, stub]);
const stream1 = new ObservableStream(execute(link, { query }));
const stream2 = new ObservableStream(execute(link, { query }));

await Promise.all([
expect(stream1).toEmitError(standardError),
expect(stream2).toEmitError(standardError),
]);

const [result1, result2] = (await waitFor(
execute(link, { query }),
execute(link, { query })
)) as any;
expect(result1.error).toEqual(standardError);
expect(result2.error).toEqual(standardError);
expect(stub).toHaveBeenCalledTimes(10);
});

Expand All @@ -144,9 +150,10 @@ describe("RetryLink", () => {
const retry = new RetryLink({ delay: delayStub, attempts: { max: 3 } });
const linkStub = jest.fn(() => fromError(standardError)) as any;
const link = ApolloLink.from([retry, linkStub]);
const [{ error }] = (await waitFor(execute(link, { query }))) as any;
const stream = new ObservableStream(execute(link, { query }));

await expect(stream).toEmitError(standardError);

expect(error).toEqual(standardError);
const operation = (delayStub.mock.calls[0] as any)[1];
expect(delayStub.mock.calls).toEqual([
[1, operation, standardError],
Expand All @@ -166,9 +173,10 @@ describe("RetryLink", () => {
});
const linkStub = jest.fn(() => fromError(standardError)) as any;
const link = ApolloLink.from([retry, linkStub]);
const [{ error }] = (await waitFor(execute(link, { query }))) as any;
const stream = new ObservableStream(execute(link, { query }));

await expect(stream).toEmitError(standardError);

expect(error).toEqual(standardError);
const operation = attemptStub.mock.calls[0][1];
expect(attemptStub.mock.calls).toEqual([
[1, operation, standardError],
Expand All @@ -191,9 +199,10 @@ describe("RetryLink", () => {
() => new Observable((o) => o.error(standardError))
) as any;
const link = ApolloLink.from([retry, linkStub]);
const [{ error }] = (await waitFor(execute(link, { query }))) as any;
const stream = new ObservableStream(execute(link, { query }));

await expect(stream).toEmitError(standardError);

expect(error).toEqual(standardError);
const operation = attemptStub.mock.calls[0][1];
expect(attemptStub.mock.calls).toEqual([
[1, operation, standardError],
Expand Down