import * as React from "react";
import { render, screen, fireEvent, cleanup } from "@testing-library/react";
import reactToCSS from "react-style-object-to-css";
import LiveFormField from "./LiveFormField";
import { FormFieldTypes } from "../types";
import { IconTypes } from "@sc/plugins/webcomponents/v2/Icon";
import { IListItem } from "../FormFields/types";
const testId = "FormBuilder-LiveFormField";
const props = {};
const textFieldTestId = "FormBuilder-FormFields-TextField-LIVE";
const dateFieldTestId = "FormBuilder-FormFields-DateField-LIVE";
const dropDownTestId = "FormBuilder-FormFields-DropdownField-LIVE";
const textAreaTestId = "FormBuilder-FormFields-TextAreaField-LIVE";
const checkboxesTestId = "FormBuilder-FormFields-Checkboxes";
const multipleChoiceTestId = "FormBuilder-FormFields-MultipleChoice";
const contentFieldTestId = "FormBuilder-FormFields-ContentField";
const fileUploadTestId = "FormBuilder-FormFields-FileUpload";
const linearScaleTestId = "FormBuilder-FormFields-LinearScaleField";
const elem = {
Text: "input",
Password: "input",
Checkbox: "input",
Checkboxes: "input",
Radio: "input",
Textarea: "textarea",
Select: "select",
Date: "input",
Content: "div",
Number: "input",
Currency: "input",
DropDown: "select",
MultipleChoice: "input",
Typography: "div",
FileUpload: "input",
LinearScale: "input",
};
describe(" Rendering Tests", () => {
afterEach(cleanup);
it(`Should render in the dom`, () => {
render();
expect(screen.queryByTestId(testId)).toBeTruthy();
});
it(`Should show the when the type prop is TEXT`, () => {
render();
expect(screen.queryByTestId(textFieldTestId)).toBeTruthy();
});
it(`Should show the when the type prop is DATE`, () => {
render();
expect(screen.queryByTestId(dateFieldTestId)).toBeTruthy();
});
it(`Should show the input when the type prop is NUMBER`, () => {
render();
expect(screen.queryByTestId(textFieldTestId)).toBeTruthy();
});
it(`Should show the when the type prop is CURRENCY`, () => {
render();
expect(screen.queryByTestId(textFieldTestId)).toBeTruthy();
});
it(`Should show menu when the type prop is DROPDOWN`, () => {
render();
expect(screen.queryByTestId(dropDownTestId)).toBeTruthy();
});
it(`Should show a checkbox & label ( the when type prop is CHECKBOX`, () => {
render();
expect(screen.queryByTestId(checkboxesTestId)).toBeTruthy();
});
it(`Should show radio input fields ( the when type prop is MULTIPLECHOICE`, () => {
render();
expect(screen.queryByTestId(multipleChoiceTestId)).toBeTruthy();
});
it(`Should show a the when type prop is TYPOGRAPHY`, () => {
render();
expect(screen.queryByTestId(contentFieldTestId)).toBeTruthy();
});
it(`Should show a component when the type prop is FILEUPLOAD`, () => {
render();
expect(screen.queryByTestId(fileUploadTestId)).toBeTruthy();
});
it(`Should show a component when the type prop is TEXTAREA`, () => {
render();
expect(screen.queryByTestId(textAreaTestId)).toBeTruthy();
});
it(`Should show a component when the type prop is LINEARSCALE`, () => {
render();
expect(screen.queryByTestId(linearScaleTestId)).toBeTruthy();
});
it(`Should use the "placeholder" property as the placeholder for the form field`, () => {
const placeholder = "Testing 123";
const { rerender } = render(
);
// TextField
rerender(
);
expect(
screen
.queryByTestId(textFieldTestId)
.querySelector("input")
.getAttribute("placeholder")
).toBe(placeholder);
// DropdownField
rerender(
);
expect(
screen
.queryByTestId(dropDownTestId)
.querySelector("select")
.getAttribute("placeholder")
).toBe(placeholder);
// TextAreaField
rerender(
);
expect(
screen
.queryByTestId(textAreaTestId)
.querySelector("textarea")
.getAttribute("placeholder")
).toBe(placeholder);
});
// it(`Should use the "styles" property to determine the style to use for the default form field`, () => {
// const { rerender } = render();
// const styles = {
// labelStyle: { padding: 5 },
// inputStyle: { padding: 5 },
// containerStyle: { padding: 5 },
// validationStyle: { padding: 5 },
// iconStyle: { padding: 5 },
// };
// for (let type in FormFieldTypes) {
// rerender(
//
// );
// // expect(container.firstChild).toMatchSnapshot();
// expect(screen.getByTestId(testId).getAttribute("style")).toContain(
// reactToCSS(styles.containerStyle).trim()
// );
// }
// });
it(`Should use the "icon" property to determine the icon to use in this form field`, () => {
const iconTestId = "WC-ICON-LIVE";
const { rerender } = render();
// TextField
rerender(
);
// debug();
expect(screen.queryByTestId(iconTestId)).toBeTruthy();
// DateField
rerender(
);
expect(screen.queryByTestId(iconTestId)).toBeTruthy();
// DropdownField
rerender(
);
expect(screen.queryByTestId(iconTestId)).toBeTruthy();
// TextAreaField
rerender(
);
expect(screen.queryByTestId(iconTestId)).toBeTruthy();
});
it(`Should use the "label" property to determine the label to show next to the form field`, () => {
const { rerender } = render();
const label = "Label Content";
// TextField
rerender(
);
expect(screen.queryByText(label)).toBeTruthy();
// Checkboxes
rerender(
);
expect(screen.queryByText(label)).toBeTruthy();
// DateField
rerender(
);
expect(screen.queryByText(label)).toBeTruthy();
// DropdownField
rerender(
);
expect(screen.queryByText(label)).toBeTruthy();
// FileUpload
rerender(
);
expect(screen.queryByText(label)).toBeTruthy();
// LinearScaleField
rerender(
);
expect(screen.queryByText(label)).toBeTruthy();
// MultipleChoiceField
rerender(
);
expect(screen.queryByText(label)).toBeTruthy();
// TextAreaField
rerender(
);
expect(screen.queryByText(label)).toBeTruthy();
});
it(`Should use the "defaultValue" property to determine the value to use for the form field`, () => {
const { rerender } = render();
const defaultValue = "This is the default value";
const items: IListItem[] = [
{ id: "1", label: "Testing", value: "123" },
{ id: "2", label: defaultValue, value: defaultValue },
{ id: "3", label: "Testing", value: "123" },
];
// TextField
rerender(
);
expect(
screen
.getByTestId(textFieldTestId)
.querySelector(elem[FormFieldTypes.TEXT])
.getAttribute("value")
).toBe(defaultValue);
// DateField
rerender(
);
expect(
screen
.getByTestId(dateFieldTestId)
.querySelector(elem[FormFieldTypes.DATE])
.getAttribute("value")
).toBe(defaultValue);
// DropdownField
rerender(
);
expect(screen.queryByText(defaultValue)).toBeTruthy();
// TextAreaField
rerender(
);
expect(screen.queryByText(defaultValue)).toBeTruthy();
});
it(`Should use the "description" property to determine the description to show next to the form field`, () => {
const { rerender } = render();
const description = "This is the description test";
// TextField
rerender(
);
expect(screen.queryByText(description)).toBeTruthy();
// Checkboxes
rerender(
);
expect(screen.queryByText(description)).toBeTruthy();
// DateField
rerender(
);
expect(screen.queryByText(description)).toBeTruthy();
// DropdownField
rerender(
);
expect(screen.queryByText(description)).toBeTruthy();
// FileUpload
rerender(
);
expect(screen.queryByText(description)).toBeTruthy();
// LinearScaleField
rerender(
);
expect(screen.queryByText(description)).toBeTruthy();
// MultipleChoice
rerender(
);
expect(screen.queryByText(description)).toBeTruthy();
// TextAreaField
rerender(
);
expect(screen.queryByText(description)).toBeTruthy();
});
it(`Should use the "(items)" property to determine the list of items to include in a dropdown menu field`, () => {
const defaultValue = "This is the default value";
const items: IListItem[] = [
{ id: "1", label: "Testing", value: "123" },
{ id: "2", label: "Testing", value: defaultValue },
{ id: "3", label: "Testing", value: "123" },
];
render(
);
expect(
screen.getByTestId(dropDownTestId).querySelectorAll("option").length
).toEqual(items.length);
});
test.todo(`Multiple Choice, Radio, LinearScaleField default values`);
});
describe(" Event Tests", () => {
afterEach(cleanup);
test.todo(
`Should trigger the "onChange" property found in the data object when the form field changes`
);
test.todo(
`Should onChange should send back the original payload it received, but mutated)`
);
test.todo(
`Should trigger the "onClick" property found in the data object when a click event occurs with the form field`
);
test.todo(
`Should trigger the "onKeyUp" property found in the data object when a keyUp event occurs with the form field`
);
});