Skip to content

Commit

Permalink
Add tests for action HOCs
Browse files Browse the repository at this point in the history
  • Loading branch information
robbieaverill committed Sep 17, 2018
1 parent deeb9bf commit 6706727
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 0 deletions.
42 changes: 42 additions & 0 deletions client/src/components/ElementActions/tests/AbstractAction-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* eslint-disable import/no-extraneous-dependencies */
/* global jest, describe, it, expect */

import React from 'react';
import AbstractAction from '../AbstractAction';
import Enzyme, { shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-15.4/build/index';

Enzyme.configure({ adapter: new Adapter() });

describe('AbstractAction', () => {
const clickHandler = jest.fn();
let wrapper = null;

beforeEach(() => {
wrapper = shallow(
<AbstractAction
onClick={clickHandler}
title="My abstract action"
disabled={false}
extraClass="foo-bar"
/>
);
});

it('renders a button', () => {
expect(wrapper.find('button').length).toBe(1);
});

it('includes the title text', () => {
expect(wrapper.text()).toContain('My abstract action');
});

it('delegates clicking to the provided handler', () => {
wrapper.find('button').simulate('click');
expect(clickHandler).toHaveBeenCalled();
});

it('adds provided extra classes', () => {
expect(wrapper.find('button').hasClass('foo-bar')).toBe(true);
});
});
78 changes: 78 additions & 0 deletions client/src/components/ElementActions/tests/ArchiveAction-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* eslint-disable import/no-extraneous-dependencies */
/* global jest, describe, it, expect */

import React from 'react';
import { Component as ArchiveAction } from '../ArchiveAction';
import Enzyme, { mount } from 'enzyme';
import Adapter from 'enzyme-adapter-react-15.4/build/index';

Enzyme.configure({ adapter: new Adapter() });

describe('ArchiveAction', () => {
let wrapper = null;
const mockMutation = jest.fn();
const WrappedComponent = (props) => <div>{props.children}</div>;
const ActionComponent = ArchiveAction(WrappedComponent);

beforeEach(() => {
wrapper = mount(
<ActionComponent
id={123}
isPublished
actions={{ handleArchiveBlock: mockMutation }}
/>
);
});

it('renders the wrapped component', () => {
expect(wrapper.children().first().type()).toEqual(WrappedComponent);
});

it('renders a button', () => {
expect(wrapper.find('button').length).toBe(1);
});

it('renders the title and class', () => {
expect(wrapper.find('button').text()).toContain('Archive');
expect(wrapper.find('button').hasClass('element-editor__actions-archive')).toBe(true);
});

it('does not archive when declining the confirmation', () => {
global.confirm = () => false;
wrapper.find('button').simulate('click');
expect(mockMutation).not.toHaveBeenCalled();
});

it('archives when accepting the confirmation', () => {
global.confirm = () => true;
wrapper.find('button').simulate('click');
expect(mockMutation).toHaveBeenCalled();
});

it('indicates that the block will be sent to archive', () => {
const unpublishedWrapper = mount(
<ActionComponent
id={123}
isPublished={false}
actions={{ handleArchiveBlock: mockMutation }}
/>
);
const mockConfirm = jest.fn();
global.confirm = mockConfirm;

unpublishedWrapper.find('button').simulate('click');
expect(mockConfirm).toHaveBeenCalledWith(
'Are you sure you want to send this block to the archive?'
);
});

it('indicates that the block will be unpublished before archiving', () => {
const mockConfirm = jest.fn();
global.confirm = mockConfirm;

wrapper.find('button').simulate('click');
expect(mockConfirm).toHaveBeenCalledWith(expect.stringContaining(
'Warning: This block will be unpublished'
));
});
});
53 changes: 53 additions & 0 deletions client/src/components/ElementActions/tests/PublishAction-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* eslint-disable import/no-extraneous-dependencies */
/* global jest, describe, it, expect */

import React from 'react';
import { Component as PublishAction } from '../PublishAction';
import Enzyme, { mount } from 'enzyme';
import Adapter from 'enzyme-adapter-react-15.4/build/index';

Enzyme.configure({ adapter: new Adapter() });

describe('PublishAction', () => {
let wrapper = null;
const mockMutation = jest.fn();
const WrappedComponent = (props) => <div>{props.children}</div>;
const ActionComponent = PublishAction(WrappedComponent);

beforeEach(() => {
wrapper = mount(
<ActionComponent
id={123}
version={234}
isLiveVersion={false}
actions={{ handlePublishBlock: mockMutation }}
/>
);
});

it('renders the wrapped component', () => {
expect(wrapper.children().first().type()).toEqual(WrappedComponent);
});

it('renders a button', () => {
expect(wrapper.find('button').length).toBe(1);
});

it('renders the title and class', () => {
expect(wrapper.find('button').text()).toContain('Publish');
expect(wrapper.find('button').hasClass('element-editor__actions-publish')).toBe(true);
});

it('publishes from draft to live', () => {
wrapper.find('button').simulate('click');
expect(mockMutation).toHaveBeenCalledWith(123, 'DRAFT', 'LIVE', 234);
});

it('returns null when is the live version', () => {
const draftWrapper = mount(
<ActionComponent isLiveVersion />
);

expect(draftWrapper.find('button').length).toBe(0);
});
});
52 changes: 52 additions & 0 deletions client/src/components/ElementActions/tests/UnpublishAction-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* eslint-disable import/no-extraneous-dependencies */
/* global jest, describe, it, expect */

import React from 'react';
import { Component as UnpublishAction } from '../UnpublishAction';
import Enzyme, { mount } from 'enzyme';
import Adapter from 'enzyme-adapter-react-15.4/build/index';

Enzyme.configure({ adapter: new Adapter() });

describe('UnpublishAction', () => {
let wrapper = null;
const mockMutation = jest.fn();
const WrappedComponent = (props) => <div>{props.children}</div>;
const ActionComponent = UnpublishAction(WrappedComponent);

beforeEach(() => {
wrapper = mount(
<ActionComponent
id={123}
isPublished
actions={{ handleUnpublishBlock: mockMutation }}
/>
);
});

it('renders the wrapped component', () => {
expect(wrapper.children().first().type()).toEqual(WrappedComponent);
});

it('renders a button', () => {
expect(wrapper.find('button').length).toBe(1);
});

it('renders the title and class', () => {
expect(wrapper.find('button').text()).toContain('Unpublish');
expect(wrapper.find('button').hasClass('element-editor__actions-unpublish')).toBe(true);
});

it('returns null when is not published', () => {
const draftWrapper = mount(
<ActionComponent isPublished={false} />
);

expect(draftWrapper.find('button').length).toBe(0);
});

it('calls the unpublish mutation', () => {
wrapper.find('button').simulate('click');
expect(mockMutation).toHaveBeenCalled();
});
});

0 comments on commit 6706727

Please sign in to comment.