Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 4 additions & 5 deletions __tests__/fetch_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ describe('query params are parsed', () => {


describe('turbostream', () => {
test('turbo fetch is called for turbo-stream responseKind', async() => {
test('turbo fetch is called when available', async() => {
const mockResponse = new Response("success!", { status: 200 })

window.fetch = jest.fn().mockResolvedValue(mockResponse)
Expand All @@ -326,16 +326,15 @@ describe('turbostream', () => {
expect(testResponse).toStrictEqual(new FetchResponse(mockResponse))
})

test('turbo fetch is called for other responseKind', async() => {
test('turbo fetch is not called when not available', async() => {
const mockResponse = new Response("success!", { status: 200 })

window.fetch = jest.fn().mockResolvedValue(mockResponse)
window.Turbo = { fetch: jest.fn().mockResolvedValue(mockResponse) }
window.Turbo = undefined

const testRequest = new FetchRequest("get", "localhost")
const testRequest = new FetchRequest("get", "localhost", { responseKind: 'turbo-stream' })
const testResponse = await testRequest.perform()

expect(window.Turbo.fetch).toHaveBeenCalledTimes(0)
expect(window.fetch).toHaveBeenCalledTimes(1)
expect(testResponse).toStrictEqual(new FetchResponse(mockResponse))
})
Expand Down
5 changes: 1 addition & 4 deletions src/fetch_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ export class FetchRequest {
console.error(error)
}

const fetch = (this.responseKind === 'turbo-stream' && window.Turbo)
? window.Turbo.fetch
: window.fetch

const fetch = window.Turbo ? window.Turbo.fetch : window.fetch
const response = new FetchResponse(await fetch(this.url, this.fetchOptions))

if (response.unauthenticated && response.authenticationURL) {
Expand Down
Loading