2023-01-16 09:45:12 +00:00
|
|
|
import useForm from "@dashboard/hooks/useForm";
|
2020-08-28 12:45:11 +00:00
|
|
|
import Wrapper from "@test/wrapper";
|
2022-12-07 10:20:09 +00:00
|
|
|
import { render, screen } from "@testing-library/react";
|
|
|
|
import userEvent from "@testing-library/user-event";
|
2020-08-28 12:45:11 +00:00
|
|
|
import React from "react";
|
|
|
|
|
|
|
|
import { props } from "./fixtures";
|
2023-05-04 08:57:18 +00:00
|
|
|
import { Metadata } from "./Metadata";
|
2020-08-28 12:45:11 +00:00
|
|
|
|
2022-12-07 10:20:09 +00:00
|
|
|
const Component = () => {
|
2020-08-28 12:45:11 +00:00
|
|
|
const { change, data } = useForm(props.data, jest.fn());
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Wrapper>
|
|
|
|
<Metadata data={data} onChange={change} />
|
|
|
|
</Wrapper>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-12-07 10:20:09 +00:00
|
|
|
const getFirstExpandIcon = () => screen.getAllByTestId("expand")[0];
|
2020-08-28 12:45:11 +00:00
|
|
|
|
2022-12-07 10:20:09 +00:00
|
|
|
describe("Metadata editor", () => {
|
|
|
|
it("can expand field", async () => {
|
|
|
|
// Arrange
|
|
|
|
render(<Component />);
|
|
|
|
const user = userEvent.setup();
|
|
|
|
const isExpandedAttribute = "data-test-expanded";
|
|
|
|
const editor = screen.getAllByTestId("metadata-editor")[0];
|
|
|
|
// Assert
|
|
|
|
expect(editor).toHaveAttribute(isExpandedAttribute, "false");
|
|
|
|
// Act
|
|
|
|
await user.click(getFirstExpandIcon());
|
|
|
|
// Assert
|
|
|
|
expect(editor).toHaveAttribute(isExpandedAttribute, "true");
|
2020-08-28 12:45:11 +00:00
|
|
|
});
|
|
|
|
|
2023-01-12 13:26:28 +00:00
|
|
|
xit("can edit field name", async () => {
|
2022-12-07 10:20:09 +00:00
|
|
|
// Arrange
|
|
|
|
render(<Component />);
|
|
|
|
const user = userEvent.setup();
|
|
|
|
// Act
|
|
|
|
await user.click(getFirstExpandIcon());
|
|
|
|
// Arrange
|
|
|
|
const input = screen.getByRole("textbox", {
|
|
|
|
name: /name:0/i,
|
|
|
|
});
|
|
|
|
// Assert
|
|
|
|
expect(input).toHaveValue(props.data.metadata[0].key);
|
|
|
|
// Act
|
|
|
|
await user.type(input, " with new name");
|
|
|
|
// Assert
|
|
|
|
expect(input).toHaveValue("key with new name");
|
2020-08-28 12:45:11 +00:00
|
|
|
});
|
|
|
|
|
2023-01-12 13:26:28 +00:00
|
|
|
xit("can edit field value", async () => {
|
2022-12-07 10:20:09 +00:00
|
|
|
// Arrange
|
|
|
|
render(<Component />);
|
|
|
|
const user = userEvent.setup();
|
|
|
|
// Act
|
|
|
|
await user.click(getFirstExpandIcon());
|
|
|
|
// Arrange
|
|
|
|
const input = screen.getByRole("textbox", { name: /value:0/i });
|
|
|
|
// Assert
|
|
|
|
expect(input).toHaveValue(props.data.metadata[0].value);
|
|
|
|
// Act
|
|
|
|
await user.type(input, " with new field value");
|
|
|
|
// Assert
|
|
|
|
expect(input).toHaveValue("value with new field value");
|
2020-08-28 12:45:11 +00:00
|
|
|
});
|
|
|
|
|
2022-12-07 10:20:09 +00:00
|
|
|
it("can delete field", async () => {
|
|
|
|
// Arrange
|
|
|
|
render(<Component />);
|
|
|
|
const user = userEvent.setup();
|
|
|
|
// Act
|
|
|
|
await user.click(getFirstExpandIcon());
|
|
|
|
// Assert
|
|
|
|
expect(screen.getAllByTestId("field")).toHaveLength(
|
2022-06-21 09:36:55 +00:00
|
|
|
props.data.metadata.length,
|
2020-08-28 12:45:11 +00:00
|
|
|
);
|
2022-12-07 10:20:09 +00:00
|
|
|
// Act
|
|
|
|
await user.click(screen.getByTestId("delete-field-0"));
|
|
|
|
// Assert
|
|
|
|
expect(screen.getAllByTestId("field")).toHaveLength(
|
2022-06-21 09:36:55 +00:00
|
|
|
props.data.metadata.length - 1,
|
2020-08-28 12:45:11 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-01-12 13:26:28 +00:00
|
|
|
xit("can add field", async () => {
|
2022-12-07 10:20:09 +00:00
|
|
|
// Arrange
|
|
|
|
render(<Component />);
|
|
|
|
const user = userEvent.setup();
|
|
|
|
// Act
|
|
|
|
await user.click(getFirstExpandIcon());
|
|
|
|
// Assert
|
|
|
|
expect(screen.getAllByTestId("field")).toHaveLength(
|
2022-06-21 09:36:55 +00:00
|
|
|
props.data.metadata.length,
|
2020-08-28 12:45:11 +00:00
|
|
|
);
|
2022-12-07 10:20:09 +00:00
|
|
|
// Act
|
|
|
|
await user.click(screen.getAllByTestId("add-field")[0]);
|
|
|
|
// Assert
|
|
|
|
expect(screen.getAllByTestId("field")).toHaveLength(
|
2022-06-21 09:36:55 +00:00
|
|
|
props.data.metadata.length + 1,
|
2020-08-28 12:45:11 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|