Skip to content

Commit ab6e19f

Browse files
authored
fix: compare length of resetKeys when checking if resetKeys has changed (#56)
* Compare length of resetKeys when checking if resetKeys has changed * Split test into more logical chunks
1 parent bc4b628 commit ab6e19f

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

src/__tests__/index.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,15 @@ test('requires either a fallback, fallbackRender, or FallbackComponent', () => {
251251
console.error.mockClear()
252252
})
253253

254+
// eslint-disable-next-line max-statements
254255
test('supports automatic reset of error boundary when resetKeys change', () => {
255256
const handleReset = jest.fn()
256257
const TRY_AGAIN_ARG1 = 'TRY_AGAIN_ARG1'
257258
const TRY_AGAIN_ARG2 = 'TRY_AGAIN_ARG2'
258259
const handleResetKeysChange = jest.fn()
259260
function App() {
260261
const [explode, setExplode] = React.useState(false)
262+
const [extra, setExtra] = React.useState(false)
261263
return (
262264
<div>
263265
<button onClick={() => setExplode(e => !e)}>toggle explode</button>
@@ -271,16 +273,19 @@ test('supports automatic reset of error boundary when resetKeys change', () => {
271273
>
272274
Try again
273275
</button>
276+
<button onClick={() => setExtra(e => !e)}>
277+
toggle extra resetKey
278+
</button>
274279
</div>
275280
)}
276281
onReset={(...args) => {
277282
setExplode(false)
278283
handleReset(...args)
279284
}}
280285
onResetKeysChange={handleResetKeysChange}
281-
resetKeys={[explode]}
286+
resetKeys={extra ? [explode, extra] : [explode]}
282287
>
283-
{explode ? <Bomb /> : null}
288+
{explode || extra ? <Bomb /> : null}
284289
</ErrorBoundary>
285290
</div>
286291
)
@@ -316,6 +321,46 @@ test('supports automatic reset of error boundary when resetKeys change', () => {
316321
expect(handleReset).not.toHaveBeenCalled()
317322
expect(screen.queryByRole('alert')).not.toBeInTheDocument()
318323
expect(console.error).not.toHaveBeenCalled()
324+
325+
// blow it up again
326+
userEvent.click(screen.getByText('toggle explode'))
327+
screen.getByRole('alert')
328+
expect(console.error).toHaveBeenCalledTimes(2)
329+
console.error.mockClear()
330+
331+
// toggles adding an extra resetKey to the array
332+
// expect error to re-render
333+
userEvent.click(screen.getByText('toggle extra resetKey'))
334+
expect(handleResetKeysChange).toHaveBeenCalledTimes(1)
335+
expect(handleResetKeysChange).toHaveBeenCalledWith([true], [true, true])
336+
handleResetKeysChange.mockClear()
337+
screen.getByRole('alert')
338+
expect(console.error).toHaveBeenCalledTimes(2)
339+
console.error.mockClear()
340+
341+
// toggle explode back to false
342+
// expect error to re-render again
343+
userEvent.click(screen.getByText('toggle explode'))
344+
expect(handleReset).not.toHaveBeenCalled()
345+
expect(handleResetKeysChange).toHaveBeenCalledTimes(1)
346+
expect(handleResetKeysChange).toHaveBeenCalledWith(
347+
[true, true],
348+
[false, true],
349+
)
350+
screen.getByRole('alert')
351+
handleResetKeysChange.mockClear()
352+
expect(console.error).toHaveBeenCalledTimes(2)
353+
console.error.mockClear()
354+
355+
// toggle extra resetKey
356+
// expect error to be reset
357+
userEvent.click(screen.getByText('toggle extra resetKey'))
358+
expect(handleReset).not.toHaveBeenCalled()
359+
expect(handleResetKeysChange).toHaveBeenCalledTimes(1)
360+
expect(handleResetKeysChange).toHaveBeenCalledWith([false, true], [false])
361+
handleResetKeysChange.mockClear()
362+
expect(screen.queryByRole('alert')).not.toBeInTheDocument()
363+
expect(console.error).not.toHaveBeenCalled()
319364
})
320365

321366
/*

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react'
22

33
const changedArray = (a = [], b = []) =>
4-
a.some((item, index) => !Object.is(item, b[index]))
4+
a.length !== b.length || a.some((item, index) => !Object.is(item, b[index]))
55

66
const initialState = {error: null, info: null}
77
class ErrorBoundary extends React.Component {

0 commit comments

Comments
 (0)