16812: Added unit tests
authorDaniel Kutyła <daniel.kutyla@contractors.roche.com>
Mon, 28 Sep 2020 15:12:10 +0000 (17:12 +0200)
committerDaniel Kutyła <daniel.kutyla@contractors.roche.com>
Mon, 28 Sep 2020 15:12:10 +0000 (17:12 +0200)
Arvados-DCO-1.1-Signed-off-by: Daniel Kutyła <daniel.kutyla@contractors.roche.com>

src/common/redirect-to.test.ts [new file with mode: 0644]
src/views-components/context-menu/actions/copy-to-clipboard-action.test.tsx [new file with mode: 0644]
src/views-components/context-menu/actions/file-viewer-action.test.tsx [new file with mode: 0644]
src/views-components/context-menu/actions/helpers.test.ts [new file with mode: 0644]
src/views-components/context-menu/actions/helpers.ts [moved from src/views-components/context-menu/actions/helpers.tsx with 100% similarity]

diff --git a/src/common/redirect-to.test.ts b/src/common/redirect-to.test.ts
new file mode 100644 (file)
index 0000000..24eaada
--- /dev/null
@@ -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 (file)
index 0000000..1ada703
--- /dev/null
@@ -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(<CopyToClipboardAction {...props} />);
+        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 (file)
index 0000000..fa455de
--- /dev/null
@@ -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(<FileViewerAction {...props} />);
+        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 (file)
index 0000000..6aaacc2
--- /dev/null
@@ -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