Skip to content

Commit

Permalink
Improve Testing (#1298)
Browse files Browse the repository at this point in the history
* test: setup enzyme, re-enable box tests

* test: update box spec to use enzyme

* test: move dustbin integration test into enzyme

* test: add multiple-dustbin integration test

* bump react version to 16.8.5
* add test-util functions: getBackendFromInstance, simulateDragDropSequence, simulateHoverSequence
  • Loading branch information
darthtrevino authored Mar 27, 2019
1 parent a9f340b commit f0aa2fa
Show file tree
Hide file tree
Showing 20 changed files with 1,004 additions and 150 deletions.
4 changes: 4 additions & 0 deletions jest/setup-enzyme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const Enzyme = require('enzyme')
const Adapter = require('enzyme-adapter-react-16')

Enzyme.configure({ adapter: new Adapter() })
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,29 @@
"@commitlint/cli": "^7.5.2",
"@commitlint/config-conventional": "^7.5.0",
"@types/jest": "^24.0.9",
"enzyme": "^3.9.0",
"enzyme-adapter-react-16": "^1.11.2",
"husky": "^1.3.1",
"jest": "^24.1.0",
"jest-environment-jsdom": "^24.0.0",
"lerna": "^3.13.1",
"lint-staged": "^8.1.5",
"npm": "^6.9.0",
"npm-run-all": "^4.1.5",
"prettier": "^1.16.4",
"react": "^16.8.5",
"react-dom": "^16.8.5",
"ts-jest": "^24.0.0",
"tslint": "^5.13.1",
"tslint-config-prettier": "^1.18.0",
"tslint-react": "^4.0.0",
"typescript": "^3.3.3333"
},
"jest": {
"clearMocks": true,
"setupFilesAfterEnv": [
"<rootDir>/jest/setup-enzyme.js"
],
"moduleFileExtensions": [
"ts",
"tsx",
Expand Down Expand Up @@ -134,5 +143,8 @@
"packages": [
"packages/*"
]
},
"dependencies": {
"@types/enzyme": "^3.9.1"
}
}
4 changes: 2 additions & 2 deletions packages/documentation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"gatsby-transformer-sharp": "^2.1.15",
"prismjs": "^1.15.0",
"query-string": "^6.3.0",
"react": "^16.8.4",
"react-dom": "^16.8.4",
"react": "^16.8.5",
"react-dom": "^16.8.5",
"react-dnd": "^7.4.3",
"react-dnd-documentation-examples": "^7.4.3",
"react-dnd-documentation-examples-hooks": "^7.4.3",
Expand Down
8 changes: 4 additions & 4 deletions packages/examples-hooks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
"@types/react-dom": "^16.8.2",
"npm-run-all": "^4.1.5",
"react-dnd-test-utils": "7.4.0",
"react": "^16.8.4",
"react-dom": "^16.8.4",
"react": "^16.8.5",
"react-dom": "^16.8.5",
"rimraf": "^2.6.3",
"typescript": "^3.3.3333"
},
"peerDependencies": {
"react": ">= 16.8.4",
"react-dom": ">= 16.8.4"
"react": ">= 16.8.5",
"react-dom": ">= 16.8.5"
}
}
8 changes: 4 additions & 4 deletions packages/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
"@types/react-dom": "^16.8.2",
"npm-run-all": "^4.1.5",
"react-dnd-test-utils": "7.4.0",
"react": "^16.8.4",
"react-dom": "^16.8.4",
"react": "^16.8.5",
"react-dom": "^16.8.5",
"rimraf": "^2.6.3",
"typescript": "^3.3.3333"
},
"peerDependencies": {
"react": ">= 16.8.4",
"react-dom": ">= 16.8.4"
"react": ">= 16.8.5",
"react-dom": ">= 16.8.5"
}
}
2 changes: 1 addition & 1 deletion packages/examples/src/01 Dustbin/Multiple Targets/Box.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface BoxProps {
isDragging: boolean
}

