Skip to content
This repository has been archived by the owner on Jun 17, 2021. It is now read-only.

EjectButton component test #348

Merged
merged 6 commits into from
Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions src/components/EjectButton/EjectButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { remote } from 'electron';
import { COLORS } from '../../constants';

import BigClickableButton from '../BigClickableButton';
import type Electron from 'electron';
Copy link
Collaborator

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 👍

Copy link
Collaborator Author

@AWolf81 AWolf81 Jan 15, 2019

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.


const { dialog } = remote;

Expand All @@ -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));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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() {
Expand Down
52 changes: 52 additions & 0 deletions src/components/EjectButton/EjectButton.test.js
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)', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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:

it('should render without being pressed (not running)', () => {

it('should render as pressed (running)', () => {

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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 src/components/EjectButton/__snapshots__/EjectButton.test.js.snap
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>
`;