-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIX] Edit form contains only relationship field of cardinality many …
…if kind is Attribute or Parent (#4229) * Added test on getRelationshipsForForm func when create form * hide relationship field of cardinality many on edit form
- Loading branch information
1 parent
1f32a2a
commit 66a33e2
Showing
7 changed files
with
126 additions
and
26 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
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
19 changes: 19 additions & 0 deletions
19
frontend/app/src/components/form/utils/getRelationshipsForForm.ts
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,19 @@ | ||
import { components } from "@/infraops"; | ||
import { peersKindForForm } from "@/config/constants"; | ||
|
||
export const getRelationshipsForForm = ( | ||
relationships: components["schemas"]["RelationshipSchema-Output"][], | ||
isUpdate?: boolean | ||
) => { | ||
// Filter relationships based on cardinality and kind for form inclusion | ||
// For create forms, include relationships with cardinality 'one', eligible kinds, or mandatory cardinality 'many' | ||
// For update forms, only include relationships with cardinality 'one' or those with eligible kinds (Attribute or Parent). Other should be display as tabs on details view | ||
return relationships.filter((relationship) => { | ||
if (relationship.cardinality === "one") return true; | ||
|
||
const isPeerKindEligibleForForm = peersKindForForm.includes(relationship?.kind ?? ""); | ||
if (isUpdate) return isPeerKindEligibleForForm; | ||
|
||
return isPeerKindEligibleForForm || !relationship.optional; | ||
}); | ||
}; |
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
97 changes: 97 additions & 0 deletions
97
frontend/app/tests/unit/components/form/utils/getRelationshipsForForm.test.ts
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,97 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { getRelationshipsForForm } from "@/components/form/utils/getRelationshipsForForm"; | ||
import { buildRelationshipSchema } from "./getFormFieldsFromSchema.test"; | ||
|
||
describe("getRelationshipsForForm", () => { | ||
it("returns an empty array if the provided relationships array is empty", () => { | ||
// GIVEN | ||
const relationships: never[] = []; | ||
|
||
// WHEN | ||
const result = getRelationshipsForForm(relationships); | ||
|
||
// THEN | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
it("returns a relationship if cardinality is one", () => { | ||
// GIVEN | ||
const relationships = [buildRelationshipSchema({ cardinality: "one" })]; | ||
|
||
// WHEN | ||
const result = getRelationshipsForForm(relationships); | ||
|
||
// THEN | ||
expect(result).toEqual([relationships[0]]); | ||
}); | ||
|
||
it("returns a relationship of cardinality many if kind is Attribute or Parent", () => { | ||
// GIVEN | ||
const relationships = [ | ||
buildRelationshipSchema({ cardinality: "many", kind: "Attribute" }), | ||
buildRelationshipSchema({ cardinality: "many", kind: "Parent" }), | ||
]; | ||
|
||
// WHEN | ||
const result = getRelationshipsForForm(relationships); | ||
|
||
// THEN | ||
expect(result).toEqual(relationships); | ||
}); | ||
|
||
it("should not return a relationship of cardinality many if kind is Generic/Component/Hierarchy", () => { | ||
// GIVEN | ||
const relationships = [ | ||
buildRelationshipSchema({ cardinality: "many", kind: "Generic" }), | ||
buildRelationshipSchema({ cardinality: "many", kind: "Component" }), | ||
buildRelationshipSchema({ cardinality: "many", kind: "Hierarchy" }), | ||
buildRelationshipSchema({ cardinality: "many", kind: "Group" }), | ||
buildRelationshipSchema({ cardinality: "many", kind: "Profile" }), | ||
]; | ||
|
||
// WHEN | ||
const result = getRelationshipsForForm(relationships); | ||
|
||
// THEN | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
it("returns a relationship of cardinality many if it's mandatory field", () => { | ||
// GIVEN | ||
const relationships = [ | ||
buildRelationshipSchema({ cardinality: "many", kind: "Attribute", optional: false }), | ||
buildRelationshipSchema({ cardinality: "many", kind: "Parent", optional: false }), | ||
buildRelationshipSchema({ cardinality: "many", kind: "Generic", optional: false }), | ||
buildRelationshipSchema({ cardinality: "many", kind: "Component", optional: false }), | ||
buildRelationshipSchema({ cardinality: "many", kind: "Hierarchy", optional: false }), | ||
buildRelationshipSchema({ cardinality: "many", kind: "Group", optional: false }), | ||
buildRelationshipSchema({ cardinality: "many", kind: "Profile", optional: false }), | ||
]; | ||
|
||
// WHEN | ||
const result = getRelationshipsForForm(relationships); | ||
|
||
// THEN | ||
expect(result).toEqual(relationships); | ||
}); | ||
|
||
it("When update, returns a mandatory relationship of cardinality is many if kind is Attribute or Parent", () => { | ||
// GIVEN | ||
const relationships = [ | ||
buildRelationshipSchema({ cardinality: "many", kind: "Attribute", optional: false }), | ||
buildRelationshipSchema({ cardinality: "many", kind: "Parent", optional: false }), | ||
buildRelationshipSchema({ cardinality: "many", kind: "Generic", optional: false }), | ||
buildRelationshipSchema({ cardinality: "many", kind: "Component", optional: false }), | ||
buildRelationshipSchema({ cardinality: "many", kind: "Hierarchy", optional: false }), | ||
buildRelationshipSchema({ cardinality: "many", kind: "Group", optional: false }), | ||
buildRelationshipSchema({ cardinality: "many", kind: "Profile", optional: false }), | ||
]; | ||
const isUpdate = true; | ||
|
||
// WHEN | ||
const result = getRelationshipsForForm(relationships, isUpdate); | ||
|
||
// THEN | ||
expect(result).toEqual([relationships[0], relationships[1]]); | ||
}); | ||
}); |