|
| 1 | +import React from "react"; |
| 2 | +import { render, screen } from "@testing-library/react"; |
| 3 | + |
| 4 | +import InputSelector from "./InputSelector"; |
| 5 | +import { AppContext } from "../contexts/GlobalContext"; |
| 6 | + |
| 7 | +jest.mock("antd", () => { |
| 8 | + const Form = ({ children }) => <form>{children}</form>; |
| 9 | + Form.Item = ({ label, children, help }) => ( |
| 10 | + <div> |
| 11 | + {label} |
| 12 | + {children} |
| 13 | + {help ? <div>{help}</div> : null} |
| 14 | + </div> |
| 15 | + ); |
| 16 | + |
| 17 | + return { |
| 18 | + Form, |
| 19 | + Space: ({ children }) => <div>{children}</div>, |
| 20 | + }; |
| 21 | +}); |
| 22 | + |
| 23 | +jest.mock("./UnifiedFileInput", () => (props) => ( |
| 24 | + <input |
| 25 | + aria-label={props.placeholder} |
| 26 | + data-selection-type={props.selectionType} |
| 27 | + readOnly |
| 28 | + value={typeof props.value === "string" ? props.value : ""} |
| 29 | + /> |
| 30 | +)); |
| 31 | + |
| 32 | +jest.mock("./InlineHelpChat", () => () => null); |
| 33 | + |
| 34 | +beforeAll(() => { |
| 35 | + Object.defineProperty(window, "matchMedia", { |
| 36 | + writable: true, |
| 37 | + value: jest.fn().mockImplementation((query) => ({ |
| 38 | + matches: false, |
| 39 | + media: query, |
| 40 | + onchange: null, |
| 41 | + addListener: jest.fn(), |
| 42 | + removeListener: jest.fn(), |
| 43 | + addEventListener: jest.fn(), |
| 44 | + removeEventListener: jest.fn(), |
| 45 | + dispatchEvent: jest.fn(), |
| 46 | + })), |
| 47 | + }); |
| 48 | +}); |
| 49 | + |
| 50 | +function renderWithContext(type) { |
| 51 | + const contextValue = { |
| 52 | + trainingState: { |
| 53 | + inputImage: "", |
| 54 | + inputLabel: "", |
| 55 | + outputPath: "", |
| 56 | + logPath: "", |
| 57 | + checkpointPath: "", |
| 58 | + setInputImage: jest.fn(), |
| 59 | + setInputLabel: jest.fn(), |
| 60 | + setOutputPath: jest.fn(), |
| 61 | + setLogPath: jest.fn(), |
| 62 | + setCheckpointPath: jest.fn(), |
| 63 | + }, |
| 64 | + inferenceState: { |
| 65 | + inputImage: "", |
| 66 | + inputLabel: "/tmp/stale-label.tif", |
| 67 | + outputPath: "", |
| 68 | + logPath: "", |
| 69 | + checkpointPath: "", |
| 70 | + setInputImage: jest.fn(), |
| 71 | + setInputLabel: jest.fn(), |
| 72 | + setOutputPath: jest.fn(), |
| 73 | + setLogPath: jest.fn(), |
| 74 | + setCheckpointPath: jest.fn(), |
| 75 | + }, |
| 76 | + }; |
| 77 | + |
| 78 | + return render( |
| 79 | + <AppContext.Provider value={contextValue}> |
| 80 | + <InputSelector type={type} /> |
| 81 | + </AppContext.Provider>, |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +describe("InputSelector", () => { |
| 86 | + it("shows an input label field for training", () => { |
| 87 | + renderWithContext("training"); |
| 88 | + |
| 89 | + expect(screen.getByText("Input Label")).toBeTruthy(); |
| 90 | + }); |
| 91 | + |
| 92 | + it("does not show an input label field for inference", () => { |
| 93 | + renderWithContext("inference"); |
| 94 | + |
| 95 | + expect(screen.queryByText("Input Label")).toBeNull(); |
| 96 | + expect(screen.queryByText("Input Label (Optional)")).toBeNull(); |
| 97 | + }); |
| 98 | +}); |
0 commit comments