Skip to content

Commit

Permalink
fix: ensure logic does not fail with empty response pre v40
Browse files Browse the repository at this point in the history
  • Loading branch information
kabaros committed Sep 10, 2024
1 parent 430e3ca commit f589122
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
11 changes: 9 additions & 2 deletions src/pages/ManualInstall/ManualInstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,17 @@ const UploadButton = () => {
setIsUploading(true)
try {
const response = await uploadApp(event.target.files[0])
const body = await response.json()

// using response.text() rather .json() to avoid breaking in <v40
// where the API returned empty response which throws with .json()
const responseText = await response.text()
const appHubId = responseText
? JSON.parse(responseText)?.app_hub_id
: null

formEl.current.reset()

successAlert.show({ id: body?.app_hub_id })
successAlert.show({ id: appHubId })
} catch (error) {
errorAlert.show({ error })
}
Expand Down
13 changes: 6 additions & 7 deletions src/pages/ManualInstall/ManualInstall.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ describe('Manual Install', () => {

it('should allow navigating to the app', async () => {
jest.spyOn(global, 'fetch').mockResolvedValueOnce({
json: () => Promise.resolve({ app_hub_id: 'some_apphub_id' }),
text: () =>
Promise.resolve(
JSON.stringify({ app_hub_id: 'some_apphub_id' })
),
})

const { getByTestId, getByText, findByText } = renderWithProvider(
Expand All @@ -57,7 +60,7 @@ describe('Manual Install', () => {

it('should work with an empty response (pre v41)', async () => {
jest.spyOn(global, 'fetch').mockResolvedValueOnce({
json: () => Promise.resolve(),
text: () => null,
})

const { getByTestId, findByText, queryByText } = renderWithProvider(
Expand All @@ -73,11 +76,7 @@ describe('Manual Install', () => {
})

it('should show an error if it fails', async () => {
jest.spyOn(global, 'fetch').mockResolvedValueOnce({
json: () => {
throw 'upload failed'
},
})
jest.spyOn(global, 'fetch').mockRejectedValue('upload failed')

const { getByTestId, findByText, queryByText } = renderWithProvider(
<ManualInstall />
Expand Down

0 comments on commit f589122

Please sign in to comment.