Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Make screen compatible with document.body.replaceWith() #1311

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/__tests__/screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,12 @@ test('exposes debug method', () => {
`)
console.log.mockClear()
})

test('queries after replaceWith', async () => {
const newBody = document.createElement('body')
newBody.innerHTML = '<div>replaceWith element</div>'
document.body.replaceWith(newBody)
screen.getByText('replaceWith element')
await screen.findByText('replaceWith element')
expect(screen.queryByText('replaceWith element')).not.toBeNull()
})
32 changes: 16 additions & 16 deletions src/screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// TODO: Statically verify we don't rely on NodeJS implicit named imports.
import lzString from 'lz-string'
import {type OptionsReceived} from 'pretty-format'
import {getQueriesForElement} from './get-queries-for-element'
import {getDocument} from './helpers'
import {logDOM} from './pretty-dom'
import * as queries from './queries'
Expand Down Expand Up @@ -46,19 +45,20 @@ const logTestingPlaygroundURL = (element = getDocument().body) => {
return playgroundUrl
}

const initialValue = {debug, logTestingPlaygroundURL}
const initialValue: Record<string, Function> = {debug, logTestingPlaygroundURL}

export const screen =
typeof document !== 'undefined' && document.body // eslint-disable-line @typescript-eslint/no-unnecessary-condition
? getQueriesForElement(document.body, queries, initialValue)
: Object.keys(queries).reduce((helpers, key) => {
// `key` is for all intents and purposes the type of keyof `helpers`, which itself is the type of `initialValue` plus incoming properties from `queries`
// if `Object.keys(something)` returned Array<keyof typeof something> this explicit type assertion would not be necessary
// see https://stackoverflow.com/questions/55012174/why-doesnt-object-keys-return-a-keyof-type-in-typescript
helpers[key as keyof typeof initialValue] = () => {
throw new TypeError(
'For queries bound to document.body a global document has to be available... Learn more: https://testing-library.com/s/screen-global-error',
)
}
return helpers
}, initialValue)
export const screen = Object.entries(queries).reduce((helpers, [key, fn]) => {
// `key` is for all intents and purposes the type of keyof `helpers`, which itself is the type of `initialValue` plus incoming properties from `queries`
// if `Object.keys(something)` returned Array<keyof typeof something> this explicit type assertion would not be necessary
// see https://stackoverflow.com/questions/55012174/why-doesnt-object-keys-return-a-keyof-type-in-typescript
helpers[key] = (...args: any[]) => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (typeof document === 'undefined' || !document.body) {
throw new TypeError(
'For queries bound to document.body a global document has to be available... Learn more: https://testing-library.com/s/screen-global-error',
)
}
return fn(document.body, ...(args as any[]))
}
return helpers
}, initialValue)
Loading