From f15181b0b69599cc33385d4af2f1a60de087882b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Daniel=20Kuty=C5=82a?= Date: Mon, 28 Sep 2020 17:12:10 +0200 Subject: [PATCH] 16812: Added unit tests MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Arvados-DCO-1.1-Signed-off-by: Daniel Kutyła --- src/common/redirect-to.test.ts | 61 +++++++++++++++++++ .../actions/copy-to-clipboard-action.test.tsx | 36 +++++++++++ .../actions/file-viewer-action.test.tsx | 33 ++++++++++ .../context-menu/actions/helpers.test.ts | 30 +++++++++ .../actions/{helpers.tsx => helpers.ts} | 0 5 files changed, 160 insertions(+) create mode 100644 src/common/redirect-to.test.ts create mode 100644 src/views-components/context-menu/actions/copy-to-clipboard-action.test.tsx create mode 100644 src/views-components/context-menu/actions/file-viewer-action.test.tsx create mode 100644 src/views-components/context-menu/actions/helpers.test.ts rename src/views-components/context-menu/actions/{helpers.tsx => helpers.ts} (100%) diff --git a/src/common/redirect-to.test.ts b/src/common/redirect-to.test.ts new file mode 100644 index 00000000..24eaada8 --- /dev/null +++ b/src/common/redirect-to.test.ts @@ -0,0 +1,61 @@ +// Copyright (C) The Arvados Authors. All rights reserved. +// +// SPDX-License-Identifier: AGPL-3.0 + +import { storeRedirects, handleRedirects } from './redirect-to'; + +describe('redirect-to', () => { + const redirectTo = 'http://localhost/test123'; + + describe('storeRedirects', () => { + beforeEach(() => { + Object.defineProperty(window, 'location', { + value: { + href: `${window.location.href}?redirectTo=${redirectTo}` + } + }); + Object.defineProperty(window, 'sessionStorage', { + value: { + setItem: jest.fn(), + }, + writable: true + }); + }); + + it('should store redirectTo in the session storage', () => { + // when + storeRedirects(); + + // then + expect(window.sessionStorage.setItem).toHaveBeenCalledWith('redirectTo', redirectTo); + }); + }); + + describe('handleRedirects', () => { + beforeEach(() => { + Object.defineProperty(window, 'location', { + value: { + href: '' + } + }); + Object.defineProperty(window, 'sessionStorage', { + value: { + getItem: () => redirectTo, + removeItem: jest.fn(), + }, + writable: true + }); + }); + + it('should redirect to page when it is present in session storage', () => { + // given + const token = 'testToken'; + + // when + handleRedirects(token); + + // then + expect(window.location.href).toBe(`${redirectTo}?api_token=${token}`); + }); + }); +}); \ No newline at end of file diff --git a/src/views-components/context-menu/actions/copy-to-clipboard-action.test.tsx b/src/views-components/context-menu/actions/copy-to-clipboard-action.test.tsx new file mode 100644 index 00000000..1ada703b --- /dev/null +++ b/src/views-components/context-menu/actions/copy-to-clipboard-action.test.tsx @@ -0,0 +1,36 @@ +// Copyright (C) The Arvados Authors. All rights reserved. +// +// SPDX-License-Identifier: AGPL-3.0 + +import * as React from 'react'; +import { shallow, configure } from 'enzyme'; +import { ListItem } from "@material-ui/core"; +import * as Adapter from 'enzyme-adapter-react-16'; +import { CopyToClipboardAction } from './copy-to-clipboard-action'; + +configure({ adapter: new Adapter() }); + +jest.mock('copy-to-clipboard', () => jest.fn()); + +describe('CopyToClipboardAction', () => { + let props; + + beforeEach(() => { + props = { + onClick: jest.fn(), + href: 'https://collections.ardev.roche.com/c=ardev-4zz18-k0hamvtwyit6q56/t=1ha4ykd3w14ed19b2gh3uyjrjup38vsx27x1utwdne0bxcfg5d/LIMS/1.html', + }; + }); + + it('should render properly and handle click', () => { + // when + const wrapper = shallow(); + wrapper.find(ListItem).simulate('click'); + + // then + expect(wrapper).not.toBeUndefined(); + + // and + expect(props.onClick).toHaveBeenCalled(); + }); +}); \ No newline at end of file diff --git a/src/views-components/context-menu/actions/file-viewer-action.test.tsx b/src/views-components/context-menu/actions/file-viewer-action.test.tsx new file mode 100644 index 00000000..fa455def --- /dev/null +++ b/src/views-components/context-menu/actions/file-viewer-action.test.tsx @@ -0,0 +1,33 @@ +// Copyright (C) The Arvados Authors. All rights reserved. +// +// SPDX-License-Identifier: AGPL-3.0 + +import * as React from 'react'; +import { shallow, configure } from 'enzyme'; +import * as Adapter from 'enzyme-adapter-react-16'; +import { FileViewerAction } from './file-viewer-action'; + +configure({ adapter: new Adapter() }); + +describe('FileViewerAction', () => { + let props; + + beforeEach(() => { + props = { + onClick: jest.fn(), + href: 'https://collections.ardev.roche.com/c=ardev-4zz18-k0hamvtwyit6q56/t=1ha4ykd3w14ed19b2gh3uyjrjup38vsx27x1utwdne0bxcfg5d/LIMS/1.html', + }; + }); + + it('should render properly and handle click', () => { + // when + const wrapper = shallow(); + wrapper.find('a').simulate('click'); + + // then + expect(wrapper).not.toBeUndefined(); + + // and + expect(props.onClick).toHaveBeenCalled(); + }); +}); \ No newline at end of file diff --git a/src/views-components/context-menu/actions/helpers.test.ts b/src/views-components/context-menu/actions/helpers.test.ts new file mode 100644 index 00000000..6aaacc2f --- /dev/null +++ b/src/views-components/context-menu/actions/helpers.test.ts @@ -0,0 +1,30 @@ +// Copyright (C) The Arvados Authors. All rights reserved. +// +// SPDX-License-Identifier: AGPL-3.0 + +import { sanitizeToken, getClipboardUrl } from "./helpers"; + +describe('helpers', () => { + // given + const url = 'https://collections.ardev.roche.com/c=ardev-4zz18-k0hamvtwyit6q56/t=1ha4ykd3w14ed19b2gh3uyjrjup38vsx27x1utwdne0bxcfg5d/LIMS/1.html'; + + describe('sanitizeToken', () => { + it('should sanitize token from the url', () => { + // when + const result = sanitizeToken(url); + + // then + expect(result).toBe('https://collections.ardev.roche.com/c=ardev-4zz18-k0hamvtwyit6q56/LIMS/1.html?api_token=1ha4ykd3w14ed19b2gh3uyjrjup38vsx27x1utwdne0bxcfg5d'); + }); + }); + + describe('getClipboardUrl', () => { + it('should add redirectTo query param', () => { + // when + const result = getClipboardUrl(url); + + // then + expect(result).toBe('http://localhost?redirectTo=https://collections.ardev.roche.com/c=ardev-4zz18-k0hamvtwyit6q56/LIMS/1.html'); + }); + }); +}); \ No newline at end of file diff --git a/src/views-components/context-menu/actions/helpers.tsx b/src/views-components/context-menu/actions/helpers.ts similarity index 100% rename from src/views-components/context-menu/actions/helpers.tsx rename to src/views-components/context-menu/actions/helpers.ts -- 2.30.2