Skip to content

Commit

Permalink
fix: don't fun button_handleClick in a setTimeout for testing (#439)
Browse files Browse the repository at this point in the history
* Not run button_handleClick setTimeout for testing(#437)

* Update downshift.js
  • Loading branch information
nicoxxie authored and Kent C. Dodds committed May 1, 2018
1 parent a9f17d0 commit bdfe5e0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
25 changes: 23 additions & 2 deletions src/__tests__/downshift.get-button-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ test('space on button opens and closes the menu', () => {
test('clicking on the button opens and closes the menu', () => {
const {button, renderSpy} = setup()
Simulate.click(button)
jest.runAllTimers()
expect(renderSpy).toHaveBeenLastCalledWith(
expect.objectContaining({isOpen: true}),
)
Simulate.click(button)
jest.runAllTimers()
expect(renderSpy).toHaveBeenLastCalledWith(
expect.objectContaining({isOpen: false}),
)
Expand Down Expand Up @@ -91,6 +89,29 @@ test(`getToggleButtonProps doesn't include event handlers when disabled is passe
}
})

describe('Expect timer to trigger on process.env.NODE_ENV !== test value', () => {
const originalEnv = process.env.NODE_ENV

afterEach(() => {
process.env.NODE_ENV = originalEnv
})

test('clicking on the button opens and closes the menu for test', () => {
process.env.NODE_ENV = 'production'
const {button, renderSpy} = setup()
Simulate.click(button)
jest.runAllTimers()
expect(renderSpy).toHaveBeenLastCalledWith(
expect.objectContaining({isOpen: true}),
)
Simulate.click(button)
jest.runAllTimers()
expect(renderSpy).toHaveBeenLastCalledWith(
expect.objectContaining({isOpen: false}),
)
})
})

function setup({buttonProps, Button = props => <button {...props} />} = {}) {
let renderArg
const renderSpy = jest.fn(controllerArg => {
Expand Down
15 changes: 11 additions & 4 deletions src/downshift.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,10 +582,17 @@ class Downshift extends Component {
) {
event.target.focus()
}
// Ensure that toggle of menu occurs after the potential blur event in iOS
setTimeout(() =>
this.toggleMenu({type: Downshift.stateChangeTypes.clickButton}),
)
// to simplify testing components that use downshift, we'll not wrap this in a setTimeout
// if the NODE_ENV is test. With the proper build system, this should be dead code eliminated
// when building for production and should therefore have no impact on production code.
if (process.env.NODE_ENV === 'test') {
this.toggleMenu({type: Downshift.stateChangeTypes.clickButton})
} else {
// Ensure that toggle of menu occurs after the potential blur event in iOS
setTimeout(() =>
this.toggleMenu({type: Downshift.stateChangeTypes.clickButton}),
)
}
}

button_handleBlur = () => {
Expand Down

0 comments on commit bdfe5e0

Please sign in to comment.