From bb6fb6b9ea02038c0075455aa3ed04d2de225ca4 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 8 Jan 2019 21:19:38 +0100 Subject: [PATCH] Created utils test --- .../helpers/CreateShortUrlResult.test.js | 17 ++++-- test/utils/utils.test.js | 60 +++++++++++++++++++ 2 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 test/utils/utils.test.js diff --git a/test/short-urls/helpers/CreateShortUrlResult.test.js b/test/short-urls/helpers/CreateShortUrlResult.test.js index b4ff4b50..e256969f 100644 --- a/test/short-urls/helpers/CreateShortUrlResult.test.js +++ b/test/short-urls/helpers/CreateShortUrlResult.test.js @@ -3,17 +3,24 @@ import { shallow } from 'enzyme'; import { identity } from 'ramda'; import { CopyToClipboard } from 'react-copy-to-clipboard'; import { Tooltip } from 'reactstrap'; -import CreateShortUrlResult from '../../../src/short-urls/helpers/CreateShortUrlResult'; +import * as sinon from 'sinon'; +import createCreateShortUrlResult from '../../../src/short-urls/helpers/CreateShortUrlResult'; describe('', () => { let wrapper; + const stateFlagTimeout = sinon.spy(); const createWrapper = (result, error = false) => { + const CreateShortUrlResult = createCreateShortUrlResult(stateFlagTimeout); + wrapper = shallow(); return wrapper; }; - afterEach(() => wrapper && wrapper.unmount()); + afterEach(() => { + stateFlagTimeout.resetHistory(); + wrapper && wrapper.unmount(); + }); it('renders an error when error is true', () => { const wrapper = createWrapper({}, true); @@ -37,12 +44,12 @@ describe('', () => { expect(wrapper.find(Tooltip)).toHaveLength(1); }); - it('Shows tooltip when copy to clipboard button is clicked', () => { + it('Invokes tooltip timeout when copy to clipboard button is clicked', () => { const wrapper = createWrapper({ shortUrl: 'https://doma.in/abc123' }); const copyBtn = wrapper.find(CopyToClipboard); - expect(wrapper.state('showCopyTooltip')).toEqual(false); + expect(stateFlagTimeout.callCount).toEqual(0); copyBtn.simulate('copy'); - expect(wrapper.state('showCopyTooltip')).toEqual(true); + expect(stateFlagTimeout.callCount).toEqual(1); }); }); diff --git a/test/utils/utils.test.js b/test/utils/utils.test.js new file mode 100644 index 00000000..94a40578 --- /dev/null +++ b/test/utils/utils.test.js @@ -0,0 +1,60 @@ +import * as sinon from 'sinon'; +import L from 'leaflet'; +import marker2x from 'leaflet/dist/images/marker-icon-2x.png'; +import marker from 'leaflet/dist/images/marker-icon.png'; +import markerShadow from 'leaflet/dist/images/marker-shadow.png'; +import { stateFlagTimeout as stateFlagTimeoutFactory, determineOrderDir, fixLeafletIcons } from '../../src/utils/utils'; + +describe('utils', () => { + describe('stateFlagTimeout', () => { + it('sets state and initializes timeout with provided delay', () => { + const setTimeout = sinon.fake((callback) => callback()); + const setState = sinon.spy(); + const stateFlagTimeout = stateFlagTimeoutFactory(setTimeout); + const delay = 5000; + const expectedSetStateCalls = 2; + + stateFlagTimeout(setState, 'foo', false, delay); + + expect(setState.callCount).toEqual(expectedSetStateCalls); + expect(setState.getCall(0).args).toEqual([{ foo: false }]); + expect(setState.getCall(1).args).toEqual([{ foo: true }]); + expect(setTimeout.callCount).toEqual(1); + expect(setTimeout.getCall(0).args[1]).toEqual(delay); + }); + }); + + describe('determineOrderDir', () => { + it('returns ASC when current order field and selected field are different', () => { + expect(determineOrderDir('foo', 'bar')).toEqual('ASC'); + expect(determineOrderDir('bar', 'foo')).toEqual('ASC'); + }); + + it('returns ASC when no current order dir is provided', () => { + expect(determineOrderDir('foo', 'foo')).toEqual('ASC'); + expect(determineOrderDir('bar', 'bar')).toEqual('ASC'); + }); + + it('returns DESC when current order field and selected field are equal and current order dir is ASC', () => { + expect(determineOrderDir('foo', 'foo', 'ASC')).toEqual('DESC'); + expect(determineOrderDir('bar', 'bar', 'ASC')).toEqual('DESC'); + }); + + it('returns undefined when current order field and selected field are equal and current order dir is DESC', () => { + expect(determineOrderDir('foo', 'foo', 'DESC')).toBeUndefined(); + expect(determineOrderDir('bar', 'bar', 'DESC')).toBeUndefined(); + }); + }); + + describe('fixLeafletIcons', () => { + it('updates icons used by leaflet', () => { + fixLeafletIcons(); + + const { iconRetinaUrl, iconUrl, shadowUrl } = L.Icon.Default.prototype.options; + + expect(iconRetinaUrl).toEqual(marker2x); + expect(iconUrl).toEqual(marker); + expect(shadowUrl).toEqual(markerShadow); + }); + }); +});