forked from cypress-io/cypress-react-unit-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror-boundary-spec.js
46 lines (41 loc) · 1.17 KB
/
error-boundary-spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/// <reference types="cypress" />
/// <reference types="../../lib" />
import { ErrorBoundary } from './error-boundary.jsx'
import React from 'react'
import { mount } from 'cypress-react-unit-test'
Cypress.on('uncaught:exception', (err, runnable) => {
return false
})
/* eslint-env mocha */
describe('Error Boundary', () => {
const errorMessage = 'I crashed!'
const ChildWithoutError = () => <h1>Normal Child</h1>
const ChildWithError = () => {
throw new Error(errorMessage)
return null
}
it('renders normal children', () => {
mount(
<ErrorBoundary>
<ChildWithoutError />
</ErrorBoundary>,
)
cy.get('h1').should('have.text', 'Normal Child')
cy.get(ErrorBoundary)
.its('state.error')
.should('not.exist')
})
it('on error, display fallback UI', () => {
try {
mount(
<ErrorBoundary name="ChildWithError">
<ChildWithError />
</ErrorBoundary>,
)
} catch (e) {
// do nothing
}
cy.get('header h1').should('contain', 'Something went wrong.')
cy.get('header h3').should('contain', 'ChildWithError failed to load')
})
})