1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-05-12 01:17:04 +02:00

fix: don't send change request info unless using the new form (#7102)

I realized that, in an oversight, the form now shows and sends project
CR config, even if the new form isn't active. The API just ignores it if
it doesn't understand it, so it's not very harmful, but it's better if
we don't send it at all. This PR does that.

It does not actually test that change request info isn't included (but
it does test ID inclusion). This is because:
- change request info is only included if we're enterprise. The rendered
version of the hook isn't by default.
- Setting up module mocking and making it work seems like a lot of work
for a small gain, considering we're probably going to be removing the
old form anyway.
- I've tested it locally.

Also adds some testing for the hook related to name validation and
payload creation
This commit is contained in:
Thomas Heartman 2024-05-22 08:44:39 +02:00 committed by GitHub
parent 8542cafc9f
commit 14fd624faa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 40 additions and 7 deletions

View File

@ -75,11 +75,16 @@ export const CreateProjectDialogue = ({
const clearDocumentationOverride = () =>
setDocumentation(generalDocumentation);
const projectPayload = getCreateProjectPayload({
omitId: true,
includeChangeRequestConfig: true,
});
const formatApiCode = () => {
return `curl --location --request POST '${uiConfig.unleashUrl}/api/admin/projects' \\
--header 'Authorization: INSERT_API_KEY' \\
--header 'Content-Type: application/json' \\
--data-raw '${JSON.stringify(getCreateProjectPayload(), undefined, 2)}'`;
--data-raw '${JSON.stringify(projectPayload, undefined, 2)}'`;
};
const handleSubmit = async (e: Event) => {
@ -88,11 +93,8 @@ export const CreateProjectDialogue = ({
const validName = validateName();
if (validName) {
const payload = getCreateProjectPayload({
omitId: true,
});
try {
const createdProject = await createProject(payload);
const createdProject = await createProject(projectPayload);
refetchUser();
navigate(`/projects/${createdProject.id}`, { replace: true });
setToastData({

View File

@ -35,3 +35,29 @@ test(`adding a change request config for an env not in the project envs doesn't
'dev' in result.current.projectChangeRequestConfiguration,
).toBeFalsy();
});
describe('payload generation', () => {
test(`id is omitted only when explicitly asked to be`, () => {
const { result } = renderHook(() => useProjectForm());
const payloadWithId = result.current.getCreateProjectPayload();
expect('id' in payloadWithId).toBeTruthy();
const payloadWithoutId = result.current.getCreateProjectPayload({
omitId: true,
});
expect('id' in payloadWithoutId).toBeFalsy();
});
});
describe('name validation', () => {
test.each([
['An empty string', ''],
['Just whitespace', ' '],
])(`%s is not valid`, (_, value) => {
const { result } = renderHook(() => useProjectForm());
result.current.setProjectName(value);
expect(result.current.validateName()).toBeFalsy();
});
});

View File

@ -94,7 +94,10 @@ const useProjectForm = (
setProjectMode(initialProjectMode);
}, [initialProjectMode]);
const getCreateProjectPayload = (options?: { omitId?: boolean }) => {
const getCreateProjectPayload = (options?: {
omitId?: boolean;
includeChangeRequestConfig?: boolean;
}) => {
const environmentsPayload =
projectEnvironments.size > 0
? { environments: [...projectEnvironments] }
@ -119,7 +122,9 @@ const useProjectForm = (
? {
...ossPayload,
mode: projectMode,
changeRequestEnvironments,
...(options?.includeChangeRequestConfig
? { changeRequestEnvironments }
: {}),
}
: ossPayload;
};