|
| 1 | +import { asMockedFn, partial } from '@terra-ui-packages/test-utils'; |
| 2 | +import { screen } from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | +import { h } from 'react-hyperscript-helpers'; |
| 5 | +import { Metrics, MetricsContract } from 'src/libs/ajax/Metrics'; |
| 6 | +import Events, { extractWorkspaceDetails } from 'src/libs/events'; |
| 7 | +import { renderWithAppContexts as render } from 'src/testing/test-utils'; |
| 8 | +import { defaultGoogleWorkspace } from 'src/testing/workspace-fixtures'; |
| 9 | +import { Quota } from 'src/workspaces/dashboard/Quota'; |
| 10 | + |
| 11 | +jest.mock('src/libs/ajax/Metrics'); |
| 12 | +jest.mock('src/libs/ajax/workspaces/Workspaces'); |
| 13 | + |
| 14 | +describe('Quota', () => { |
| 15 | + afterEach(() => { |
| 16 | + jest.resetAllMocks(); |
| 17 | + }); |
| 18 | + |
| 19 | + it('displays the quota section with view quotas link when user is a writer with canCompute', () => { |
| 20 | + // Arrange |
| 21 | + const workspace = { ...defaultGoogleWorkspace, workspaceInitialized: true }; |
| 22 | + |
| 23 | + // Act |
| 24 | + render(h(Quota, { workspace })); |
| 25 | + |
| 26 | + // Assert |
| 27 | + const docLink = screen.getByText('View quotas'); |
| 28 | + expect(docLink).toBeInTheDocument(); |
| 29 | + expect(docLink.closest('a')).toHaveAttribute( |
| 30 | + 'href', |
| 31 | + 'https://console.cloud.google.com/iam-admin/quotas?project=test-gcp-ws-project&authuser=undefined' |
| 32 | + ); |
| 33 | + }); |
| 34 | + |
| 35 | + it('shows enabled quota adjuster link for owners', () => { |
| 36 | + // Act |
| 37 | + render(h(Quota, { workspace: { ...defaultGoogleWorkspace, accessLevel: 'OWNER', workspaceInitialized: true } })); |
| 38 | + |
| 39 | + // Assert |
| 40 | + const quotaAdjusterLink = screen.getByText('Open quota adjuster'); |
| 41 | + expect(quotaAdjusterLink).toBeInTheDocument(); |
| 42 | + |
| 43 | + const linkElement = quotaAdjusterLink.closest('a'); |
| 44 | + expect(linkElement).toHaveAttribute( |
| 45 | + 'href', |
| 46 | + 'https://console.cloud.google.com/iam-admin/quotas/configurations?project=test-gcp-ws-project&authuser=undefined' |
| 47 | + ); |
| 48 | + expect(linkElement).not.toHaveStyle({ pointerEvents: 'none' }); |
| 49 | + }); |
| 50 | + |
| 51 | + it('shows disabled quota adjuster link with tooltip for non-owners', async () => { |
| 52 | + // Arrange |
| 53 | + const user = userEvent.setup(); |
| 54 | + |
| 55 | + // Act |
| 56 | + render(h(Quota, { workspace: { ...defaultGoogleWorkspace, workspaceInitialized: true, accessLevel: 'WRITER' } })); |
| 57 | + |
| 58 | + // Assert |
| 59 | + const quotaAdjusterLink = screen.getByText('Open quota adjuster'); |
| 60 | + expect(quotaAdjusterLink).toBeInTheDocument(); |
| 61 | + |
| 62 | + const linkElement = quotaAdjusterLink.closest('a'); |
| 63 | + expect(linkElement).toHaveStyle({ pointerEvents: 'none' }); |
| 64 | + |
| 65 | + // Hover over the link to trigger tooltip |
| 66 | + const spanWrapper = linkElement?.closest('span'); |
| 67 | + await user.hover(spanWrapper!); |
| 68 | + |
| 69 | + // Check that tooltip appears |
| 70 | + const tooltip = await screen.findByRole('tooltip'); |
| 71 | + expect(tooltip).toHaveTextContent( |
| 72 | + 'You do not have permission to adjust quotas for this project. Please contact your workspace owner(s) for assistance.' |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + it('emits an event when view quotas link is clicked', async () => { |
| 77 | + // Arrange |
| 78 | + const user = userEvent.setup(); |
| 79 | + const captureEvent = jest.fn(); |
| 80 | + const workspace = { ...defaultGoogleWorkspace, workspaceInitialized: true }; |
| 81 | + |
| 82 | + asMockedFn(Metrics).mockReturnValue(partial<MetricsContract>({ captureEvent })); |
| 83 | + |
| 84 | + // Act |
| 85 | + render(h(Quota, { workspace })); |
| 86 | + const viewQuotasLink = screen.getByText('View quotas'); |
| 87 | + await user.click(viewQuotasLink); |
| 88 | + |
| 89 | + // Assert |
| 90 | + expect(captureEvent).toHaveBeenCalledWith( |
| 91 | + Events.workspaceOpenQuotaInConsole, |
| 92 | + extractWorkspaceDetails(defaultGoogleWorkspace) |
| 93 | + ); |
| 94 | + }); |
| 95 | + |
| 96 | + it('shows disabled view quotas link with tooltip when user does not have canCompute', async () => { |
| 97 | + // Arrange |
| 98 | + const user = userEvent.setup(); |
| 99 | + |
| 100 | + // Act |
| 101 | + render( |
| 102 | + h(Quota, { |
| 103 | + workspace: { |
| 104 | + ...defaultGoogleWorkspace, |
| 105 | + workspaceInitialized: true, |
| 106 | + accessLevel: 'WRITER', |
| 107 | + canCompute: false, |
| 108 | + }, |
| 109 | + }) |
| 110 | + ); |
| 111 | + |
| 112 | + // Assert |
| 113 | + const viewQuotasLink = screen.getByText('View quotas'); |
| 114 | + expect(viewQuotasLink).toBeInTheDocument(); |
| 115 | + |
| 116 | + const linkElement = viewQuotasLink.closest('a'); |
| 117 | + expect(linkElement).toHaveStyle({ pointerEvents: 'none' }); |
| 118 | + |
| 119 | + // Hover over the link to trigger tooltip |
| 120 | + const spanWrapper = linkElement?.closest('span'); |
| 121 | + await user.hover(spanWrapper!); |
| 122 | + |
| 123 | + // Check that tooltip appears |
| 124 | + const tooltip = await screen.findByRole('tooltip'); |
| 125 | + expect(tooltip).toHaveTextContent( |
| 126 | + 'You do not have permission to view quotas for this project. Please contact your workspace owner(s) for assistance.' |
| 127 | + ); |
| 128 | + }); |
| 129 | + |
| 130 | + it('renders quota documentation link in info box', async () => { |
| 131 | + // Arrange |
| 132 | + const user = userEvent.setup(); |
| 133 | + const workspace = { ...defaultGoogleWorkspace, workspaceInitialized: true }; |
| 134 | + |
| 135 | + // Act |
| 136 | + render(h(Quota, { workspace })); |
| 137 | + const infoButton = screen.getByLabelText('More info'); |
| 138 | + await user.click(infoButton); |
| 139 | + |
| 140 | + // Assert |
| 141 | + expect(screen.getByText('For more information, please refer to the')).toBeInTheDocument(); |
| 142 | + const docLink = screen.getByText('resource quota documentation'); |
| 143 | + expect(docLink).toBeInTheDocument(); |
| 144 | + expect(docLink.closest('a')).toHaveAttribute( |
| 145 | + 'href', |
| 146 | + 'https://support.terra.bio/hc/en-us/articles/6396351981595-Are-resource-quotas-slowing-your-analysis-down' |
| 147 | + ); |
| 148 | + }); |
| 149 | +}); |
0 commit comments