From 7351dda8a62f0b80fa3be3b388eddf03d002ebd1 Mon Sep 17 00:00:00 2001 From: Brendon Muir Date: Tue, 27 Aug 2024 12:01:40 +1200 Subject: [PATCH 1/2] Use window.Turbo.fetch if available for turbo-stream requests --- __tests__/fetch_request.js | 31 +++++++++++++++++++++++++++++++ src/fetch_request.js | 5 ++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/__tests__/fetch_request.js b/__tests__/fetch_request.js index f23efe1..649b84c 100644 --- a/__tests__/fetch_request.js +++ b/__tests__/fetch_request.js @@ -281,3 +281,34 @@ describe('query params are parsed', () => { expect(emptyQueryRequest.url).toBe("localhost/test") }) }) + + +describe('turbostream', () => { + test('turbo fetch is called for turbo-stream responseKind', async() => { + const mockResponse = new Response("success!", { status: 200 }) + + window.fetch = jest.fn().mockResolvedValue(mockResponse) + window.Turbo = { fetch: jest.fn().mockResolvedValue(mockResponse) } + + const testRequest = new FetchRequest("get", "localhost", { responseKind: 'turbo-stream' }) + const testResponse = await testRequest.perform() + + expect(window.Turbo.fetch).toHaveBeenCalledTimes(1) + expect(window.fetch).toHaveBeenCalledTimes(0) + expect(testResponse).toStrictEqual(new FetchResponse(mockResponse)) + }) + + test('turbo fetch is called for other responseKind', async() => { + const mockResponse = new Response("success!", { status: 200 }) + + window.fetch = jest.fn().mockResolvedValue(mockResponse) + window.Turbo = { fetch: jest.fn().mockResolvedValue(mockResponse) } + + const testRequest = new FetchRequest("get", "localhost") + const testResponse = await testRequest.perform() + + expect(window.Turbo.fetch).toHaveBeenCalledTimes(0) + expect(window.fetch).toHaveBeenCalledTimes(1) + expect(testResponse).toStrictEqual(new FetchResponse(mockResponse)) + }) +}) diff --git a/src/fetch_request.js b/src/fetch_request.js index 85ae308..c951883 100644 --- a/src/fetch_request.js +++ b/src/fetch_request.js @@ -19,7 +19,10 @@ export class FetchRequest { console.error(error) } - const response = new FetchResponse(await window.fetch(this.url, this.fetchOptions)) + const fetch = (this.responseKind === 'turbo-stream' && window.Turbo) + ? window.Turbo.fetch : window.fetch + + const response = new FetchResponse(await fetch(this.url, this.fetchOptions)) if (response.unauthenticated && response.authenticationURL) { return Promise.reject(window.location.href = response.authenticationURL) From f22d6e96f470a022adb8df0de6bfb212e98da304 Mon Sep 17 00:00:00 2001 From: Brendon Muir Date: Tue, 27 Aug 2024 12:05:51 +1200 Subject: [PATCH 2/2] Fix linting --- src/fetch_request.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/fetch_request.js b/src/fetch_request.js index c951883..a5f3038 100644 --- a/src/fetch_request.js +++ b/src/fetch_request.js @@ -20,7 +20,8 @@ export class FetchRequest { } const fetch = (this.responseKind === 'turbo-stream' && window.Turbo) - ? window.Turbo.fetch : window.fetch + ? window.Turbo.fetch + : window.fetch const response = new FetchResponse(await fetch(this.url, this.fetchOptions))