|
| 1 | +/** |
| 2 | + * @file Defines integration tests for all sub-components in the |
| 3 | + * CoderWorkspacesCard directory. |
| 4 | + */ |
| 5 | +import React from 'react'; |
| 6 | +import { screen, waitFor } from '@testing-library/react'; |
| 7 | +import { renderInCoderEnvironment } from '../../testHelpers/setup'; |
| 8 | +import { mockAuthStates } from '../../testHelpers/mockBackstageData'; |
| 9 | +import { |
| 10 | + mockWorkspaceNoParameters, |
| 11 | + mockWorkspaceWithMatch2, |
| 12 | + mockWorkspacesList, |
| 13 | +} from '../../testHelpers/mockCoderAppData'; |
| 14 | +import { type CoderAuthStatus } from '../CoderProvider'; |
| 15 | +import { CoderWorkspacesCard } from './CoderWorkspacesCard'; |
| 16 | +import userEvent from '@testing-library/user-event'; |
| 17 | + |
| 18 | +type RenderInputs = Readonly<{ |
| 19 | + authStatus?: CoderAuthStatus; |
| 20 | + readEntityData?: boolean; |
| 21 | +}>; |
| 22 | + |
| 23 | +function renderWorkspacesCard(input?: RenderInputs) { |
| 24 | + const { authStatus = 'authenticated', readEntityData = false } = input ?? {}; |
| 25 | + |
| 26 | + return renderInCoderEnvironment({ |
| 27 | + auth: mockAuthStates[authStatus], |
| 28 | + children: <CoderWorkspacesCard readEntityData={readEntityData} />, |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +const matchers = { |
| 33 | + authenticationForm: /Authenticate with Coder/i, |
| 34 | + searchTitle: /Coder Workspaces/i, |
| 35 | + searchbox: /Search your Coder workspaces/i, |
| 36 | + emptyState: /Use the search bar to find matching Coder workspaces/i, |
| 37 | +} as const satisfies Record<string, RegExp>; |
| 38 | + |
| 39 | +describe(`${CoderWorkspacesCard.name}`, () => { |
| 40 | + describe('General behavior', () => { |
| 41 | + it('Shows the authentication form when the user is not authenticated', async () => { |
| 42 | + await renderWorkspacesCard({ |
| 43 | + authStatus: 'tokenMissing', |
| 44 | + }); |
| 45 | + |
| 46 | + expect(() => { |
| 47 | + screen.getByRole('form', { name: matchers.authenticationForm }); |
| 48 | + }).not.toThrow(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('Shows the workspaces list when the user is authenticated (exposed as an accessible search landmark)', async () => { |
| 52 | + await renderWorkspacesCard(); |
| 53 | + |
| 54 | + await waitFor(() => { |
| 55 | + expect(() => { |
| 56 | + screen.getByRole('search', { name: matchers.searchTitle }); |
| 57 | + }).not.toThrow(); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + it('Shows zero workspaces when the query text matches nothing', async () => { |
| 62 | + const entityValues = [true, false] as const; |
| 63 | + const user = userEvent.setup(); |
| 64 | + |
| 65 | + for (const value of entityValues) { |
| 66 | + const { unmount } = await renderWorkspacesCard({ |
| 67 | + readEntityData: value, |
| 68 | + }); |
| 69 | + |
| 70 | + const searchbox = await screen.findByRole('searchbox', { |
| 71 | + name: matchers.searchbox, |
| 72 | + }); |
| 73 | + |
| 74 | + await user.tripleClick(searchbox); |
| 75 | + await user.keyboard('[Backspace]'); |
| 76 | + await user.keyboard('I can do it - I can do it nine times'); |
| 77 | + |
| 78 | + await waitFor(() => { |
| 79 | + // getAllByRole will throw if there isn't at least one node matched |
| 80 | + const listItems = screen.queryAllByRole('listitem'); |
| 81 | + expect(listItems.length).toBe(0); |
| 82 | + }); |
| 83 | + |
| 84 | + unmount(); |
| 85 | + } |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe('With readEntityData set to false', () => { |
| 90 | + it('Will NOT filter any workspaces by the current repo', async () => { |
| 91 | + await renderWorkspacesCard({ readEntityData: false }); |
| 92 | + const workspaceItems = await screen.findAllByRole('listitem'); |
| 93 | + expect(workspaceItems.length).toEqual(mockWorkspacesList.length); |
| 94 | + }); |
| 95 | + |
| 96 | + it('Lets the user filter the workspaces by their query text', async () => { |
| 97 | + await renderWorkspacesCard({ readEntityData: false }); |
| 98 | + const searchbox = await screen.findByRole('searchbox', { |
| 99 | + name: matchers.searchbox, |
| 100 | + }); |
| 101 | + |
| 102 | + const user = userEvent.setup(); |
| 103 | + await user.tripleClick(searchbox); |
| 104 | + await user.keyboard(mockWorkspaceNoParameters.name); |
| 105 | + |
| 106 | + // If more than one workspace matches, that throws an error |
| 107 | + const onlyWorkspace = await screen.findByRole('listitem'); |
| 108 | + expect(onlyWorkspace).toHaveTextContent(mockWorkspaceNoParameters.name); |
| 109 | + }); |
| 110 | + |
| 111 | + it('Shows all workspaces when query text is empty', async () => { |
| 112 | + await renderWorkspacesCard({ readEntityData: false }); |
| 113 | + const searchbox = await screen.findByRole('searchbox', { |
| 114 | + name: matchers.searchbox, |
| 115 | + }); |
| 116 | + |
| 117 | + const user = userEvent.setup(); |
| 118 | + await user.tripleClick(searchbox); |
| 119 | + await user.keyboard('[Backspace]'); |
| 120 | + |
| 121 | + const allWorkspaces = await screen.findAllByRole('listitem'); |
| 122 | + expect(allWorkspaces.length).toEqual(mockWorkspacesList.length); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + describe('With readEntityData set to true', () => { |
| 127 | + it('Will show only the workspaces that match the current repo', async () => { |
| 128 | + await renderWorkspacesCard({ readEntityData: true }); |
| 129 | + const workspaceItems = await screen.findAllByRole('listitem'); |
| 130 | + expect(workspaceItems.length).toEqual(2); |
| 131 | + }); |
| 132 | + |
| 133 | + it('Lets the user filter the workspaces by their query text (on top of filtering from readEntityData)', async () => { |
| 134 | + await renderWorkspacesCard({ readEntityData: true }); |
| 135 | + |
| 136 | + await waitFor(() => { |
| 137 | + const workspaceItems = screen.getAllByRole('listitem'); |
| 138 | + expect(workspaceItems.length).toBe(2); |
| 139 | + }); |
| 140 | + |
| 141 | + const user = userEvent.setup(); |
| 142 | + const searchbox = await screen.findByRole('searchbox', { |
| 143 | + name: matchers.searchbox, |
| 144 | + }); |
| 145 | + |
| 146 | + await user.tripleClick(searchbox); |
| 147 | + await user.keyboard(mockWorkspaceWithMatch2.name); |
| 148 | + |
| 149 | + await waitFor(() => { |
| 150 | + const newWorkspaceItems = screen.getAllByRole('listitem'); |
| 151 | + expect(newWorkspaceItems.length).toBe(1); |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + /** |
| 156 | + * 2024-03-28 - MES - This is a test case to account for a previous |
| 157 | + * limitation around querying workspaces by repo URL. |
| 158 | + * |
| 159 | + * This limitation no longer exists, so this test should be removed once the |
| 160 | + * rest of the codebase is updated to support the new API endpoint for |
| 161 | + * searching by build parameter |
| 162 | + */ |
| 163 | + it('Will not show any workspaces at all when the query text is empty', async () => { |
| 164 | + await renderWorkspacesCard({ readEntityData: true }); |
| 165 | + |
| 166 | + const user = userEvent.setup(); |
| 167 | + const searchbox = await screen.findByRole('searchbox', { |
| 168 | + name: matchers.searchbox, |
| 169 | + }); |
| 170 | + |
| 171 | + await user.tripleClick(searchbox); |
| 172 | + await user.keyboard('[Backspace]'); |
| 173 | + |
| 174 | + const emptyState = await screen.findByText(matchers.emptyState); |
| 175 | + expect(emptyState).toBeInTheDocument(); |
| 176 | + }); |
| 177 | + }); |
| 178 | +}); |
0 commit comments