17782: Fixes a jest warning when running tests.
[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 // This mocks react-copy-to-clipboard's dependency module to avoid warnings
6 // from jest when running tests. As we're not testing copy-to-clipboard, it's
7 // safe to just mock it.
8 // https://github.com/nkbt/react-copy-to-clipboard/issues/106#issuecomment-605227151
9 jest.mock('copy-to-clipboard', () => {
10   return jest.fn();
11 });
12
13 import React from 'react';
14 import { Button } from '@material-ui/core';
15 import { mount, configure } from 'enzyme';
16 import Adapter from 'enzyme-adapter-react-16';
17 import CopyToClipboard from 'react-copy-to-clipboard';
18 import { TokenDialogComponent } from './token-dialog';
19
20 configure({ adapter: new Adapter() });
21
22 jest.mock('toggle-selection', () => () => () => null);
23
24 describe('<CurrentTokenDialog />', () => {
25   let props;
26   let wrapper;
27
28   beforeEach(() => {
29     props = {
30       classes: {},
31       token: 'xxxtokenxxx',
32       apiHost: 'example.com',
33       open: true,
34       dispatch: jest.fn(),
35     };
36   });
37
38   describe('Get API Token dialog', () => {
39     beforeEach(() => {
40       wrapper = mount(<TokenDialogComponent {...props} />);
41     });
42
43     it('should include API host and token', () => {
44       expect(wrapper.html()).toContain('export ARVADOS_API_HOST=example.com');
45       expect(wrapper.html()).toContain('export ARVADOS_API_TOKEN=xxxtokenxxx');
46     });
47
48     it('should show the token expiration if present', () => {
49       expect(props.tokenExpiration).toBeUndefined();
50       expect(wrapper.html()).toContain('This token does not have an expiration date');
51
52       const someDate = '2140-01-01T00:00:00.000Z'
53       props.tokenExpiration = new Date(someDate);
54       wrapper = mount(<TokenDialogComponent {...props} />);
55       expect(wrapper.html()).toContain(props.tokenExpiration.toLocaleString());
56     });
57
58     it('should show a create new token button when allowed', () => {
59       expect(props.canCreateNewTokens).toBeFalsy();
60       expect(wrapper.html()).not.toContain('GET NEW TOKEN');
61
62       props.canCreateNewTokens = true;
63       wrapper = mount(<TokenDialogComponent {...props} />);
64       expect(wrapper.html()).toContain('GET NEW TOKEN');
65     });
66   });
67
68   describe('copy to clipboard button', () => {
69     beforeEach(() => {
70       wrapper = mount(<TokenDialogComponent {...props} />);
71     });
72
73     it('should copy API TOKEN to the clipboard', () => {
74       // when
75       wrapper.find(CopyToClipboard).find(Button).simulate('click');
76
77       // and
78       expect(props.dispatch).toHaveBeenCalledWith({
79         payload: {
80           hideDuration: 2000,
81           kind: 1,
82           message: 'Shell code block copied',
83         },
84         type: 'OPEN_SNACKBAR',
85       });
86     });
87   });
88 });