const Box: React.FC<BoxProps> = ({
export const Box: React.FC<BoxProps> = ({
name,
isDropped,
isDragging,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface DustbinProps {
connectDropTarget: ConnectDropTarget
}

const Dustbin: React.FC<DustbinProps> = ({
export const Dustbin: React.FC<DustbinProps> = ({
accepts,
isOver,
canDrop,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import React from 'react'
import Example from '..'
import DndDustbin, { DustbinProps } from '../Dustbin'
import DndBox, { BoxProps } from '../Box'
import {
wrapInTestContext,
getBackendFromInstance,
simulateDragDropSequence,
} from 'react-dnd-test-utils'
import { mount } from 'enzyme'
import { TestBackend } from 'react-dnd-test-backend'
import { DndComponent as DndC } from 'react-dnd'

describe('Dustbin: Multiple Targets', () => {
it('behaves as expected', () => {
const Wrapped = wrapInTestContext(Example)
const root = mount(<Wrapped />)
const backend = getBackendFromInstance<TestBackend>(root.instance() as any)

// Verify that all of the key components mounted
const dustbins = root.find(DndDustbin)
const boxes = root.find(DndBox)
expect(dustbins.length).toEqual(4)
expect(boxes.length).toEqual(3)

window.alert = jest.fn()

// Bin Types
const glassBin: DndC<DustbinProps> = dustbins.at(0).instance() as any
const foodBin: DndC<DustbinProps> = dustbins.at(1).instance() as any
// const paperGlassUrlBin: DndC<DustbinProps> = dustbins
// .at(2)
// .instance() as any
// const paperFileBin: DndC<DustbinProps> = dustbins.at(3).instance() as any

// Box Types
const bottleBox: DndC<BoxProps> = boxes.at(0).instance() as any
const bananaBox: DndC<BoxProps> = boxes.at(1).instance() as any
// const magazineBox: DndC<BoxProps> = boxes.at(2).instance() as any

// interactions

// drop bottle into glass bin
simulateDragDropSequence(bottleBox, glassBin, backend)
expect(glassBin.props.lastDroppedItem.name).toEqual(bottleBox.props.name)

// food won't drop into the glass bin
simulateDragDropSequence(bananaBox, glassBin, backend)
expect(glassBin.props.lastDroppedItem.name).toEqual(bottleBox.props.name)

// glass won't drop into the food box...
simulateDragDropSequence(bottleBox, foodBin, backend)
expect(foodBin.props.lastDroppedItem).toBeNull()

// but some food will work
simulateDragDropSequence(bananaBox, foodBin, backend)
expect(foodBin.props.lastDroppedItem.name).toEqual(bananaBox.props.name)
})
})
2 changes: 1 addition & 1 deletion packages/examples/src/01 Dustbin/Single Target/Box.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const style: React.CSSProperties = {
float: 'left',
}

interface BoxProps {
export interface BoxProps {
name: string

// Collected Props
Expand Down
Original file line number Diff line number Diff line change
@@ -1,74 +1,63 @@
import React from 'react'
import * as TestUtils from 'react-dom/test-utils'
import Box from '../Box'
import { wrapInTestContext } from 'react-dnd-test-utils'
import { mount } from 'enzyme'
import { TestBackend } from 'react-dnd-test-backend'
import { ContextComponent } from 'react-dnd'

class Wrapper extends React.Component {
public render() {
return this.props.children
}
}
describe('Box', () => {
// TODO: test utils are acting wonking with function components.
// take a pass at making the tests behave better
it.skip('can be tested independently', () => {
it('can be tested independently', () => {
// Obtain the reference to the component before React DnD wrapping
const OriginalBox = Box.DecoratedComponent

// Stub the React DnD connector functions with an identity function
const identity = (x: any) => x

const TestCase: React.FC = () => (
<Wrapper>
<OriginalBox
name="test"
connectDragSource={identity}
isDragging={false}
/>
</Wrapper>
)

// Render with one set of props and test
let root: any = TestUtils.renderIntoDocument(<TestCase />)
let div: HTMLDivElement = TestUtils.findRenderedDOMComponentWithTag(
root,
'div,',
) as HTMLDivElement
let root = mount(
<OriginalBox
name="test"
connectDragSource={identity}
isDragging={false}
/>,
)
let div = root.getDOMNode() as HTMLDivElement
expect(div.style.opacity).toEqual('1')

// Render with another set of props and test
root = TestUtils.renderIntoDocument(
root = mount(
<OriginalBox
name="test"
connectDragSource={identity}
isDragging={true}
/>,
)
div = TestUtils.findRenderedDOMComponentWithTag(
root,
'div',
) as HTMLDivElement
div = root.getDOMNode() as HTMLDivElement
expect(div.style.opacity).toEqual('0.4')
})

it('can be tested with the testing backend', () => {
// Render with the testing backend
const BoxContext = wrapInTestContext(Box)
const root: any = TestUtils.renderIntoDocument(<BoxContext name="test" />)
const root = mount(<BoxContext name="test" />)

// Obtain a reference to the backend
const backend = root.getManager().getBackend()
const element = root.instance() as ContextComponent<TestBackend>
const backend = (element.getManager().getBackend() as any) as TestBackend
expect(backend).toBeDefined()

// Check that the opacity is 1
let div: any = TestUtils.findRenderedDOMComponentWithTag(root, 'div')
let div = root.getDOMNode() as HTMLDivElement
expect(div.style.opacity).toEqual('1')

// Find the drag source ID and use it to simulate the dragging state
const box: any = TestUtils.findRenderedComponentWithType(root, Box as any)
const box: any = root.find(Box).instance()
backend.simulateBeginDrag([box.getHandlerId()])

// Verify that the div changed its opacity
div = TestUtils.findRenderedDOMComponentWithTag(root, 'div')
div = root.getDOMNode() as HTMLDivElement
expect(div.style.opacity).toEqual('0.4')
})
})

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React from 'react'
import Example from '../index'
import Box, { BoxProps } from '../Box'
import Dustbin, { DustbinProps } from '../Dustbin'
import {
wrapInTestContext,
getBackendFromInstance,
simulateDragDropSequence,
} from 'react-dnd-test-utils'
import { mount } from 'enzyme'
import { TestBackend } from 'react-dnd-test-backend'
import { DndComponent } from 'react-dnd'

describe('Integration: Dustbin Single Target', () => {
it('can simulate a full drag and drop interaction', () => {
function TestCase() {
return <Example />
}
const WrappedTestCase = wrapInTestContext(TestCase)

// Render with the test context that uses the test backend
const root = mount(<WrappedTestCase />)

// Obtain a reference to the backend
const backend = getBackendFromInstance<TestBackend>(root.instance() as any)

// Find the drag source ID and use it to simulate the dragging operation
const box: DndComponent<BoxProps> = root
.find(Box)
.at(0)
.instance() as any
const dustbin: DndComponent<DustbinProps> = root
.find(Dustbin)
.instance() as any

window.alert = jest.fn()
simulateDragDropSequence(box, dustbin, backend)
expect(window.alert).toHaveBeenCalledWith(
`You dropped ${box.props.name} into Dustbin!`,
)
})
})
4 changes: 2 additions & 2 deletions packages/react-dnd-html5-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"@types/react": "^16.8.7",
"npm-run-all": "^4.1.5",
"react-dnd-test-backend": "^7.4.0",
"react": "^16.8.4",
"react-dom": "^16.8.4",
"react": "^16.8.5",
"react-dom": "^16.8.5",
"rimraf": "^2.6.3",
"ts-loader": "^5.3.3",
"typescript": "^3.3.3333",
Expand Down
2 changes: 1 addition & 1 deletion packages/react-dnd-test-backend/src/TestBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function noop() {
// noop
}

export interface TestBackend {
export interface TestBackend extends Backend {
didCallSetup: boolean
didCallTeardown: boolean
simulateBeginDrag(sourceIds: Identifier[], options?: any): void
Expand Down
7 changes: 4 additions & 3 deletions packages/react-dnd-test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
},
"devDependencies": {
"npm-run-all": "^4.1.5",
"react": "^16.8.4",
"react": "^16.8.5",
"react-dom": "^16.8.5",
"react-dnd-test-backend": "^7.4.0",
"rimraf": "^2.6.3",
"typescript": "^3.3.3333"
},
"peerDependencies": {
"react": ">= 16.8.4",
"react": ">= 16.8.5",
"react-dnd": "^7.3.2",
"react-dnd-test-backend": "^7.2.0",
"react-dom": ">= 16.8.4"
"react-dom": ">= 16.8.5"
}
}
2 changes: 1 addition & 1 deletion packages/react-dnd-test-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './wrapInTestContext'
export * from './utils'
Loading

0 comments on commit f0aa2fa

Please sign in to comment.