16115: Adds unit tests to SharingURLsComponent.
[arvados-workbench2.git] / src / views-components / sharing-dialog / sharing-urls-component.test.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React from 'react';
6 import { mount, configure } from 'enzyme';
7 import Adapter from 'enzyme-adapter-react-16';
8
9 import {
10     SharingURLsComponent,
11     SharingURLsComponentProps
12 } from './sharing-urls-component';
13
14 configure({ adapter: new Adapter() });
15
16 describe("<SharingURLsComponent />", () => {
17     let props: SharingURLsComponentProps;
18     let wrapper;
19
20     beforeEach(() => {
21         props = {
22             collectionUuid: 'collection-uuid',
23             sharingURLsPrefix: 'sharing-urls-prefix',
24             sharingTokens: [
25                 {
26                     uuid: 'token-uuid1',
27                     apiToken: 'aaaaaaaaaa',
28                     expiresAt: '2009-01-03T18:15:00Z',
29                 },
30                 {
31                     uuid: 'token-uuid2',
32                     apiToken: 'bbbbbbbbbb',
33                     expiresAt: '2009-01-03T18:15:01Z',
34                 },
35             ],
36             onCopy: jest.fn(),
37             onDeleteSharingToken: jest.fn(),
38         };
39         wrapper = mount(<SharingURLsComponent {...props} />);
40     });
41
42     it("renders a list of sharing URLs", () => {
43         expect(wrapper.find('a').length).toBe(2);
44         // Check 1st URL
45         expect(wrapper.find('a').at(0).text()).toContain(`Token aaaaaaaa... expiring at: ${new Date(props.sharingTokens[0].expiresAt).toLocaleString()}`);
46         expect(wrapper.find('a').at(0).props().href).toBe(`${props.sharingURLsPrefix}/c=${props.collectionUuid}/t=${props.sharingTokens[0].apiToken}/_/`);
47         // Check 2nd URL
48         expect(wrapper.find('a').at(1).text()).toContain(`Token bbbbbbbb... expiring at: ${new Date(props.sharingTokens[1].expiresAt).toLocaleString()}`);
49         expect(wrapper.find('a').at(1).props().href).toBe(`${props.sharingURLsPrefix}/c=${props.collectionUuid}/t=${props.sharingTokens[1].apiToken}/_/`);
50     });
51
52     it("renders a list URLs with collection UUIDs as subdomains", () => {
53         props.sharingURLsPrefix = '*.sharing-urls-prefix';
54         const sharingPrefix = '.sharing-urls-prefix';
55         wrapper = mount(<SharingURLsComponent {...props} />);
56         expect(wrapper.find('a').at(0).props().href).toBe(`${props.collectionUuid}${sharingPrefix}/t=${props.sharingTokens[0].apiToken}/_/`);
57         expect(wrapper.find('a').at(1).props().href).toBe(`${props.collectionUuid}${sharingPrefix}/t=${props.sharingTokens[1].apiToken}/_/`);
58     });
59
60     it("calls delete token handler when delete button is clicked", () => {
61         wrapper.find('button').at(0).simulate('click');
62         expect(props.onDeleteSharingToken).toHaveBeenCalledWith(props.sharingTokens[0].uuid);
63     });
64 });