Merge branch 'main' into 21720-material-ui-upgrade
[arvados.git] / services / workbench2 / src / views-components / sharing-dialog / sharing-urls-component.cy.js
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 { SharingURLsComponent } from './sharing-urls-component';
7 import { ThemeProvider } from "@mui/material";
8 import { CustomTheme } from 'common/custom-theme';
9 import { Provider } from "react-redux";
10 import { configureStore } from "store/store";
11 import { createBrowserHistory } from "history";
12
13 describe("<SharingURLsComponent />", () => {
14     let props;
15     const store = configureStore(createBrowserHistory());
16
17     beforeEach(() => {
18         props = {
19             collectionUuid: 'collection-uuid',
20             sharingURLsPrefix: 'sharing-urls-prefix',
21             sharingTokens: [
22                 {
23                     uuid: 'token-uuid1',
24                     apiToken: 'aaaaaaaaaa',
25                     expiresAt: '2009-01-03T18:15:00Z',
26                 },
27                 {
28                     uuid: 'token-uuid2',
29                     apiToken: 'bbbbbbbbbb',
30                     expiresAt: '2009-01-03T18:15:01Z',
31                 },
32             ],
33             onCopy: cy.stub(),
34             onDeleteSharingToken: cy.stub().as('onDeleteSharingToken'),
35         };
36         cy.mount(
37             <Provider store={store}>
38                 <ThemeProvider theme={CustomTheme}>
39                     <SharingURLsComponent {...props} />
40                 </ThemeProvider>
41             </Provider>);
42     });
43
44     it("renders a list of sharing URLs", () => {
45         // Check number of URLs
46         cy.get('a').should('have.length', 2);
47         // Check 1st URL
48         cy.get('a').eq(0).should('contain', `Token aaaaaaaa... expiring at: ${new Date(props.sharingTokens[0].expiresAt).toLocaleString()}`);
49         cy.get('a').eq(0).should('have.attr', 'href', `${props.sharingURLsPrefix}/c=${props.collectionUuid}/t=${props.sharingTokens[0].apiToken}/_/`);
50         // Check 2nd URL
51         cy.get('a').eq(1).should('contain', `Token bbbbbbbb... expiring at: ${new Date(props.sharingTokens[1].expiresAt).toLocaleString()}`);
52         cy.get('a').eq(1).should('have.attr', 'href', `${props.sharingURLsPrefix}/c=${props.collectionUuid}/t=${props.sharingTokens[1].apiToken}/_/`);
53     });
54
55     it("renders a list URLs with collection UUIDs as subdomains", () => {
56         props.sharingURLsPrefix = '*.sharing-urls-prefix';
57         const sharingPrefix = '.sharing-urls-prefix';
58         cy.mount(
59             <Provider store={store}>
60                 <ThemeProvider theme={CustomTheme}>
61                     <SharingURLsComponent {...props} />
62                 </ThemeProvider>
63             </Provider>);
64
65         cy.get('a').eq(0).should('have.attr', 'href', `${props.collectionUuid}${sharingPrefix}/t=${props.sharingTokens[0].apiToken}/_/`);
66         cy.get('a').eq(1).should('have.attr', 'href', `${props.collectionUuid}${sharingPrefix}/t=${props.sharingTokens[1].apiToken}/_/`);
67     });
68
69     it("renders a list of URLs with no expiration", () => {
70         props.sharingTokens[0].expiresAt = null;
71         props.sharingTokens[1].expiresAt = null;
72         cy.mount(
73             <Provider store={store}>
74                 <ThemeProvider theme={CustomTheme}>
75                     <SharingURLsComponent {...props} />
76                 </ThemeProvider>
77             </Provider>);
78         cy.get('a').eq(0).should('contain', `Token aaaaaaaa... with no expiration date`);
79         cy.get('a').eq(1).should('contain', `Token bbbbbbbb... with no expiration date`);
80     });
81
82     it("calls delete token handler when delete button is clicked", () => {
83         cy.get('button').eq(0).click();
84         cy.get('@onDeleteSharingToken').should('be.calledWith', props.sharingTokens[0].uuid);
85     });
86 });