Skip to content

Instantly share code, notes, and snippets.

@kentcdodds
Created June 23, 2020 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kentcdodds/cab9925152e2aef270d76a446b9c8fba to your computer and use it in GitHub Desktop.
Save kentcdodds/cab9925152e2aef270d76a446b9c8fba to your computer and use it in GitHub Desktop.
import React from 'react'
import {client} from 'utils/api-client'
import {render, screen, loginAsUser} from 'test/app-test-utils'
import {buildBook, buildListItem} from 'test/generate'
import * as booksDB from 'test/data/books'
import * as listItemsDB from 'test/data/list-items'
import {App} from 'app'
async function renderBookScreen({book, listItem} = {}) {
const user = await loginAsUser()
if (book === undefined) {
book = await booksDB.create(buildBook())
}
if (listItem === undefined) {
listItem = await listItemsDB.create(buildListItem({owner: user, book}))
}
const route = `/book/${book.id}`
const utils = await render(<App />, {user, route})
return {
...utils,
book,
user,
listItem,
}
}
/*
// fails when using describe
describe('fake timers', () => {
// using fake timers to skip debounce time
beforeEach(() => jest.useFakeTimers())
afterEach(() => jest.useRealTimers())
test('can edit a note', async () => {
// jest.useFakeTimers()
await renderBookScreen()
})
})
/* */
/*
// fails when enabling fake timers in a test
afterEach(() => jest.useRealTimers())
test('can edit a note', async () => {
jest.useFakeTimers()
await renderBookScreen()
})
/* */
/*
// fails when enabling modern fake timers in a beforeEach
beforeEach(() => jest.useFakeTimers('modern'))
afterEach(() => jest.useRealTimers())
test('can edit a note', async () => {
await renderBookScreen()
})
/* */
/*
// passes when not in describe and not enabling legacy fake timers in a single test
beforeEach(() => jest.useFakeTimers('legacy'))
afterEach(() => jest.useRealTimers())
test('can edit a note', async () => {
await renderBookScreen()
})
/* */
/*
// passes when not using fake timers
test('can edit a note', async () => {
await renderBookScreen()
})
/* */
// afterEach(() => jest.useRealTimers())
// test('can edit a note', async () => {
// jest.useFakeTimers('modern')
// await renderBookScreen()
// })
beforeAll(() => {
jest.spyOn(console, 'error').mockImplementation(() => {})
})
afterAll(() => {
console.error.mockRestore()
})
test('shows an error message when the book fails to load', async () => {
const book = {id: 'BAD_ID'}
await renderBookScreen({listItem: null, book})
expect((await screen.findByRole('alert')).textContent).toMatchInlineSnapshot(
`"There was an error: Book not found"`,
)
expect(console.error).toHaveBeenCalled()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment