Skip to content

Commit

Permalink
Tests and storybook
Browse files Browse the repository at this point in the history
  • Loading branch information
TrangPham committed Mar 19, 2024
1 parent 3363409 commit 03d9164
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 520 deletions.
37 changes: 31 additions & 6 deletions src/controls/ArrayControl.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
stringArrayControlJsonSchema,
stringArrayControlJsonSchemaWithRequired,
stringArrayControlJsonSchemaWithTitle,
numberArrayControlJsonSchema,
} from "../testSchemas/arraySchema"

describe("ArrayControl for Objects", () => {
Expand Down Expand Up @@ -58,8 +59,13 @@ describe("ArrayControl for Objects", () => {
await screen.findByDisplayValue("my asset")
await screen.findByDisplayValue("my other asset")
//note: the text is within a span in the <button>
const removeButton = (await screen.findAllByText("Delete"))[0].parentNode
expect(removeButton).toHaveProperty("disabled", false)
const removeButtons = await screen.findAllByRole("button", {
name: "Delete",
})
expect(removeButtons).toHaveLength(2)
removeButtons.forEach((removeButton) => {
expect(removeButton).toHaveProperty("disabled", false)
})
},
)

Expand Down Expand Up @@ -207,8 +213,13 @@ describe("ArrayControl for Primatives", () => {
await screen.findByDisplayValue("my asset")
await screen.findByDisplayValue("my other asset")
//note: the text is within a span in the <button>
const removeButton = (await screen.findAllByText("Delete"))[0].parentNode
expect(removeButton).toHaveProperty("disabled", false)
const removeButtons = await screen.findAllByRole("button", {
name: "Delete",
})
expect(removeButtons).toHaveLength(2)
removeButtons.forEach((removeButton) => {
expect(removeButton).toHaveProperty("disabled", false)
})
},
)

Expand Down Expand Up @@ -307,8 +318,7 @@ describe("ArrayControl for Primatives", () => {
})

test("renders with title", async () => {
const user = userEvent.setup()
let data = { assets: ["apple"] }
const data = { assets: ["apple"] }
render({
schema: stringArrayControlJsonSchemaWithTitle,
uischema: arrayControlUISchema,
Expand All @@ -324,4 +334,19 @@ describe("ArrayControl for Primatives", () => {
const textbox = await screen.findByLabelText("Use this as the label")
expect(textbox).toHaveProperty("value", "apple")
})

test("Primitive Number Array should not error if item is null", async () => {
const user = userEvent.setup()
const data = { assets: [0] }
render({
schema: numberArrayControlJsonSchema,
uischema: arrayControlUISchema,
data: data,
})

await screen.findByDisplayValue("0")
// Antd List component does not like null or undefined values, and will error if they are present
// This test is to ensure that the component does not error if the value is null
await user.clear(screen.getByDisplayValue("0"))
})
})
32 changes: 25 additions & 7 deletions src/controls/ArrayControl.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import {
Helpers,
ArrayLayoutProps,
Expand All @@ -7,7 +11,11 @@ import {
createDefaultValue,
findUISchema,
} from "@jsonforms/core"
import { JsonFormsDispatch } from "@jsonforms/react"
import {
JsonFormsDispatch,
withJsonFormsArrayControlProps,
withJsonFormsArrayLayoutProps,
} from "@jsonforms/react"
import { Flex, List, Button } from "antd"
import range from "lodash.range"
import { useCallback, useMemo } from "react"
Expand Down Expand Up @@ -59,7 +67,11 @@ export function ArrayControl({

// Note: For primative arrays, ArrayControlProps.data is an array
// For object arrays, ArrayLayoutProps.data is a number
const dataSource = typeof data === "number" ? range(data) : data
const dataSource: any[] =
typeof data === "number"
? range(data)
: // antd List component doesn't like undefined/null in the dataSource
data?.map((item: any) => item ?? "")
const dataLength = typeof data === "number" ? data : data?.length || 0

const renderItem = (_item: number, index: number) => {
Expand All @@ -69,7 +81,6 @@ export function ArrayControl({
actions={[
<Button
key="remove"
children="Delete"
{...options.removeButtonProps}
disabled={
!removeItems || (required && dataLength == 1 && index === 0)
Expand All @@ -78,7 +89,9 @@ export function ArrayControl({
e.stopPropagation()
removeItems?.(path, [index])()
}}
/>,
>
{options.removeButtonProps?.children ?? "Delete"}
</Button>,
]}
>
<div style={{ width: "100%" }}>
Expand All @@ -99,13 +112,14 @@ export function ArrayControl({
const addButton = (
<Flex justify="center">
<Button
children={`Add ${label}`}
{...options.addButtonProps}
onClick={(e) => {
e.stopPropagation()
addItem(path, innerCreateDefaultValue())()
}}
/>
>
{options.addButtonProps?.children || `Add ${label}`}
</Button>
</Flex>
)

Expand All @@ -122,3 +136,7 @@ export function ArrayControl({
</>
)
}

export const ObjectArrayRenderer = withJsonFormsArrayLayoutProps(ArrayControl)
export const PrimitiveArrayRenderer =
withJsonFormsArrayControlProps(ArrayControl)
160 changes: 0 additions & 160 deletions src/controls/ObjectArrayControl.test.tsx

This file was deleted.

Loading

0 comments on commit 03d9164

Please sign in to comment.