-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Select correct owner when creating a new app (#12612)
* Set selected org as default owner when creating a new app * Add unit tests * Select current user when org is invalid (e.g. all) * Redirect to right context when clicking cancel button * Add missing tests * Move DASHBOARD_ROOT_ROUTE into shared/src/constants.js * Add unit tests for PageLayout
- Loading branch information
Showing
8 changed files
with
216 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
frontend/dashboard/components/ServiceOwnerSelector/ServiceOwnerSelector.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import React from 'react'; | ||
import { render as rtlRender, screen, within } from '@testing-library/react'; | ||
import type { ServiceOwnerSelectorProps } from './ServiceOwnerSelector'; | ||
import { ServiceOwnerSelector } from './ServiceOwnerSelector'; | ||
import { textMock } from '../../../testing/mocks/i18nMock'; | ||
import { user } from 'app-shared/mocks/mocks'; | ||
|
||
const defaultProps = { | ||
selectedOrgOrUser: 'userLogin', | ||
user: { | ||
...user, | ||
login: 'userLogin', | ||
}, | ||
organizations: [ | ||
{ | ||
avatar_url: '', | ||
id: 1, | ||
username: 'organizationUsername', | ||
}, | ||
], | ||
errorMessage: '', | ||
name: '', | ||
}; | ||
|
||
const render = (props: Partial<ServiceOwnerSelectorProps> = {}) => { | ||
rtlRender(<ServiceOwnerSelector {...defaultProps} {...props} />); | ||
}; | ||
|
||
describe('ServiceOwnerSelector', () => { | ||
it('renders select with all options', async () => { | ||
render(); | ||
|
||
const select = screen.getByLabelText(textMock('general.service_owner')); | ||
expect( | ||
within(select).getByRole('option', { name: defaultProps.user.login }), | ||
).toBeInTheDocument(); | ||
expect( | ||
within(select).getByRole('option', { name: defaultProps.organizations[0].username }), | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it('shows validation errors', async () => { | ||
const errorMessage = 'Field cannot be empty'; | ||
|
||
render({ errorMessage }); | ||
|
||
expect(screen.getByText(errorMessage)).toBeInTheDocument(); | ||
}); | ||
|
||
it('selects the org when the current context is the org', async () => { | ||
const selectedOrgOrUser = defaultProps.organizations[0].username; | ||
|
||
render({ selectedOrgOrUser }); | ||
|
||
const select = screen.getByLabelText(textMock('general.service_owner')); | ||
expect(select).toHaveValue(selectedOrgOrUser); | ||
}); | ||
|
||
it('selects the user when the current context is the user', async () => { | ||
const selectedOrgOrUser = defaultProps.user.login; | ||
|
||
render({ selectedOrgOrUser }); | ||
|
||
const select = screen.getByLabelText(textMock('general.service_owner')); | ||
expect(select).toHaveValue(selectedOrgOrUser); | ||
}); | ||
|
||
it('selects the user when the current context is invalid', async () => { | ||
const selectedOrgOrUser = 'all'; | ||
|
||
render({ selectedOrgOrUser }); | ||
|
||
const select = screen.getByLabelText(textMock('general.service_owner')); | ||
expect(select).toHaveValue(defaultProps.user.login); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import { MockServicesContextWrapper } from '../../dashboardTestUtils'; | ||
import type { ServicesContextProps } from 'app-shared/contexts/ServicesContext'; | ||
import { organization, user } from 'app-shared/mocks/mocks'; | ||
import { PageLayout } from './PageLayout'; | ||
import { createQueryClientMock } from 'app-shared/mocks/queryClientMock'; | ||
import { QueryKey } from 'app-shared/types/QueryKey'; | ||
import { DASHBOARD_ROOT_ROUTE } from 'app-shared/constants'; | ||
import { useParams } from 'react-router-dom'; | ||
import { SelectedContextType } from 'app-shared/navigation/main-header/Header'; | ||
|
||
const mockedNavigate = jest.fn(); | ||
jest.mock('react-router-dom', () => ({ | ||
...jest.requireActual('react-router-dom'), | ||
useNavigate: () => mockedNavigate, | ||
useParams: jest.fn(), | ||
})); | ||
|
||
const renderWithMockServices = (services?: Partial<ServicesContextProps>) => { | ||
const queryClient = createQueryClientMock(); | ||
queryClient.setQueryData( | ||
[QueryKey.Organizations], | ||
[ | ||
{ | ||
...organization, | ||
username: 'ttd', | ||
}, | ||
], | ||
); | ||
queryClient.setQueryData([QueryKey.CurrentUser], user); | ||
|
||
render( | ||
<MockServicesContextWrapper customServices={services} client={queryClient}> | ||
<PageLayout /> | ||
</MockServicesContextWrapper>, | ||
); | ||
}; | ||
|
||
describe('PageLayout', () => { | ||
test('should not redirect to root if context is self', async () => { | ||
(useParams as jest.Mock).mockReturnValue({ | ||
selectedContext: SelectedContextType.Self, | ||
}); | ||
renderWithMockServices(); | ||
expect(mockedNavigate).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('should not redirect to root if context is all', async () => { | ||
(useParams as jest.Mock).mockReturnValue({ | ||
selectedContext: SelectedContextType.All, | ||
}); | ||
renderWithMockServices(); | ||
expect(mockedNavigate).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('should not redirect to root if user have access to selected context', async () => { | ||
(useParams as jest.Mock).mockReturnValue({ | ||
selectedContext: 'ttd', | ||
}); | ||
renderWithMockServices(); | ||
expect(mockedNavigate).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('should redirect to root if user does not have access to selected context', async () => { | ||
(useParams as jest.Mock).mockReturnValue({ | ||
selectedContext: 'test', | ||
}); | ||
renderWithMockServices(); | ||
expect(mockedNavigate).toHaveBeenCalledTimes(1); | ||
expect(mockedNavigate).toHaveBeenCalledWith(DASHBOARD_ROOT_ROUTE); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters