parent
b82fb93695
commit
9257e0642f
@ -1,76 +0,0 @@
|
||||
let asyncAct
|
||||
|
||||
jest.mock('react-dom/test-utils', () => ({
|
||||
act: cb => {
|
||||
return cb()
|
||||
},
|
||||
}))
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetModules()
|
||||
asyncAct = require('../act-compat').asyncAct
|
||||
jest.spyOn(console, 'error').mockImplementation(() => {})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
console.error.mockRestore()
|
||||
})
|
||||
|
||||
test('async act works when it does not exist (older versions of react)', async () => {
|
||||
const callback = jest.fn()
|
||||
await asyncAct(async () => {
|
||||
await Promise.resolve()
|
||||
await callback()
|
||||
})
|
||||
expect(console.error).toHaveBeenCalledTimes(0)
|
||||
expect(callback).toHaveBeenCalledTimes(1)
|
||||
|
||||
callback.mockClear()
|
||||
console.error.mockClear()
|
||||
|
||||
await asyncAct(async () => {
|
||||
await Promise.resolve()
|
||||
await callback()
|
||||
})
|
||||
expect(console.error).toHaveBeenCalledTimes(0)
|
||||
expect(callback).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
test('async act recovers from errors', async () => {
|
||||
try {
|
||||
await asyncAct(async () => {
|
||||
await null
|
||||
throw new Error('test error')
|
||||
})
|
||||
} catch (err) {
|
||||
console.error('call console.error')
|
||||
}
|
||||
expect(console.error).toHaveBeenCalledTimes(1)
|
||||
expect(console.error.mock.calls).toMatchInlineSnapshot(`
|
||||
Array [
|
||||
Array [
|
||||
"call console.error",
|
||||
],
|
||||
]
|
||||
`)
|
||||
})
|
||||
|
||||
test('async act recovers from sync errors', async () => {
|
||||
try {
|
||||
await asyncAct(() => {
|
||||
throw new Error('test error')
|
||||
})
|
||||
} catch (err) {
|
||||
console.error('call console.error')
|
||||
}
|
||||
expect(console.error).toHaveBeenCalledTimes(1)
|
||||
expect(console.error.mock.calls).toMatchInlineSnapshot(`
|
||||
Array [
|
||||
Array [
|
||||
"call console.error",
|
||||
],
|
||||
]
|
||||
`)
|
||||
})
|
||||
|
||||
/* eslint no-console:0 */
|
@ -1,80 +0,0 @@
|
||||
let act, asyncAct
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetModules()
|
||||
act = require('..').act
|
||||
asyncAct = require('../act-compat').asyncAct
|
||||
jest.spyOn(console, 'error').mockImplementation(() => {})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
console.error.mockRestore()
|
||||
})
|
||||
|
||||
jest.mock('react-dom/test-utils', () => ({}))
|
||||
|
||||
test('act works even when there is no act from test utils', () => {
|
||||
const callback = jest.fn()
|
||||
act(callback)
|
||||
expect(callback).toHaveBeenCalledTimes(1)
|
||||
expect(console.error).toHaveBeenCalledTimes(0)
|
||||
})
|
||||
|
||||
test('async act works when it does not exist (older versions of react)', async () => {
|
||||
const callback = jest.fn()
|
||||
await asyncAct(async () => {
|
||||
await Promise.resolve()
|
||||
await callback()
|
||||
})
|
||||
expect(console.error).toHaveBeenCalledTimes(0)
|
||||
expect(callback).toHaveBeenCalledTimes(1)
|
||||
|
||||
callback.mockClear()
|
||||
console.error.mockClear()
|
||||
|
||||
await asyncAct(async () => {
|
||||
await Promise.resolve()
|
||||
await callback()
|
||||
})
|
||||
expect(console.error).toHaveBeenCalledTimes(0)
|
||||
expect(callback).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
test('async act recovers from errors', async () => {
|
||||
try {
|
||||
await asyncAct(async () => {
|
||||
await null
|
||||
throw new Error('test error')
|
||||
})
|
||||
} catch (err) {
|
||||
console.error('call console.error')
|
||||
}
|
||||
expect(console.error).toHaveBeenCalledTimes(1)
|
||||
expect(console.error.mock.calls).toMatchInlineSnapshot(`
|
||||
Array [
|
||||
Array [
|
||||
"call console.error",
|
||||
],
|
||||
]
|
||||
`)
|
||||
})
|
||||
|
||||
test('async act recovers from sync errors', async () => {
|
||||
try {
|
||||
await asyncAct(() => {
|
||||
throw new Error('test error')
|
||||
})
|
||||
} catch (err) {
|
||||
console.error('call console.error')
|
||||
}
|
||||
expect(console.error).toHaveBeenCalledTimes(1)
|
||||
expect(console.error.mock.calls).toMatchInlineSnapshot(`
|
||||
Array [
|
||||
Array [
|
||||
"call console.error",
|
||||
],
|
||||
]
|
||||
`)
|
||||
})
|
||||
|
||||
/* eslint no-console:0 */
|
@ -1,142 +0,0 @@
|
||||
let asyncAct
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetModules()
|
||||
asyncAct = require('../act-compat').asyncAct
|
||||
jest.spyOn(console, 'error').mockImplementation(() => {})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
console.error.mockRestore()
|
||||
})
|
||||
|
||||
jest.mock('react-dom/test-utils', () => ({
|
||||
act: cb => {
|
||||
cb()
|
||||
return {
|
||||
then() {
|
||||
console.error(
|
||||
'Warning: Do not await the result of calling ReactTestUtils.act(...), it is not a Promise.',
|
||||
)
|
||||
},
|
||||
}
|
||||
},
|
||||
}))
|
||||
|
||||
test('async act works even when the act is an old one', async () => {
|
||||
const callback = jest.fn()
|
||||
await asyncAct(async () => {
|
||||
console.error('sigil')
|
||||
await Promise.resolve()
|
||||
await callback()
|
||||
console.error('sigil')
|
||||
})
|
||||
expect(console.error.mock.calls).toMatchInlineSnapshot(`
|
||||
Array [
|
||||
Array [
|
||||
"sigil",
|
||||
],
|
||||
Array [
|
||||
"It looks like you're using a version of react-dom that supports the \\"act\\" function, but not an awaitable version of \\"act\\" which you will need. Please upgrade to at least react-dom@16.9.0 to remove this warning.",
|
||||
],
|
||||
Array [
|
||||
"sigil",
|
||||
],
|
||||
]
|
||||
`)
|
||||
expect(callback).toHaveBeenCalledTimes(1)
|
||||
|
||||
// and it doesn't warn you twice
|
||||
callback.mockClear()
|
||||
console.error.mockClear()
|
||||
|
||||
await asyncAct(async () => {
|
||||
await Promise.resolve()
|
||||
await callback()
|
||||
})
|
||||
expect(console.error).toHaveBeenCalledTimes(0)
|
||||
expect(callback).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
test('async act recovers from async errors', async () => {
|
||||
try {
|
||||
await asyncAct(async () => {
|
||||
await null
|
||||
throw new Error('test error')
|
||||
})
|
||||
} catch (err) {
|
||||
console.error('call console.error')
|
||||
}
|
||||
expect(console.error).toHaveBeenCalledTimes(2)
|
||||
expect(console.error.mock.calls).toMatchInlineSnapshot(`
|
||||
Array [
|
||||
Array [
|
||||
"It looks like you're using a version of react-dom that supports the \\"act\\" function, but not an awaitable version of \\"act\\" which you will need. Please upgrade to at least react-dom@16.9.0 to remove this warning.",
|
||||
],
|
||||
Array [
|
||||
"call console.error",
|
||||
],
|
||||
]
|
||||
`)
|
||||
})
|
||||
|
||||
test('async act recovers from sync errors', async () => {
|
||||
try {
|
||||
await asyncAct(() => {
|
||||
throw new Error('test error')
|
||||
})
|
||||
} catch (err) {
|
||||
console.error('call console.error')
|
||||
}
|
||||
expect(console.error).toHaveBeenCalledTimes(1)
|
||||
expect(console.error.mock.calls).toMatchInlineSnapshot(`
|
||||
Array [
|
||||
Array [
|
||||
"call console.error",
|
||||
],
|
||||
]
|
||||
`)
|
||||
})
|
||||
|
||||
test('async act can handle any sort of console.error', async () => {
|
||||
await asyncAct(async () => {
|
||||
console.error({error: 'some error'})
|
||||
await null
|
||||
})
|
||||
|
||||
expect(console.error).toHaveBeenCalledTimes(2)
|
||||
expect(console.error.mock.calls).toMatchInlineSnapshot(`
|
||||
Array [
|
||||
Array [
|
||||
Object {
|
||||
"error": "some error",
|
||||
},
|
||||
],
|
||||
Array [
|
||||
"It looks like you're using a version of react-dom that supports the \\"act\\" function, but not an awaitable version of \\"act\\" which you will need. Please upgrade to at least react-dom@16.9.0 to remove this warning.",
|
||||
],
|
||||
]
|
||||
`)
|
||||
})
|
||||
|
||||
test('async act should not show an error when ReactTestUtils.act returns something', async () => {
|
||||
jest.resetModules()
|
||||
jest.mock('react-dom/test-utils', () => ({
|
||||
act: () => {
|
||||
return new Promise(resolve => {
|
||||
console.error(
|
||||
'Warning: The callback passed to ReactTestUtils.act(...) function must not return anything',
|
||||
)
|
||||
resolve()
|
||||
})
|
||||
},
|
||||
}))
|
||||
asyncAct = require('../act-compat').asyncAct
|
||||
await asyncAct(async () => {
|
||||
await null
|
||||
})
|
||||
|
||||
expect(console.error).toHaveBeenCalledTimes(0)
|
||||
})
|
||||
|
||||
/* eslint no-console:0 */
|
@ -1,31 +0,0 @@
|
||||
import React from 'react'
|
||||
import {render} from '../'
|
||||
|
||||
test('rerender will re-render the element', () => {
|
||||
const Greeting = props => <div>{props.message}</div>
|
||||
const {container, rerender} = render(<Greeting message="hi" />)
|
||||
expect(container.firstChild).toHaveTextContent('hi')
|
||||
rerender(<Greeting message="hey" />)
|
||||
expect(container.firstChild).toHaveTextContent('hey')
|
||||
})
|
||||
|
||||
test('hydrate will not update props until next render', () => {
|
||||
const initialInputElement = document.createElement('input')
|
||||
const container = document.createElement('div')
|
||||
container.appendChild(initialInputElement)
|
||||
document.body.appendChild(container)
|
||||
|
||||
const firstValue = 'hello'
|
||||
initialInputElement.value = firstValue
|
||||
|
||||
const {rerender} = render(<input value="" onChange={() => null} />, {
|
||||
container,
|
||||
hydrate: true,
|
||||
})
|
||||
|
||||
expect(initialInputElement.value).toBe(firstValue)
|
||||
|
||||
const secondValue = 'goodbye'
|
||||
rerender(<input value={secondValue} onChange={() => null} />)
|
||||
expect(initialInputElement.value).toBe(secondValue)
|
||||
})
|
Loading…
Reference in new issue