diff --git a/lib/graphql/fetch/create-fetch.spec.ts b/lib/graphql/fetch/create-fetch.spec.ts index fb883f3..04d255d 100644 --- a/lib/graphql/fetch/create-fetch.spec.ts +++ b/lib/graphql/fetch/create-fetch.spec.ts @@ -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'; @@ -27,10 +37,6 @@ describe('createFetch', () => { }, }; - afterEach(() => { - vi.restoreAllMocks(); - }); - beforeEach(() => { (fetch as Mock).mockImplementationOnce(() => ({ ok: true, @@ -38,100 +44,112 @@ describe('createFetch', () => { })); }); - 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); + }); }); });