Skip to content

Commit 8320b8d

Browse files
committed
Always use Turbo.fetch when available, not just in turbo-stream requests
1 parent 83620bb commit 8320b8d

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

__tests__/fetch_request.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ describe('query params are parsed', () => {
312312

313313

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

318318
window.fetch = jest.fn().mockResolvedValue(mockResponse)
@@ -326,13 +326,13 @@ describe('turbostream', () => {
326326
expect(testResponse).toStrictEqual(new FetchResponse(mockResponse))
327327
})
328328

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

332332
window.fetch = jest.fn().mockResolvedValue(mockResponse)
333-
window.Turbo = { fetch: jest.fn().mockResolvedValue(mockResponse) }
333+
window.Turbo = undefined
334334

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

338338
expect(window.Turbo.fetch).toHaveBeenCalledTimes(0)

src/fetch_request.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ export class FetchRequest {
1919
console.error(error)
2020
}
2121

22-
const fetch = (this.responseKind === 'turbo-stream' && window.Turbo)
23-
? window.Turbo.fetch
24-
: window.fetch
25-
22+
const fetch = window.Turbo ? window.Turbo.fetch : window.fetch
2623
const response = new FetchResponse(await fetch(this.url, this.fetchOptions))
2724

2825
if (response.unauthenticated && response.authenticationURL) {

0 commit comments

Comments
 (0)