Merge branch '21128-toolbar-context-menu'
[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 import { combineReducers, createStore } from 'redux';
20 import { Provider } from 'react-redux';
21
22 configure({ adapter: new Adapter() });
23
24 jest.mock('toggle-selection', () => () => () => null);
25
26 describe('<CurrentTokenDialog />', () => {
27   let props;
28   let wrapper;
29   let store;
30
31   beforeEach(() => {
32     props = {
33       classes: {},
34       token: 'xxxtokenxxx',
35       apiHost: 'example.com',
36       open: true,
37       dispatch: jest.fn(),
38     };
39
40     const initialAuthState = {
41       localCluster: "zzzzz",
42       remoteHostsConfig: {},
43       sessions: {},
44     };
45
46     store = createStore(combineReducers({
47       auth: (state: any = initialAuthState, action: any) => state,
48     }));
49   });
50
51   describe('Get API Token dialog', () => {
52     beforeEach(() => {
53       wrapper = mount(
54         <Provider store={store}>
55           <TokenDialogComponent {...props} />
56         </Provider>
57       );
58     });
59
60     it('should include API host and token', () => {
61       expect(wrapper.html()).toContain('export ARVADOS_API_HOST=example.com');
62       expect(wrapper.html()).toContain('export ARVADOS_API_TOKEN=xxxtokenxxx');
63     });
64
65     it('should show the token expiration if present', () => {
66       expect(props.tokenExpiration).toBeUndefined();
67       expect(wrapper.html()).toContain('This token does not have an expiration date');
68
69       const someDate = '2140-01-01T00:00:00.000Z'
70       props.tokenExpiration = new Date(someDate);
71       wrapper = mount(
72         <Provider store={store}>
73           <TokenDialogComponent {...props} />
74         </Provider>);
75       expect(wrapper.html()).toContain(props.tokenExpiration.toLocaleString());
76     });
77
78     it('should show a create new token button when allowed', () => {
79       expect(props.canCreateNewTokens).toBeFalsy();
80       expect(wrapper.html()).not.toContain('GET NEW TOKEN');
81
82       props.canCreateNewTokens = true;
83       wrapper = mount(
84         <Provider store={store}>
85           <TokenDialogComponent {...props} />
86         </Provider>);
87       expect(wrapper.html()).toContain('GET NEW TOKEN');
88     });
89   });
90
91   describe('copy to clipboard button', () => {
92     beforeEach(() => {
93       wrapper = mount(
94         <Provider store={store}>
95           <TokenDialogComponent {...props} />
96         </Provider>);
97     });
98
99     it('should copy API TOKEN to the clipboard', () => {
100       // when
101       wrapper.find(CopyToClipboard).find(Button).simulate('click');
102
103       // and
104       expect(props.dispatch).toHaveBeenCalledWith({
105         payload: {
106           hideDuration: 2000,
107           kind: 1,
108           message: 'Shell code block copied',
109         },
110         type: 'OPEN_SNACKBAR',
111       });
112     });
113   });
114 });