Skip to content

Commit

Permalink
refactor: Expose popper testing helper
Browse files Browse the repository at this point in the history
  • Loading branch information
y-lohse committed Apr 27, 2020
1 parent f30f6e3 commit 2c32652
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
19 changes: 2 additions & 17 deletions react/ActionMenu/index.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,10 @@ import React from 'react'
import { mount } from 'enzyme'

import ActionMenu, { ActionMenuItem } from './'
import fixPopperTesting from '../Popper/testing'

describe('ActionMenu', () => {
let createRangeBackup

beforeAll(() => {
createRangeBackup = global.document.createRange
global.document.createRange = jest.fn(() => ({
setStart: () => {},
setEnd: () => {},
commonAncestorContainer: {
nodeName: 'BODY',
ownerDocument: document
}
}))
})

afterAll(() => {
global.document.createRange = createRangeBackup
})
fixPopperTesting()

it('should support null children', () => {
const comp = mount(
Expand Down
23 changes: 23 additions & 0 deletions react/Popper/testing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Fixes the error "TypeError: document.createRange is not a function" in jest,
// see https://github.com/mui-org/material-ui/issues/15726#issuecomment-493124813
const fixPopperTesting = () => {
let createRangeBackup

beforeAll(() => {
createRangeBackup = global.document.createRange
global.document.createRange = jest.fn(() => ({
setStart: () => {},
setEnd: () => {},
commonAncestorContainer: {
nodeName: 'BODY',
ownerDocument: document
}
}))
})

afterAll(() => {
global.document.createRange = createRangeBackup
})
}

export default fixPopperTesting

0 comments on commit 2c32652

Please sign in to comment.