Skip to content
Merged
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
15 changes: 15 additions & 0 deletions src/components/dialogs/__tests__/deleteConfirm.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @jest-environment jsdom
*/
import React from 'react'
import { render, screen, withMarkup } from 'rtl'
import { DeleteConfirmDialog } from '../deleteConfirm'

describe('DeleteConfirmDialog', () => {
it('should show confirm dialog with passed count', () => {
const count = 10
const message = new RegExp(`Are you sure you want to delete ${count} file\\(s\\)`)
render(<DeleteConfirmDialog count={count} />)
expect(withMarkup(screen.getByText)(message)).toBeInTheDocument()
})
})
33 changes: 30 additions & 3 deletions src/utils/test/rtl.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react'
import type { ReactElement } from 'react'
import { render } from '@testing-library/react'
import type { MatcherFunction } from '@testing-library/react'
import { makeAutoObservable } from 'mobx'
import { Provider } from 'mobx-react'
import { DndProvider } from 'react-dnd'
Expand All @@ -11,6 +12,8 @@ import { HotkeysProvider } from '@blueprintjs/core'
import userEvent from '@testing-library/user-event'
import en from '$src/locale/lang/en.json'

type Query = (f: MatcherFunction) => HTMLElement

const LOCALE_EN = en.translations

class State {
Expand Down Expand Up @@ -43,10 +46,34 @@ const AllTheProviders = ({ children }: { children: ReactElement }) => {

const customRender = (ui: ReactElement, options = {}) => render(ui, { wrapper: AllTheProviders, ...options })

// re-export everything
export * from '@testing-library/react'
function withMarkup(query: Query) {
return (text: string | RegExp) =>
query((content: string, node: Element | null) => {
const didMatch = (node: Element) => {
const expected = node.textContent

return (typeof text !== 'string' && text.test(expected)) || text === expected
}

const childrenMatch = Array.from(node.children).every((child) => !didMatch(child))

return didMatch(node) && childrenMatch
})
}

const t = i18n.i18next.t
const i18next = i18n.i18next

// jest doesn't have require.context so we patch this include
// to require the expected data
jest.mock('$src/locale/i18n', () => ({
i18n: {
i18next,
},
}))

// re-export everything
export * from '@testing-library/react'

// override render method
export { customRender as render, LOCALE_EN, userEvent, t }
export { customRender as render, withMarkup, LOCALE_EN, userEvent, t, i18next }