Skip to content

Commit

Permalink
fix: use fresh client independent of isomorphic fetch
Browse files Browse the repository at this point in the history
resolves #482
  • Loading branch information
danielroe committed Sep 23, 2022
1 parent 53fb5db commit c906850
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/runtime/client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { $fetch } from 'ohmyfetch'

/**
* Adapted from https://github.com/rexxars/picosanity
*/
Expand Down
31 changes: 15 additions & 16 deletions test/unit/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { beforeEach, afterEach, describe, it, vi, expect } from 'vitest'
import { afterEach, describe, it, vi, expect } from 'vitest'
import { $fetch } from 'ohmyfetch'
import { getQuery, createClient } from '../../src/runtime/client'
import { request as largeRequest } from './fixture/large-request.json'

vi.mock('ohmyfetch', () => ({
$fetch: vi.fn(() =>
Promise.resolve({ result: [1, 2] }),
),
}))

describe('minimal sanity client', () => {
let mockFetch
beforeEach(() => {
mockFetch = vi.fn(() =>
Promise.resolve([1, 2]),
)
// @ts-ignore
global.$fetch = mockFetch
})
afterEach(() => {
vi.clearAllMocks()
})
Expand Down Expand Up @@ -47,7 +46,7 @@ describe('minimal sanity client', () => {
})
client.fetch('*[_type == "article"')

expect(mockFetch).toBeCalledWith(
expect($fetch).toBeCalledWith(
'https://sample-project.api.sanity.io/v1/data/query/undefined?query=*%5B_type%20%3D%3D%20%22article%22',
expect.not.objectContaining({ method: 'post' }),
)
Expand All @@ -60,7 +59,7 @@ describe('minimal sanity client', () => {
})
client.fetch(largeRequest)

expect(mockFetch).toBeCalledWith(
expect($fetch).toBeCalledWith(
'https://sample-project.api.sanity.io/v1/data/query/undefined',
expect.objectContaining({ method: 'post' }),
)
Expand Down Expand Up @@ -90,7 +89,7 @@ describe('minimal sanity client', () => {
})
await client.fetch('*[_type == "article"]')

expect(mockFetch).toBeCalledWith(
expect($fetch).toBeCalledWith(
expect.stringContaining(`https://${project}.api.sanity.io`),
defaultOptions,
)
Expand All @@ -104,7 +103,7 @@ describe('minimal sanity client', () => {
})
await client.fetch('*[_type == "article"]')

expect(mockFetch).toBeCalledWith(
expect($fetch).toBeCalledWith(
expect.stringContaining(`https://${project}.apicdn.sanity.io`),
defaultOptions,
)
Expand All @@ -120,7 +119,7 @@ describe('minimal sanity client', () => {
})
await client.fetch('*[_type == "article"]')

expect(mockFetch).toBeCalledWith(
expect($fetch).toBeCalledWith(
expect.stringContaining(`https://${project}.api.sanity.io`),
{
credentials: 'include',
Expand All @@ -141,7 +140,7 @@ describe('minimal sanity client', () => {
})
await client.fetch('*[_type == "article"]')

expect(mockFetch).toBeCalledWith(
expect($fetch).toBeCalledWith(
expect.stringContaining(`https://${project}.api.sanity.io`),
{
credentials: 'omit',
Expand All @@ -162,7 +161,7 @@ describe('minimal sanity client', () => {
})
await client.fetch('*[_type == "article"]')

expect(mockFetch).toBeCalledWith(
expect($fetch).toBeCalledWith(
expect.stringContaining(`https://${project}.api.sanity.io/v2021-03-25`),
{
credentials: 'omit',
Expand Down

0 comments on commit c906850

Please sign in to comment.