This repository has been archived by the owner on Jun 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
EjectButton component test #348
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fbfe00e
added test
AWolf81 fc2153e
fixed flow
AWolf81 168fffa
inlined click handler
AWolf81 caa6ebb
improved test text as requested in review
AWolf81 635cf75
removed typing as it's a typescript definition
AWolf81 dd4dc4c
updated snapshot
AWolf81 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import { remote } from 'electron'; | |
import { COLORS } from '../../constants'; | ||
|
||
import BigClickableButton from '../BigClickableButton'; | ||
import type Electron from 'electron'; | ||
|
||
const { dialog } = remote; | ||
|
||
|
@@ -17,30 +18,31 @@ type Props = { | |
onClick: () => void, | ||
}; | ||
|
||
export const dialogOptions: Electron.MessageBoxOptions = { | ||
type: 'warning', | ||
buttons: ['Yes, light this candle', "Ahhh no don't do that"], | ||
defaultId: 1, | ||
cancelId: 1, | ||
title: 'Are you sure?', | ||
message: | ||
'Ejecting is a permanent one-time task that unwraps the create-react-app environment.', | ||
detail: | ||
"It's recommended for users comfortable with Webpack, who need to make tweaks not possible without ejecting.", | ||
}; | ||
|
||
export function dialogCallback(response: number) { | ||
// The response will be the index of the chosen option, from the | ||
// `buttons` array above. | ||
const isConfirmed = response === 0; | ||
|
||
if (isConfirmed) { | ||
this.props.onClick(); | ||
} | ||
} | ||
|
||
class EjectButton extends PureComponent<Props> { | ||
handleClick = () => { | ||
dialog.showMessageBox( | ||
{ | ||
type: 'warning', | ||
buttons: ['Yes, light this candle', "Ahhh no don't do that"], | ||
defaultId: 1, | ||
cancelId: 1, | ||
title: 'Are you sure?', | ||
message: | ||
'Ejecting is a permanent one-time task that unwraps the create-react-app environment.', | ||
detail: | ||
"It's recommended for users comfortable with Webpack, who need to make tweaks not possible without ejecting.", | ||
}, | ||
(response: number) => { | ||
// The response will be the index of the chosen option, from the | ||
// `buttons` array above. | ||
const isConfirmed = response === 0; | ||
|
||
if (isConfirmed) { | ||
this.props.onClick(); | ||
} | ||
} | ||
); | ||
dialog.showMessageBox(dialogOptions, dialogCallback.bind(this)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Maybe I'm inlining this in the render method as this got really short with the exports. |
||
}; | ||
|
||
render() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* eslint-disable flowtype/require-valid-file-annotation */ | ||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import { remote } from 'electron'; // Mocked | ||
import EjectButton, { dialogOptions, dialogCallback } from './EjectButton'; | ||
import BigClickableButton from '../BigClickableButton'; | ||
|
||
const { dialog } = remote; | ||
|
||
describe('EjectButton component', () => { | ||
let wrapper; | ||
let clickHandler; | ||
|
||
beforeEach(() => { | ||
clickHandler = jest.fn(); | ||
wrapper = shallow(<EjectButton onClick={clickHandler} />); | ||
}); | ||
|
||
it('should render (not running)', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At first glance it was a little unclear to me what running/not running meant, until I realized it was the pressed state... I think it could be changed to something like:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. I modified it. |
||
expect(wrapper).toMatchSnapshot(); | ||
}); | ||
|
||
it('should render (running)', () => { | ||
wrapper.setProps({ isRunning: true }); | ||
expect(wrapper).toMatchSnapshot(); | ||
}); | ||
|
||
describe('Confirm dialog', () => { | ||
let ejectButton; | ||
beforeEach(() => { | ||
ejectButton = wrapper.find(BigClickableButton); | ||
ejectButton.simulate('click'); | ||
}); | ||
|
||
it('should display dialog', () => { | ||
expect(dialog.showMessageBox).toBeCalledWith( | ||
dialogOptions, | ||
expect.anything() | ||
); | ||
}); | ||
|
||
it('should call clickHandler if confirmed', () => { | ||
dialogCallback.call(wrapper.instance(), 0); | ||
expect(clickHandler.mock.calls.length).toBe(1); | ||
}); | ||
|
||
it('should dismiss with-out calling clickHandler', () => { | ||
dialogCallback.call(wrapper.instance(), 1); | ||
expect(clickHandler.mock.calls.length).toBe(0); | ||
}); | ||
}); | ||
}); |
68 changes: 68 additions & 0 deletions
68
src/components/EjectButton/__snapshots__/EjectButton.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`EjectButton component should render (not running) 1`] = ` | ||
<BigClickableButton | ||
colors={ | ||
Array [ | ||
"#651fff", | ||
"#304FFE", | ||
] | ||
} | ||
height={34} | ||
onClick={[Function]} | ||
thickness={6} | ||
width={40} | ||
> | ||
<Icon | ||
fill="currentColor" | ||
icon={ | ||
Object { | ||
"children": Array [ | ||
Object { | ||
"attribs": Object { | ||
"d": "M5 17h14v2H5zm7-12L5.33 15h13.34z", | ||
}, | ||
"name": "path", | ||
}, | ||
], | ||
"viewBox": "0 0 24 24", | ||
} | ||
} | ||
size={24} | ||
/> | ||
</BigClickableButton> | ||
`; | ||
|
||
exports[`EjectButton component should render (running) 1`] = ` | ||
<BigClickableButton | ||
colors={ | ||
Array [ | ||
"#651fff", | ||
"#304FFE", | ||
] | ||
} | ||
height={34} | ||
isOn={true} | ||
onClick={[Function]} | ||
thickness={6} | ||
width={40} | ||
> | ||
<Icon | ||
fill="currentColor" | ||
icon={ | ||
Object { | ||
"children": Array [ | ||
Object { | ||
"attribs": Object { | ||
"d": "M5 17h14v2H5zm7-12L5.33 15h13.34z", | ||
}, | ||
"name": "path", | ||
}, | ||
], | ||
"viewBox": "0 0 24 24", | ||
} | ||
} | ||
size={24} | ||
/> | ||
</BigClickableButton> | ||
`; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't realize they now include type definitions 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, OK, I've tested if the definition is working (by adding a key that's not in the definition - wasn't working) and noticed that this is a TypeScript definition and not Flow.
That's why I removed it.
So there is typing in Electron but no Flow types.