diff --git a/packages/app/obojobo-document-engine/__tests__/common/chunk/text-chunk/text-group-el.test.js b/packages/app/obojobo-document-engine/__tests__/common/chunk/text-chunk/text-group-el.test.js
index 789cb5e070..1bb005a1fa 100644
--- a/packages/app/obojobo-document-engine/__tests__/common/chunk/text-chunk/text-group-el.test.js
+++ b/packages/app/obojobo-document-engine/__tests__/common/chunk/text-chunk/text-group-el.test.js
@@ -1,4 +1,3 @@
-import { mount } from 'enzyme'
import React from 'react'
import renderer from 'react-test-renderer'
import TextGroupEl from '../../../../src/scripts/common/chunk/text-chunk/text-group-el'
@@ -39,11 +38,24 @@ describe('TextGroupEl', () => {
})
test('Renders text', () => {
- const component = mount(
+ const component = renderer.create(
)
- expect(component.text()).toBe('First line')
+ const tree = component.toJSON()
+
+ const findText = node => {
+ if (typeof node === 'string') {
+ return node
+ }
+ if (Array.isArray(node.children) && node.children.length > 0) {
+ return findText(node.children[0])
+ }
+ return null
+ }
+
+ const textContent = findText(tree)
+ expect(textContent).toBe('First line')
})
test('Variable replacement', () => {
@@ -51,11 +63,23 @@ describe('TextGroupEl', () => {
event.text = 'REPLACE(' + variable + ')'
})
- const component = mount(
+ const component = renderer.create(
)
+ const tree = component.toJSON()
- expect(component.text()).toBe('Some BOLD text with a REPLACE(variable) included')
+ const findText = node => {
+ if (typeof node === 'string') {
+ return node
+ }
+ if (Array.isArray(node.children) && node.children.length > 0) {
+ return node.children.map(child => findText(child)).join('')
+ }
+ return ''
+ }
+
+ const textContent = findText(tree)
+ expect(textContent).toBe('Some BOLD text with a REPLACE(variable) included')
})
test('Variable replacement with no match', () => {
@@ -63,11 +87,24 @@ describe('TextGroupEl', () => {
event.text = null
})
- const component = mount(
+ const component = renderer.create(
)
- expect(component.text()).toBe('Some BOLD text with a {{variable}} included')
+ const tree = component.toJSON()
+
+ const findText = node => {
+ if (typeof node === 'string') {
+ return node
+ }
+ if (Array.isArray(node.children) && node.children.length > 0) {
+ return node.children.map(child => findText(child)).join('')
+ }
+ return ''
+ }
+
+ const textContent = findText(tree)
+ expect(textContent).toBe('Some BOLD text with a {{variable}} included')
})
test('String values of hangingIndent work as expected', () => {
diff --git a/packages/app/obojobo-document-engine/__tests__/common/components/button-bar.test.js b/packages/app/obojobo-document-engine/__tests__/common/components/button-bar.test.js
index e7fea9c6ba..35ae262613 100644
--- a/packages/app/obojobo-document-engine/__tests__/common/components/button-bar.test.js
+++ b/packages/app/obojobo-document-engine/__tests__/common/components/button-bar.test.js
@@ -1,7 +1,6 @@
import React from 'react'
import ButtonBar from '../../../src/scripts/common/components/button-bar'
import renderer from 'react-test-renderer'
-import { mount } from 'enzyme'
describe('ButtonBar', () => {
test('ButtonBar component', () => {
@@ -35,21 +34,17 @@ describe('ButtonBar', () => {
}
]
const mockClick = jest.fn()
- const component = mount({children})
+ const component = renderer.create({children})
+ const buttonInstance = component.root.findByType('button')
- component
- .childAt(0)
- .find('button')
- .simulate('click')
- expect(mockClick).toBeCalledTimes(1)
+ buttonInstance.props.onClick()
+ expect(mockClick).toHaveBeenCalledTimes(1)
- // default function coverage for buttonBarOnClick
- const componentNoClick = mount({children})
- componentNoClick
- .childAt(0)
- .find('button')
- .simulate('click')
- expect(mockClick).toBeCalledTimes(1)
+ const componentNoClick = renderer.create({children})
+ const buttonInstanceNoClick = componentNoClick.root.findByType('button')
+
+ buttonInstanceNoClick.props.onClick()
+ expect(mockClick).toHaveBeenCalledTimes(1)
})
test('ButtonBar component clicks button but does not fire', () => {
@@ -59,13 +54,10 @@ describe('ButtonBar', () => {
}
]
const mockClick = jest.fn()
- const component = mount({children})
-
- component
- .childAt(0)
- .find('button')
- .simulate('click')
+ const component = renderer.create({children})
+ const buttonInstance = component.root.findByType('button')
+ buttonInstance.props.onClick()
expect(mockClick).toHaveBeenCalled()
})
})
diff --git a/packages/app/obojobo-document-engine/__tests__/common/components/delete-button-base.test.js b/packages/app/obojobo-document-engine/__tests__/common/components/delete-button-base.test.js
index 125b662712..13c51751f6 100644
--- a/packages/app/obojobo-document-engine/__tests__/common/components/delete-button-base.test.js
+++ b/packages/app/obojobo-document-engine/__tests__/common/components/delete-button-base.test.js
@@ -1,7 +1,6 @@
import React from 'react'
import DeleteButton from '../../../src/scripts/common/components/delete-button'
import DeleteButtonBase from '../../../src/scripts/common/components/delete-button-base'
-import { mount } from 'enzyme'
import renderer from 'react-test-renderer'
jest.mock('../../../src/scripts/common/page/focus')
@@ -44,8 +43,8 @@ describe('DeleteButton', () => {
test('DeleteButton calls focus callback with ref argument', () => {
const focus = require('../../../src/scripts/common/page/focus').default
- const wrapper = mount()
- const inst = wrapper.find(DeleteButtonBase).instance()
+ const component = renderer.create()
+ const inst = component.root.findByType(DeleteButtonBase).instance
inst.focus()
expect(focus).toHaveBeenCalledWith(inst.deleteButtonRef)
})
diff --git a/packages/app/obojobo-document-engine/__tests__/common/components/modal/modal.test.js b/packages/app/obojobo-document-engine/__tests__/common/components/modal/modal.test.js
index d24f6c456d..ea8a8445f0 100644
--- a/packages/app/obojobo-document-engine/__tests__/common/components/modal/modal.test.js
+++ b/packages/app/obojobo-document-engine/__tests__/common/components/modal/modal.test.js
@@ -1,7 +1,6 @@
import Modal from '../../../../src/scripts/common/components/modal/modal'
import ModalUtil from '../../../../src/scripts/common/util/modal-util'
import React from 'react'
-import { mount } from 'enzyme'
import renderer from 'react-test-renderer'
jest.mock('../../../../src/scripts/common/util/modal-util')
@@ -29,7 +28,7 @@ describe('Modal', () => {
})
test('Modal close button closes the modal', () => {
- const component = mount(
+ const component = renderer.create(
Content
@@ -37,10 +36,9 @@ describe('Modal', () => {
expect(onClose).toHaveBeenCalledTimes(0)
- component
- .find('DeleteButton')
- .find('button')
- .simulate('click')
+ const instance = component.root
+ const deleteButton = instance.findByType('button')
+ deleteButton.props.onClick()
expect(onClose).toHaveBeenCalledTimes(1)
@@ -48,7 +46,7 @@ describe('Modal', () => {
})
test('Esc closes modal', () => {
- const component = mount(
+ const component = renderer.create(
Content
@@ -64,7 +62,9 @@ describe('Modal', () => {
})
test('Esc closes modal (even when no onClose method present)', () => {
- const component = mount(Content)
+ const component = renderer.create(
+ Content
+ )
expect(ModalUtil.hide).toHaveBeenCalledTimes(0)
@@ -76,25 +76,32 @@ describe('Modal', () => {
})
test('Esc does not work if preventEsc is set', () => {
- const component = mount(
-
+ const onCloseMock = jest.fn()
+ const onKeyUpMock = jest.fn()
+ const component = renderer.create(
+
Content
)
expect(ModalUtil.hide).toHaveBeenCalledTimes(0)
- expect(onClose).toHaveBeenCalledTimes(0)
+ expect(onCloseMock).toHaveBeenCalledTimes(0)
- document.dispatchEvent(new KeyboardEvent('keyup', { keyCode: 27 }))
+ component.getInstance().onKeyUp({ keyCode: 27 })
expect(ModalUtil.hide).toHaveBeenCalledTimes(0)
- expect(onClose).toHaveBeenCalledTimes(0)
+ expect(onCloseMock).toHaveBeenCalledTimes(0)
component.unmount()
})
test('Modal does not close with other keys', () => {
- const component = mount(
+ const component = renderer.create(
Content
@@ -110,7 +117,7 @@ describe('Modal', () => {
})
test('Tab will focus on nothing if no close or first element', () => {
- const component = mount(
+ const component = renderer.create(
@@ -118,11 +125,7 @@ describe('Modal', () => {
expect(focusOnFirstElement).toHaveBeenCalledTimes(0)
- component
- .find('div')
- .at(0)
- .find('.first-tab')
- .simulate('focus')
+ component.root.findByProps({ className: 'first-tab' }).props.onFocus()
expect(focusOnFirstElement).not.toHaveBeenCalled()
@@ -130,7 +133,7 @@ describe('Modal', () => {
})
test('Tab will focus on the first element if no close button', () => {
- const component = mount(
+ const component = renderer.create(
@@ -138,11 +141,7 @@ describe('Modal', () => {
expect(focusOnFirstElement).toHaveBeenCalledTimes(0)
- component
- .find('div')
- .at(0)
- .find('.first-tab')
- .simulate('focus')
+ component.root.findByProps({ className: 'first-tab' }).props.onFocus()
expect(focusOnFirstElement).toHaveBeenCalledTimes(1)
@@ -150,20 +149,15 @@ describe('Modal', () => {
})
test('Tab will focus on the close button if it exists', () => {
- const component = mount(
+ const component = renderer.create(
)
- const deleteButtonFocus = jest.spyOn(component.instance().deleteButtonRef.current, 'focus')
+ const deleteButtonFocus = jest.spyOn(component.getInstance().deleteButtonRef.current, 'focus')
expect(focusOnFirstElement).toHaveBeenCalledTimes(0)
-
- component
- .find('div')
- .at(0)
- .find('.first-tab')
- .simulate('focus')
+ component.root.findByProps({ className: 'first-tab' }).props.onFocus()
expect(focusOnFirstElement).toHaveBeenCalledTimes(0)
expect(deleteButtonFocus).toHaveBeenCalledTimes(1)
@@ -172,7 +166,7 @@ describe('Modal', () => {
})
test('Unmounts with onClose function', () => {
- const component = mount(
+ const component = renderer.create(
Content
@@ -184,7 +178,9 @@ describe('Modal', () => {
})
test('Unmounts with no onClose function', () => {
- const component = mount(Content)
+ const component = renderer.create(
+ Content
+ )
component.unmount()
@@ -195,9 +191,11 @@ describe('Modal', () => {
const onClose = jest.fn()
const focusOnFirstElement = jest.fn()
const focus = jest.fn()
- const component = mount()
+ const component = renderer.create(
+
+ )
- const inst = component.instance()
+ const inst = component.getInstance()
inst.deleteButtonRef = { current: { focus } }
inst.onTabTrapFocus()
@@ -210,9 +208,9 @@ describe('Modal', () => {
test('onTabTrapFocus focuses on focusOnFirstElement with onClose prop not set', () => {
const focusOnFirstElement = jest.fn()
const focus = jest.fn()
- const component = mount()
+ const component = renderer.create()
- const inst = component.instance()
+ const inst = component.getInstance()
inst.deleteButtonRef = { current: { focus } }
inst.onTabTrapFocus()
@@ -224,9 +222,9 @@ describe('Modal', () => {
test('onTabTrapFocus does nothing without focusOnFirstElement or onClose props', () => {
const focus = jest.fn()
- const component = mount()
+ const component = renderer.create()
- const inst = component.instance()
+ const inst = component.getInstance()
inst.deleteButtonRef = { current: { focus } }
inst.onTabTrapFocus()
diff --git a/packages/app/obojobo-document-engine/__tests__/common/components/more-info-button.test.js b/packages/app/obojobo-document-engine/__tests__/common/components/more-info-button.test.js
index f9e75c7c1a..8cc597105a 100644
--- a/packages/app/obojobo-document-engine/__tests__/common/components/more-info-button.test.js
+++ b/packages/app/obojobo-document-engine/__tests__/common/components/more-info-button.test.js
@@ -1,6 +1,5 @@
import React from 'react'
import renderer from 'react-test-renderer'
-import { mount } from 'enzyme'
import MoreInfoButton from '../../../src/scripts/common/components/more-info-button'
jest.mock('../../../src/scripts/common/util/uuid', () => {
@@ -23,41 +22,91 @@ describe('MoreInfoButton', () => {
})
test('Renders mouse over', () => {
- const component = mount()
- component.instance().state.mode = 'hidden'
+ const component = renderer.create()
+ const button = component.root.findByType('button')
+
+ component.getInstance().setState({ mode: 'hover' })
+ button.props.onMouseOver()
+
+ const tree = component.toJSON()
+ const classNames = tree.props.className.split(' ')
- component.find('button').simulate('mouseOver')
+ expect(classNames).toContain('is-mode-hover')
+ })
+ test('Renders mouse over else', () => {
+ const component = renderer.create()
+ const button = component.root.findByType('button')
- expect(component.instance().state.mode).toEqual('hover')
+ button.props.onMouseOver()
- component.find('button').simulate('mouseOver')
+ const tree = component.toJSON()
+ const classNames = tree.props.className.split(' ')
- expect(component.instance().state.mode).toEqual('hover')
+ expect(classNames).toContain('is-mode-hover')
})
test('Renders mouse out', () => {
- const component = mount()
- component.instance().state.mode = 'hover'
+ const component = renderer.create()
+ const button = component.root.findByType('button')
- component.find('button').simulate('mouseOut')
+ button.props.onMouseOver()
- expect(component.instance().state.mode).toEqual('hidden')
+ button.props.onMouseOut()
- component.find('button').simulate('mouseOut')
+ const tree = component.toJSON()
+ const classNames = tree.props.className.split(' ')
+
+ expect(classNames).toContain('is-mode-hidden')
+ })
+ test('Renders mouse out else', () => {
+ const component = renderer.create()
+ const button = component.root.findByType('button')
+
+ button.props.onMouseOut()
+
+ const tree = component.toJSON()
+ const classNames = tree.props.className.split(' ')
- expect(component.instance().state.mode).toEqual('hidden')
+ expect(classNames).toContain('is-mode-hidden')
})
test('Renders click', () => {
- const component = mount()
- component.instance().state.mode = 'hidden'
+ const component = renderer.create()
+ const button = component.root.findByType('button')
+
+ const focusMock = jest.fn()
+ const moreInfoButtonInstance = component.getInstance()
+ moreInfoButtonInstance.dialogRef = { current: { focus: focusMock } }
+
+ button.props.onClick()
+
+ const tree = component.toJSON()
+
+ const classNames = tree.props.className.split(' ')
+
+ expect(classNames).toContain('is-mode-clicked')
+
+ expect(focusMock).not.toHaveBeenCalled()
+
+ button.props.onClick()
+
+ const tree2 = component.toJSON()
+
+ const classNames2 = tree2.props.className.split(' ')
+
+ expect(classNames2).toContain('is-mode-hidden')
+ })
+ test('componentDidUpdate method', () => {
+ const component = renderer.create()
+ const moreInfoButtonInstance = component.getInstance()
+ const focusMock = jest.fn()
- component.find('button').simulate('click')
+ moreInfoButtonInstance.onClick()
- expect(component.instance().state.mode).toEqual('clicked')
+ moreInfoButtonInstance.dialogRef = { current: { focus: focusMock } }
- component.find('button').simulate('click')
+ moreInfoButtonInstance.componentDidUpdate()
- expect(component.instance().state.mode).toEqual('hidden')
+ expect(focusMock).toHaveBeenCalled()
})
})
diff --git a/packages/app/obojobo-document-engine/__tests__/common/components/switch.test.js b/packages/app/obojobo-document-engine/__tests__/common/components/switch.test.js
index e2826decae..01e940775f 100644
--- a/packages/app/obojobo-document-engine/__tests__/common/components/switch.test.js
+++ b/packages/app/obojobo-document-engine/__tests__/common/components/switch.test.js
@@ -1,7 +1,6 @@
import React from 'react'
import Switch from '../../../src/scripts/common/components/switch'
import TestRenderer from 'react-test-renderer'
-import { mount } from 'enzyme'
describe('Switch', () => {
test('Switch renders correctly with no options set', () => {
@@ -41,11 +40,15 @@ describe('Switch', () => {
test('Switch calls onChange', () => {
const onChecked = jest.fn()
- const component = mount()
- const checkbox = component.find('input')
+ const component = TestRenderer.create()
+ const checkbox = component.root.findByType('input')
+
expect(onChecked).not.toHaveBeenCalled()
- checkbox.simulate('change', { target: { checked: true } })
+
+ checkbox.props.onChange({ target: { checked: true } })
+
expect(onChecked).toHaveBeenCalledTimes(1)
+
expect(onChecked).toHaveBeenCalledWith(expect.objectContaining({ target: { checked: true } }))
})
})
diff --git a/packages/app/obojobo-document-engine/__tests__/common/components/text-menu.test.js b/packages/app/obojobo-document-engine/__tests__/common/components/text-menu.test.js
index 74b022ace7..9cd6fcaf52 100644
--- a/packages/app/obojobo-document-engine/__tests__/common/components/text-menu.test.js
+++ b/packages/app/obojobo-document-engine/__tests__/common/components/text-menu.test.js
@@ -1,7 +1,5 @@
import React from 'react'
import renderer from 'react-test-renderer'
-import { mount } from 'enzyme'
-
import TextMenu from '../../../src/scripts/common/components/text-menu'
const createRect = () => {
@@ -59,8 +57,12 @@ describe('TextMenu', () => {
test('TextMenu calls mouseDown', () => {
const mockCommandHandler = jest.fn()
+ const mockEvent = {
+ preventDefault: jest.fn(),
+ stopPropagation: jest.fn()
+ }
- const component = mount(
+ const component = renderer.create(
createRect()
@@ -72,8 +74,12 @@ describe('TextMenu', () => {
/>
)
- component.find('a').simulate('mouseDown')
+ const linkElement = component.root.findByType('a')
+
+ linkElement.props.onMouseDown(mockEvent, 'Label')
+ expect(mockEvent.preventDefault).toHaveBeenCalledTimes(1)
+ expect(mockEvent.stopPropagation).toHaveBeenCalledTimes(1)
expect(mockCommandHandler).toHaveBeenCalledWith('Label')
})
})
diff --git a/packages/app/obojobo-document-engine/src/scripts/common/components/more-info-button.js b/packages/app/obojobo-document-engine/src/scripts/common/components/more-info-button.js
index 459ae0982e..b45e80fd35 100644
--- a/packages/app/obojobo-document-engine/src/scripts/common/components/more-info-button.js
+++ b/packages/app/obojobo-document-engine/src/scripts/common/components/more-info-button.js
@@ -53,7 +53,7 @@ class MoreInfoButton extends React.Component {
}
componentDidUpdate() {
- if (this.state.mode === 'clicked') {
+ if (this.state.mode === 'clicked' && this.dialogRef.current) {
this.dialogRef.current.focus()
}
}