~statianzo/angularjs-testing-library

9257e0642f6191987435f6b3b057ac69514d0afc — Jason Staten 4 years ago b82fb93
fix(test): multi-base test passes. rm unused tests
5 files changed, 2 insertions(+), 332 deletions(-)

M src/__tests__/multi-base.js
D src/__tests__/new-act.js
D src/__tests__/no-act.js
D src/__tests__/old-act.js
D src/__tests__/rerender.js
M src/__tests__/multi-base.js => src/__tests__/multi-base.js +2 -3
@@ 1,4 1,3 @@
import React from 'react'
import {render} from '../'

// these are created once per test suite and reused for each case


@@ 16,10 15,10 @@ afterAll(() => {
})

test('baseElement isolates trees from one another', () => {
  const {getByText: getByTextInA} = render(<div>Jekyll</div>, {
  const {getByText: getByTextInA} = render(`<div>Jekyll</div>`, {
    baseElement: treeA,
  })
  const {getByText: getByTextInB} = render(<div>Hyde</div>, {
  const {getByText: getByTextInB} = render(`<div>Hyde</div>`, {
    baseElement: treeB,
  })


D src/__tests__/new-act.js => src/__tests__/new-act.js +0 -76
@@ 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 */

D src/__tests__/no-act.js => src/__tests__/no-act.js +0 -80
@@ 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 */

D src/__tests__/old-act.js => src/__tests__/old-act.js +0 -142
@@ 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 */

D src/__tests__/rerender.js => src/__tests__/rerender.js +0 -31
@@ 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)
})