Skip to content

Commit

Permalink
Update fetch calls and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Gordon committed May 20, 2024
1 parent 897b77d commit 8ccc57d
Showing 1 changed file with 97 additions and 79 deletions.
176 changes: 97 additions & 79 deletions lib/graphql/fetch/create-fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ import { createFetch } from './create-fetch';

global.fetch = vi.fn();

vi.mock('next/headers', () => {
return {
draftMode: vi.fn(() => draftModeMock),
};
});

const draftModeMock = {
isEnabled: false,
};

describe('createFetch', () => {
const spaceId = 'testSpaceId';
const accessToken = 'testAccessToken';
Expand All @@ -27,111 +37,119 @@ describe('createFetch', () => {
},
};

afterEach(() => {
vi.restoreAllMocks();
});

beforeEach(() => {
(fetch as Mock).mockImplementationOnce(() => ({
ok: true,
json: vi.fn(() => new Promise((resolve) => resolve({}))),
}));
});

it('should call fetch with correct arguments for regular request', async () => {
const expectedArgs = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({
afterEach(() => {
vi.restoreAllMocks();
draftModeMock.isEnabled = false;
});

describe('preview', () => {
it('should call fetch with correct arguments for regular request', async () => {
const expectedArgs = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({
query: request.query,
variables: request.variables,
}),
cache: 'force-cache',
};

await fetchGraphQL({
query: request.query,
variables: request.variables,
}),
cache: 'force-cache',
};
});

await fetchGraphQL({
query: request.query,
variables: request.variables,
expect(fetch).toHaveBeenCalledWith(graphqlEndpoint, expectedArgs);
});

expect(fetch).toHaveBeenCalledWith(graphqlEndpoint, expectedArgs);
});

it('should call fetch with correct arguments for preview request', async () => {
const expectedArgs = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${previewToken}`,
},
body: JSON.stringify({
it('should call fetch with correct arguments for preview request', async () => {
draftModeMock.isEnabled = true;

const expectedArgs = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${previewToken}`,
},
body: JSON.stringify({
query: request.query,
variables: request.variables,
}),
cache: 'no-store',
};

await fetchGraphQL({
query: request.query,
variables: request.variables,
}),
cache: 'no-store',
};

await fetchGraphQL({
query: request.query,
variables: request.variables,
preview: true,
});
});

expect(fetch).toHaveBeenCalledWith(graphqlEndpoint, expectedArgs);
expect(fetch).toHaveBeenCalledWith(graphqlEndpoint, expectedArgs);
});
});

it('should call fetch with correct arguments for tags', async () => {
const expectedArgs = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({
describe('tags', () => {
it('should call fetch with correct arguments for tags', async () => {
const expectedArgs = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({
query: request.query,
variables: request.variables,
}),
next: {
tags: ['post'],
},
cache: 'force-cache',
};

await fetchGraphQL({
query: request.query,
variables: request.variables,
}),
next: {
tags: ['post'],
},
cache: 'force-cache',
};

await fetchGraphQL({
query: request.query,
variables: request.variables,
tags: ['post'],
});
});

expect(fetch).toHaveBeenCalledWith(graphqlEndpoint, expectedArgs);
expect(fetch).toHaveBeenCalledWith(graphqlEndpoint, expectedArgs);
});
});

it('should call fetch with correct arguments for revalidate', async () => {
const expectedArgs = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({
describe('revalidate', () => {
it('should call fetch with correct arguments for revalidate', async () => {
const expectedArgs = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({
query: request.query,
variables: request.variables,
}),
next: {
revalidate: 5,
},
cache: 'force-cache',
};

await fetchGraphQL({
query: request.query,
variables: request.variables,
}),
next: {
revalidate: 5,
},
cache: 'force-cache',
};

await fetchGraphQL({
query: request.query,
variables: request.variables,
revalidate: 5,
});
});

expect(fetch).toHaveBeenCalledWith(graphqlEndpoint, expectedArgs);
expect(fetch).toHaveBeenCalledWith(graphqlEndpoint, expectedArgs);
});
});
});

0 comments on commit 8ccc57d

Please sign in to comment.