|
| 1 | +import "@testing-library/jest-dom"; |
| 2 | +import {act, render, screen} from "@testing-library/react"; |
| 3 | +import userEvent from "@testing-library/user-event"; |
| 4 | +import {Provider} from "react-redux"; |
| 5 | +import {applyMiddleware, createStore} from "redux"; |
| 6 | +import thunk, {ThunkMiddleware} from "redux-thunk"; |
| 7 | + |
| 8 | +import {Setup} from "test/helpers/Setup"; |
| 9 | + |
| 10 | +import {LED, ORGate, Switch} from "digital/models/ioobjects"; |
| 11 | + |
| 12 | +import {OpenHeaderPopup} from "shared/state/Header"; |
| 13 | + |
| 14 | +import "shared/tests/helpers/Extensions"; |
| 15 | +import {PressToggle} from "shared/tests/helpers/PressToggle"; |
| 16 | + |
| 17 | +import {AppState} from "site/digital/state"; |
| 18 | + |
| 19 | +import {AllActions} from "site/digital/state/actions"; |
| 20 | +import {reducers} from "site/digital/state/reducers"; |
| 21 | + |
| 22 | +import {ExprToCircuitPopup} from "site/digital/containers/ExprToCircuitPopup"; |
| 23 | + |
| 24 | + |
| 25 | +// beforeAll and beforeEach can be used to avoid duplicating store/render code, but is not recommended |
| 26 | +// see: https://testing-library.com/docs/user-event/intro |
| 27 | +describe("Main Popup", () => { |
| 28 | + const info = Setup(); |
| 29 | + const store = createStore(reducers, applyMiddleware(thunk as ThunkMiddleware<AppState, AllActions>)); |
| 30 | + const user = userEvent.setup(); |
| 31 | + |
| 32 | + beforeEach(() => { |
| 33 | + render(<Provider store={store}><ExprToCircuitPopup mainInfo={info} /></Provider>); |
| 34 | + act(() => { store.dispatch(OpenHeaderPopup("expr_to_circuit")) }); |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(() => { |
| 38 | + info.designer.reset(); |
| 39 | + }); |
| 40 | + |
| 41 | + test("Popup Created with default states", () => { |
| 42 | + // Check header and button states |
| 43 | + expect(screen.getByText("Digital Expression To Circuit Generator")).toBeVisible(); |
| 44 | + expect(screen.getByText("Cancel")).toBeVisible(); |
| 45 | + expect(screen.getByText("Generate")).toBeVisible(); |
| 46 | + expect(screen.getByText("Generate")).toBeDisabled(); |
| 47 | + |
| 48 | + // Check format options |
| 49 | + expect(/Programming 1/).toBeToggledOn(); |
| 50 | + expect(/Custom/).toBeToggledOff(); |
| 51 | + expect(screen.queryByText(/Custom AND/)).toBeNull(); |
| 52 | + |
| 53 | + // Check toggle switches |
| 54 | + expect(/Place labels for inputs/).toBeToggledOff(); |
| 55 | + expect(/Generate into IC/).toBeToggledOff(); |
| 56 | + expect(screen.queryByText(/Connect Clocks/)).toBeNull(); |
| 57 | + |
| 58 | + // Check dropdowns |
| 59 | + const inputOptions = screen.getByLabelText(/Input Component/).querySelectorAll("option"); |
| 60 | + const switchInputOption = [...inputOptions].find((input) => input.text === "Switch"); |
| 61 | + expect(switchInputOption?.selected).toBeTruthy(); |
| 62 | + const outputOptions = screen.getByLabelText(/Output Component/).querySelectorAll("option"); |
| 63 | + const ledOutputOption = [...outputOptions].find((input) => input.text === "LED"); |
| 64 | + expect(ledOutputOption?.selected).toBeTruthy(); |
| 65 | + |
| 66 | + // Text input is empty |
| 67 | + const input = screen.getByRole<HTMLInputElement>("textbox"); |
| 68 | + expect(input.value).toBe(""); |
| 69 | + }); |
| 70 | + |
| 71 | + test("Cancel Button Cancels", async () => { |
| 72 | + await user.type(screen.getByRole("textbox"), "a | b"); |
| 73 | + |
| 74 | + await user.click(screen.getByText("Cancel")); |
| 75 | + expect(screen.getByText("Cancel")).not.toBeVisible(); |
| 76 | + expect(screen.getByText("Digital Expression To Circuit Generator")).not.toBeVisible(); |
| 77 | + |
| 78 | + // Reopen and requery in case reference changed |
| 79 | + act(() => { store.dispatch(OpenHeaderPopup("expr_to_circuit")) }); |
| 80 | + expect((screen.getByRole<HTMLInputElement>("textbox")).value).toBe(""); |
| 81 | + }); |
| 82 | + |
| 83 | + test("Generate Button", async () => { |
| 84 | + // Enter the expression and generate |
| 85 | + await user.type(screen.getByRole("textbox"), "a | b"); |
| 86 | + expect(screen.getByText("Generate")).toBeEnabled(); |
| 87 | + await user.click(screen.getByText("Generate")); |
| 88 | + expect(screen.getByText("Digital Expression To Circuit Generator")).not.toBeVisible(); |
| 89 | + |
| 90 | + // Check that the components are placed and connected |
| 91 | + const components = info.designer.getObjects(); |
| 92 | + expect(components).toHaveLength(4); |
| 93 | + const inputA = components.find(component => component instanceof Switch |
| 94 | + && component.getName() === "a") as Switch; |
| 95 | + const inputB = components.find(component => component instanceof Switch |
| 96 | + && component.getName() === "b") as Switch; |
| 97 | + const orGate = components.find(component => component instanceof ORGate) as ORGate; |
| 98 | + const led = components.find(component => component instanceof LED) as LED; |
| 99 | + expect(inputA).toBeDefined(); |
| 100 | + expect(inputB).toBeDefined(); |
| 101 | + expect(orGate).toBeDefined(); |
| 102 | + expect(led).toBeDefined(); |
| 103 | + expect(led.isOn()).toBeFalsy(); |
| 104 | + inputA.click(); |
| 105 | + expect(led.isOn()).toBeTruthy(); |
| 106 | + inputA.click(); |
| 107 | + inputB.click(); |
| 108 | + expect(led.isOn()).toBeTruthy(); |
| 109 | + |
| 110 | + // Reopen and requery in case reference changed |
| 111 | + act(() => { store.dispatch(OpenHeaderPopup("expr_to_circuit")) }); |
| 112 | + expect((screen.getByRole<HTMLInputElement>("textbox")).value).toBe(""); |
| 113 | + }); |
| 114 | + |
| 115 | + test("Custom format settings appear", async () => { |
| 116 | + await PressToggle("Custom", user); |
| 117 | + expect("Custom").toBeToggledOn(); |
| 118 | + expect(screen.queryByText(/Custom AND/)).toBeVisible(); |
| 119 | + }); |
| 120 | + |
| 121 | + test("Conditions for options to appear", async () => { |
| 122 | + await user.selectOptions(screen.getByLabelText(/Output Component/), "Oscilloscope"); |
| 123 | + expect(screen.queryByText(/Generate into IC/)).toBeNull(); |
| 124 | + expect(screen.queryByText(/Connect Clocks/)).toBeNull(); |
| 125 | + |
| 126 | + await user.selectOptions(screen.getByLabelText(/Input Component/), "Clock"); |
| 127 | + expect(/Connect Clocks/).toBeToggledOff(); |
| 128 | + |
| 129 | + await user.selectOptions(screen.getByLabelText(/Input Component/), "Switch"); |
| 130 | + expect(screen.queryByText(/Connect Clocks/)).toBeNull(); |
| 131 | + }); |
| 132 | +}); |
0 commit comments