-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
709b8fc
commit 545bb9b
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
tests/component/sections/error-page/useErrorLogger.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { renderHook } from '@testing-library/react' | ||
import { useErrorLogger } from '../../../../src/sections/error-page/useErrorLogger' | ||
|
||
describe('useErrorLogger', () => { | ||
beforeEach(() => { | ||
cy.window().then((win) => { | ||
cy.stub(win.console, 'error').as('consoleError') | ||
}) | ||
}) | ||
|
||
it('should only log the same errors once when multiple occur, and log different errors seperately', () => { | ||
const error = new Error('Test error') | ||
|
||
cy.window().then(() => { | ||
const { rerender } = renderHook( | ||
({ error }: { error: Error | null }) => useErrorLogger(error), | ||
{ initialProps: { error } } | ||
) | ||
cy.get('@consoleError').should('have.been.calledOnceWith', 'Error:', 'Test error') | ||
|
||
cy.then(() => { | ||
rerender({ error }) | ||
}) | ||
cy.get('@consoleError').should('have.been.calledOnce') | ||
|
||
const newError = new Error('Another error') | ||
cy.then(() => { | ||
rerender({ error: newError }) | ||
}) | ||
|
||
cy.get('@consoleError').should('have.been.calledWith', 'Error:', 'Another error') | ||
}) | ||
}) | ||
|
||
it('should not log anything if no error is provided', () => { | ||
cy.window().then(() => { | ||
const { rerender } = renderHook( | ||
({ error }: { error: Error | null }) => useErrorLogger(error), | ||
{ initialProps: { error: null } } | ||
) | ||
|
||
cy.get('@consoleError').should('not.have.been.called') | ||
cy.then(() => { | ||
rerender({ error: null }) | ||
}) | ||
cy.get('@consoleError').should('not.have.been.called') | ||
}) | ||
}) | ||
|
||
it('should be used to log errors manually only once for the same error, and log different errors seperately', () => { | ||
cy.window().then(() => { | ||
const { result } = renderHook(() => useErrorLogger(null)) | ||
|
||
cy.then(() => { | ||
result.current(new Error('Manually logged error')) | ||
}) | ||
|
||
cy.get('@consoleError').should('have.been.calledOnceWith', 'Error:', 'Manually logged error') | ||
|
||
cy.then(() => { | ||
result.current(new Error('Manually logged error')) | ||
}) | ||
|
||
cy.get('@consoleError').should('have.been.calledOnce') | ||
|
||
cy.then(() => { | ||
result.current(new Error('Another manually logged error')) | ||
}) | ||
|
||
cy.get('@consoleError').should( | ||
'have.been.calledWith', | ||
'Error:', | ||
'Another manually logged error' | ||
) | ||
}) | ||
}) | ||
}) |