Skip to content

Commit

Permalink
Merge pull request #4282 from Shopify/ah.fix-app-logs-rerendering
Browse files Browse the repository at this point in the history
Only reset errors if there are errors
  • Loading branch information
andrewhassan committed Aug 8, 2024
2 parents e253d4d + 4486ea6 commit 3148623
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -374,14 +374,14 @@ describe('usePollAppLogs', () => {
// needed to await the render
await vi.advanceTimersByTimeAsync(0)

// Initial invocation, 429 returned
// Initial invocation, 422 returned
expect(mockedPollAppLogs).toHaveBeenCalledTimes(1)

expect(hook.lastResult?.appLogOutputs).toHaveLength(0)
expect(hook.lastResult?.errors[0]).toEqual('Error while polling app logs')
expect(hook.lastResult?.errors[1]).toEqual('Retrying in 5s')

expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), POLLING_ERROR_RETRY_INTERVAL_MS)
expect(timeoutSpy).toHaveBeenCalledWith(expect.any(Function), POLLING_ERROR_RETRY_INTERVAL_MS)

await vi.advanceTimersToNextTimerAsync()
expect(hook.lastResult?.appLogOutputs).toHaveLength(6)
Expand All @@ -391,6 +391,30 @@ describe('usePollAppLogs', () => {
expect(vi.getTimerCount()).toEqual(1)
timeoutSpy.mockRestore()
})

test('clears error on success', async () => {
const mockedPollAppLogs = vi
.fn()
.mockResolvedValueOnce(POLL_APP_LOGS_FOR_LOGS_429_RESPONSE)
.mockResolvedValueOnce(POLL_APP_LOGS_FOR_LOGS_RESPONSE)
vi.mocked(pollAppLogs).mockImplementation(mockedPollAppLogs)

const hook = renderHook(() =>
usePollAppLogs({
initialJwt: MOCKED_JWT_TOKEN,
filters: EMPTY_FILTERS,
resubscribeCallback: vi.fn().mockResolvedValue(MOCKED_JWT_TOKEN),
}),
)

// initial poll with errors
await vi.advanceTimersByTimeAsync(0)
expect(hook.lastResult?.errors).toHaveLength(2)

// second poll with no errors
await vi.advanceTimersToNextTimerAsync()
expect(hook.lastResult?.errors).toHaveLength(0)
})
})

function renderHook<THookResult>(renderHookCallback: () => THookResult) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function usePollAppLogs({initialJwt, filters, resubscribeCallback}: UsePo
}
retryIntervalMs = result.retryIntervalMs
} else {
setErrors([])
setErrors((errors) => (errors.length ? [] : errors))
}

const {cursor: nextCursor, appLogs} = response as SuccessResponse
Expand Down

0 comments on commit 3148623

Please sign in to comment.