Skip to content

Commit 4821f5c

Browse files
authored
Merge pull request #56 from KirilSoshnin/allow-unprocessable-entity-turbo-stream
Allow turbo streams for unprocessable entity responses
2 parents 85d16d3 + ac5a7b0 commit 4821f5c

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

__tests__/fetch_request.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('perform', () => {
4444
})
4545
})
4646

47-
test('turbo stream request automatically calls renderTurboStream', async () => {
47+
test('turbo stream request automatically calls renderTurboStream when status is ok', async () => {
4848
const mockResponse = new Response('', { status: 200, headers: { 'Content-Type': 'text/vnd.turbo-stream.html' }})
4949
window.fetch = jest.fn().mockResolvedValue(mockResponse)
5050
jest.spyOn(FetchResponse.prototype, "ok", "get").mockReturnValue(true)
@@ -55,6 +55,21 @@ describe('perform', () => {
5555
await testRequest.perform()
5656

5757
expect(renderSpy).toHaveBeenCalledTimes(1)
58+
jest.clearAllMocks();
59+
})
60+
61+
test('turbo stream request automatically calls renderTurboStream when status is unprocessable entity', async () => {
62+
const mockResponse = new Response('', { status: 422, headers: { 'Content-Type': 'text/vnd.turbo-stream.html' }})
63+
window.fetch = jest.fn().mockResolvedValue(mockResponse)
64+
jest.spyOn(FetchResponse.prototype, "ok", "get").mockReturnValue(true)
65+
jest.spyOn(FetchResponse.prototype, "isTurboStream", "get").mockReturnValue(true)
66+
const renderSpy = jest.spyOn(FetchResponse.prototype, "renderTurboStream").mockImplementation()
67+
68+
const testRequest = new FetchRequest("get", "localhost")
69+
await testRequest.perform()
70+
71+
expect(renderSpy).toHaveBeenCalledTimes(1)
72+
jest.clearAllMocks();
5873
})
5974
})
6075

src/fetch_request.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ export class FetchRequest {
2525
return Promise.reject(window.location.href = response.authenticationURL)
2626
}
2727

28-
if (response.ok && response.isTurboStream) {
28+
const responseStatusIsTurboStreamable = response.ok || response.unprocessableEntity
29+
30+
if (responseStatusIsTurboStreamable && response.isTurboStream) {
2931
await response.renderTurboStream()
3032
}
3133

0 commit comments

Comments
 (0)