Skip to content

Commit

Permalink
test: ensure types are inferred correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
sf-twingate committed Apr 26, 2024
1 parent a866e5b commit ac6995e
Showing 1 changed file with 95 additions and 49 deletions.
144 changes: 95 additions & 49 deletions src/testing/core/mocking/__tests__/mockLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,112 @@ import gql from "graphql-tag";
import { MockLink, MockedResponse } from "../mockLink";
import { execute } from "../../../../link/core/execute";

describe("MockedResponse.newData", () => {
const setup = () => {
const weaklyTypedMockResponse: MockedResponse = {
request: {
query: gql`
query A {
a
}
`,
},
};
describe("MockedResponse", () => {
test("infers variable types correctly", () => {
function setup<M extends MockedResponse[]>(props: { m: M }) {
const { m } = props;

return {
m,
};
}

const stronglyTypedMockResponse: MockedResponse<
{ a: string },
{ input: string }
> = {
request: {
query: gql`
query A {
a
}
`,
},
};
setup<
[
MockedResponse<{ a: string }, { a: string }>,
MockedResponse<{ b: string }, { b: string }>,
]
>({
m: [
{
request: {
query: gql`
query A {
a
}
`,
variables: {
a: "a",
},
},
},
{
request: {
query: gql`
query B {
b
}
`,
variables: {
b: "b",
},
},
},
],
});
});

describe("newData", () => {
const setup = () => {
const weaklyTypedMockResponse: MockedResponse = {
request: {
query: gql`
query A {
a
}
`,
},
};

return {
weaklyTypedMockResponse,
stronglyTypedMockResponse,
const stronglyTypedMockResponse: MockedResponse<
{ a: string },
{ input: string }
> = {
request: {
query: gql`
query A {
a
}
`,
},
};

return {
weaklyTypedMockResponse,
stronglyTypedMockResponse,
};
};
};

test("returned 'data' can be any object with untyped response", () => {
const { weaklyTypedMockResponse } = setup();
test("returned 'data' can be any object with untyped response", () => {
const { weaklyTypedMockResponse } = setup();

weaklyTypedMockResponse.newData = ({ fake: { faker } }) => ({
data: {
pretend: faker,
},
weaklyTypedMockResponse.newData = ({ fake: { faker } }) => ({
data: {
pretend: faker,
},
});
});
});

test("can't return output that doesn't match TData", () => {
const { stronglyTypedMockResponse } = setup();
test("can't return output that doesn't match TData", () => {
const { stronglyTypedMockResponse } = setup();

// @ts-expect-error return type does not match `TData`
stronglyTypedMockResponse.newData = () => ({
data: {
a: 123,
},
// @ts-expect-error return type does not match `TData`
stronglyTypedMockResponse.newData = () => ({
data: {
a: 123,
},
});
});
});

test("can't use input variables that don't exist in TVariables", () => {
const { stronglyTypedMockResponse } = setup();
test("can't use input variables that don't exist in TVariables", () => {
const { stronglyTypedMockResponse } = setup();

// @ts-expect-error unknown variables
stronglyTypedMockResponse.newData = ({ fake: { faker } }) => ({
data: {
a: faker,
},
// @ts-expect-error unknown variables
stronglyTypedMockResponse.newData = ({ fake: { faker } }) => ({
data: {
a: faker,
},
});
});
});
});
Expand Down

0 comments on commit ac6995e

Please sign in to comment.