-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathNewApplicationForm.test.tsx
152 lines (128 loc) · 5.15 KB
/
NewApplicationForm.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import React from 'react';
import { screen } from '@testing-library/react';
import {
NewApplicationForm,
type NewApplicationFormProps,
type ActionableElement,
} from './NewApplicationForm';
import { type User } from 'app-shared/types/Repository';
import { type Organization } from 'app-shared/types/Organization';
import userEvent from '@testing-library/user-event';
import { textMock } from '@studio/testing/mocks/i18nMock';
import type { ServicesContextProps } from 'app-shared/contexts/ServicesContext';
import { renderWithProviders } from '../../testing/mocks';
import { createQueryClientMock } from 'app-shared/mocks/queryClientMock';
import { useUserOrgPermissionQuery } from '../../hooks/queries/useUserOrgPermissionsQuery';
const mockOnSubmit = jest.fn();
const mockUser: User = {
id: 1,
avatar_url: '',
email: '[email protected]',
full_name: 'Test Tester',
login: 'test',
userType: 0,
};
const mockOrg: Organization = {
avatar_url: '',
id: 1,
username: 'unit-test',
full_name: 'unit-test',
};
const mockOrganizations: Organization[] = [mockOrg];
const mockOnClickCancelButton = jest.fn();
const mockCancelComponentButton: ActionableElement = {
onClick: mockOnClickCancelButton,
type: 'button',
};
const mockSubmitbuttonText: string = 'Submit';
const defaultProps: NewApplicationFormProps = {
onSubmit: mockOnSubmit,
user: mockUser,
organizations: mockOrganizations,
isLoading: false,
submitButtonText: mockSubmitbuttonText,
formError: {
org: '',
repoName: '',
},
setFormError: jest.fn(),
actionableElement: mockCancelComponentButton,
};
jest.mock('../../hooks/queries/useUserOrgPermissionsQuery');
(useUserOrgPermissionQuery as jest.Mock).mockReturnValue({
data: { canCreateOrgRepo: true },
});
describe('NewApplicationForm', () => {
afterEach(jest.clearAllMocks);
it('calls onSubmit when form is submitted with valid data', async () => {
const user = userEvent.setup();
renderNewApplicationForm();
const select = screen.getByLabelText(textMock('general.service_owner'));
await user.click(select);
const orgOption = screen.getByRole('option', { name: mockUser.full_name });
await user.click(orgOption);
const repoTextField = screen.getByLabelText(textMock('general.service_name'));
expect(repoTextField).toHaveValue('');
const newRepoValue: string = 'repo';
await user.type(repoTextField, newRepoValue);
expect(screen.getByLabelText(textMock('general.service_name'))).toHaveValue(newRepoValue);
const submitButton = screen.getByRole('button', { name: mockSubmitbuttonText });
await user.click(submitButton);
expect(mockOnSubmit).toHaveBeenCalledTimes(1);
expect(mockOnSubmit).toHaveBeenCalledWith({
org: mockUser.login,
repoName: newRepoValue,
});
});
it('does not call onSubmit when form is submitted with invalid data', async () => {
const user = userEvent.setup();
renderNewApplicationForm();
const select = screen.getByLabelText(textMock('general.service_owner'));
await user.click(select);
const orgOption = screen.getByRole('option', { name: mockUser.full_name });
await user.click(orgOption);
const repoTextField = screen.getByLabelText(textMock('general.service_name'));
await user.click(repoTextField);
const submitButton = screen.getByRole('button', { name: mockSubmitbuttonText });
await user.click(submitButton);
expect(mockOnSubmit).toHaveBeenCalledTimes(0);
});
it('should notify the user if they lack permission to create a new application for the organization and disable the "Create" button', async () => {
const user = userEvent.setup();
(useUserOrgPermissionQuery as jest.Mock).mockReturnValue({
data: { canCreateOrgRepo: false },
});
renderNewApplicationForm(defaultProps);
const serviceOwnerSelect = screen.getByLabelText(textMock('general.service_owner'));
await user.selectOptions(serviceOwnerSelect, mockOrg.username);
expect(
await screen.findByText(textMock('dashboard.missing_service_owner_rights_error_message')),
).toBeInTheDocument();
expect(screen.getByRole('button', { name: mockSubmitbuttonText })).toBeDisabled();
});
it('should enable the "Create" button and not display an error if the user has permission to create an organization', async () => {
const user = userEvent.setup();
(useUserOrgPermissionQuery as jest.Mock).mockReturnValue({
data: { canCreateOrgRepo: true },
});
renderNewApplicationForm(defaultProps);
const serviceOwnerSelect = screen.getByLabelText(textMock('general.service_owner'));
await user.selectOptions(serviceOwnerSelect, mockOrg.username);
expect(
screen.queryByText(textMock('dashboard.missing_service_owner_rights_error_message')),
).not.toBeInTheDocument();
expect(screen.getByRole('button', { name: mockSubmitbuttonText })).toBeEnabled();
});
});
function renderNewApplicationForm(
newApplicationFormProps?: Partial<NewApplicationFormProps>,
services?: Partial<ServicesContextProps>,
) {
return renderWithProviders(
<NewApplicationForm {...defaultProps} {...newApplicationFormProps} />,
{
queries: services,
queryClient: createQueryClientMock(),
},
);
}