From 6706727a8806911e0b4de165b43ccda5a0b3fc8e Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Thu, 13 Sep 2018 13:09:59 +0200 Subject: [PATCH] Add tests for action HOCs --- .../tests/AbstractAction-test.js | 42 ++++++++++ .../tests/ArchiveAction-test.js | 78 +++++++++++++++++++ .../tests/PublishAction-test.js | 53 +++++++++++++ .../tests/UnpublishAction-test.js | 52 +++++++++++++ 4 files changed, 225 insertions(+) create mode 100644 client/src/components/ElementActions/tests/AbstractAction-test.js create mode 100644 client/src/components/ElementActions/tests/ArchiveAction-test.js create mode 100644 client/src/components/ElementActions/tests/PublishAction-test.js create mode 100644 client/src/components/ElementActions/tests/UnpublishAction-test.js diff --git a/client/src/components/ElementActions/tests/AbstractAction-test.js b/client/src/components/ElementActions/tests/AbstractAction-test.js new file mode 100644 index 00000000..d370fb8f --- /dev/null +++ b/client/src/components/ElementActions/tests/AbstractAction-test.js @@ -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( + + ); + }); + + 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); + }); +}); diff --git a/client/src/components/ElementActions/tests/ArchiveAction-test.js b/client/src/components/ElementActions/tests/ArchiveAction-test.js new file mode 100644 index 00000000..4f7073f4 --- /dev/null +++ b/client/src/components/ElementActions/tests/ArchiveAction-test.js @@ -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) =>
{props.children}
; + const ActionComponent = ArchiveAction(WrappedComponent); + + beforeEach(() => { + wrapper = mount( + + ); + }); + + 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( + + ); + 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' + )); + }); +}); diff --git a/client/src/components/ElementActions/tests/PublishAction-test.js b/client/src/components/ElementActions/tests/PublishAction-test.js new file mode 100644 index 00000000..34135974 --- /dev/null +++ b/client/src/components/ElementActions/tests/PublishAction-test.js @@ -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) =>
{props.children}
; + const ActionComponent = PublishAction(WrappedComponent); + + beforeEach(() => { + wrapper = mount( + + ); + }); + + 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( + + ); + + expect(draftWrapper.find('button').length).toBe(0); + }); +}); diff --git a/client/src/components/ElementActions/tests/UnpublishAction-test.js b/client/src/components/ElementActions/tests/UnpublishAction-test.js new file mode 100644 index 00000000..a9007672 --- /dev/null +++ b/client/src/components/ElementActions/tests/UnpublishAction-test.js @@ -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) =>
{props.children}
; + const ActionComponent = UnpublishAction(WrappedComponent); + + beforeEach(() => { + wrapper = mount( + + ); + }); + + 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( + + ); + + expect(draftWrapper.find('button').length).toBe(0); + }); + + it('calls the unpublish mutation', () => { + wrapper.find('button').simulate('click'); + expect(mockMutation).toHaveBeenCalled(); + }); +});