d95486128e7480c0eb7ab37bf70b50590a83b35c
[arvados-workbench2.git] / src / views-components / token-dialog / token-dialog.test.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import { Button } from '@material-ui/core';
7 import { mount, configure } from 'enzyme';
8 import * as Adapter from 'enzyme-adapter-react-16';
9 import * as CopyToClipboard from 'react-copy-to-clipboard';
10 import { TokenDialogComponent } from './token-dialog';
11
12 configure({ adapter: new Adapter() });
13
14 jest.mock('toggle-selection', () => () => () => null);
15
16 describe('<CurrentTokenDialog />', () => {
17   let props;
18   let wrapper;
19
20   beforeEach(() => {
21     props = {
22       classes: {},
23       token: 'xxxtokenxxx',
24       apiHost: 'example.com',
25       open: true,
26       dispatch: jest.fn(),
27     };
28   });
29
30   describe('Get API Token dialog', () => {
31     beforeEach(() => {
32       wrapper = mount(<TokenDialogComponent {...props} />);
33     });
34
35     it('should include API host and token', () => {
36       expect(wrapper.html()).toContain('export ARVADOS_API_HOST=example.com');
37       expect(wrapper.html()).toContain('export ARVADOS_API_TOKEN=xxxtokenxxx');
38     });
39
40     it('should show the token expiration if present', () => {
41       expect(props.tokenExpiration).toBeUndefined();
42       expect(wrapper.html()).toContain('This token does not have an expiration date');
43
44       const someDate = '2140-01-01T00:00:00.000Z'
45       props.tokenExpiration = new Date(someDate);
46       wrapper = mount(<TokenDialogComponent {...props} />);
47       expect(wrapper.html()).toContain(props.tokenExpiration.toLocaleString());
48     });
49
50     it('should show a create new token button when allowed', () => {
51       expect(props.canCreateNewTokens).toBeFalsy();
52       expect(wrapper.html()).not.toContain('GET NEW TOKEN');
53
54       props.canCreateNewTokens = true;
55       wrapper = mount(<TokenDialogComponent {...props} />);
56       expect(wrapper.html()).toContain('GET NEW TOKEN');
57     });
58   });
59
60   describe('copy to clipboard button', () => {
61     beforeEach(() => {
62       wrapper = mount(<TokenDialogComponent {...props} />);
63     });
64
65     it('should copy API TOKEN to the clipboard', () => {
66       // when
67       wrapper.find(CopyToClipboard).find(Button).simulate('click');
68
69       // and
70       expect(props.dispatch).toHaveBeenCalledWith({
71         payload: {
72           hideDuration: 2000,
73           kind: 1,
74           message: 'Shell code block copied',
75         },
76         type: 'OPEN_SNACKBAR',
77       });
78     });
79   });
80 });