|
| 1 | +import { DEFAULT_DATE_VALUES } from "../../../../config"; |
| 2 | +import { fireEvent, mountedComponent, screen, setScreenSizeToMobile, waitFor } from "../../../../test-utils"; |
| 3 | +import { DATE_FIELD } from "../../../form"; |
| 4 | +import { FieldRecord } from "../../records"; |
| 5 | + |
| 6 | +import DateField from "./date-field"; |
| 7 | + |
| 8 | +describe("<DateField />", () => { |
| 9 | + setScreenSizeToMobile(false); |
| 10 | + |
| 11 | + describe("when is date of birth field", () => { |
| 12 | + const props = { |
| 13 | + field: FieldRecord({ |
| 14 | + name: "date_of_birth", |
| 15 | + type: DATE_FIELD, |
| 16 | + display_name_en: "Text test field" |
| 17 | + }), |
| 18 | + formik: { |
| 19 | + values: [] |
| 20 | + }, |
| 21 | + label: "Date of birth", |
| 22 | + mode: { |
| 23 | + isShow: false, |
| 24 | + isEdit: true |
| 25 | + }, |
| 26 | + name: "date_of_birth" |
| 27 | + }; |
| 28 | + |
| 29 | + describe.skip("and the age field is visible", () => { |
| 30 | + const formProps = { initialValues: { date_of_birth: null } }; |
| 31 | + |
| 32 | + const ageProsp = { ...props, formSection: { fields: [{ name: "age", visible: true }] } }; |
| 33 | + |
| 34 | + it("should set the age field", () => { |
| 35 | + mountedComponent(<DateField {...ageProsp} />, {}, [], {}, formProps); |
| 36 | + expect(screen.getByRole("textbox")).toBeInTheDocument(); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + describe.skip("and the age field is not visible", () => { |
| 41 | + const formProps = { initialValues: { date_of_birth: null } }; |
| 42 | + |
| 43 | + it("should not set the age field", () => { |
| 44 | + mountedComponent(<DateField {...props} />, {}, [], {}, formProps); |
| 45 | + expect(screen.getByRole("textbox")).toBeInTheDocument(); |
| 46 | + }); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + describe.skip("when is date field with a default value", () => { |
| 51 | + const props = { |
| 52 | + field: FieldRecord({ |
| 53 | + name: "date_of_interview", |
| 54 | + type: DATE_FIELD, |
| 55 | + display_name_en: "Date of Interview", |
| 56 | + selected_value: DEFAULT_DATE_VALUES.TODAY |
| 57 | + }), |
| 58 | + formik: { |
| 59 | + values: [] |
| 60 | + }, |
| 61 | + label: "Date of Interview", |
| 62 | + name: "date_of_interview" |
| 63 | + }; |
| 64 | + |
| 65 | + const formProps = { initialValues: {} }; |
| 66 | + |
| 67 | + const modeProps = { |
| 68 | + ...props, |
| 69 | + mode: { isNew: true } |
| 70 | + }; |
| 71 | + |
| 72 | + it("sets the date default value as string if the mode is new", () => { |
| 73 | + mountedComponent(<DateField {...modeProps} />, {}, [], {}, formProps); |
| 74 | + expect(screen.getByRole("textbox")).toBeInTheDocument(); |
| 75 | + }); |
| 76 | + |
| 77 | + it("sets the datetime default value as string if the mode is new", () => { |
| 78 | + const newProps = { |
| 79 | + ...props, |
| 80 | + field: FieldRecord({ |
| 81 | + name: "date_of_interview", |
| 82 | + type: DATE_FIELD, |
| 83 | + display_name_en: "Date of Interview", |
| 84 | + selected_value: DEFAULT_DATE_VALUES.TODAY, |
| 85 | + date_include_time: true |
| 86 | + }), |
| 87 | + mode: { isNew: true } |
| 88 | + }; |
| 89 | + |
| 90 | + mountedComponent(<DateField {...newProps} />, {}, [], {}, formProps); |
| 91 | + expect(screen.getByRole("textbox")).toBeInTheDocument(); |
| 92 | + }); |
| 93 | + |
| 94 | + it("should clear the current value if the mode is new and the clear button is clicked", async () => { |
| 95 | + const clearProps = { |
| 96 | + ...props, |
| 97 | + mode: { isNew: true } |
| 98 | + }; |
| 99 | + |
| 100 | + mountedComponent(<DateField {...clearProps} />, {}, [], {}, formProps); |
| 101 | + fireEvent.click(screen.getByRole("textbox")); |
| 102 | + await fireEvent.click(screen.getByText("buttons.clear").closest("button")); |
| 103 | + await waitFor(() => expect(screen.getByRole("textbox").value).toBe("")); |
| 104 | + }); |
| 105 | + |
| 106 | + it("should not set the default value if the mode is not new", () => { |
| 107 | + const notNewProps = { |
| 108 | + ...props, |
| 109 | + mode: { isEdit: true } |
| 110 | + }; |
| 111 | + |
| 112 | + mountedComponent(<DateField {...notNewProps} />, {}, [], {}, formProps); |
| 113 | + expect(screen.getByRole("textbox")).toBeInTheDocument(); |
| 114 | + }); |
| 115 | + }); |
| 116 | +}); |
0 commit comments