|
| 1 | +/** |
| 2 | + * @typedef {import("../../src/tool/DevToolPanel").DevToolPanelProps} DevToolPanelProps |
| 3 | + * @typedef {import("../../src/tool/DevTool.mjs").DevToolValue} DevToolValue |
| 4 | + * @typedef {import("../../src/theme/Theme.mjs").ThemeType} ThemeType |
| 5 | + */ |
| 6 | +import React from "react"; |
| 7 | +import TestRenderer from "react-test-renderer"; |
| 8 | +import assert from "node:assert/strict"; |
| 9 | +import { assertComponents, mockComponent } from "react-assert"; |
| 10 | +import mockFunction from "mock-fn"; |
| 11 | +import withThemeContext from "../theme/withThemeContext.mjs"; |
| 12 | +import DefaultTheme from "../../src/theme/DefaultTheme.mjs"; |
| 13 | +import XTerm256Theme from "../../src/theme/XTerm256Theme.mjs"; |
| 14 | +import * as UI from "../../src/UI.mjs"; |
| 15 | +import LogPanel from "../../src/tool/LogPanel.mjs"; |
| 16 | +import InputController from "../../src/tool/InputController.mjs"; |
| 17 | +import ColorPanel from "../../src/tool/ColorPanel.mjs"; |
| 18 | +import DevTool from "../../src/tool/DevTool.mjs"; |
| 19 | +import DevToolPanel from "../../src/tool/DevToolPanel.mjs"; |
| 20 | + |
| 21 | +const h = React.createElement; |
| 22 | + |
| 23 | +const { describe, it } = await (async () => { |
| 24 | + // @ts-ignore |
| 25 | + return process.isBun // @ts-ignore |
| 26 | + ? Promise.resolve({ describe: (_, fn) => fn(), it: test }) |
| 27 | + : import("node:test"); |
| 28 | +})(); |
| 29 | + |
| 30 | +DevToolPanel.logPanelComp = mockComponent(LogPanel); |
| 31 | +DevToolPanel.inputController = mockComponent(InputController); |
| 32 | +DevToolPanel.colorPanelComp = mockComponent(ColorPanel); |
| 33 | + |
| 34 | +const { logPanelComp, inputController, colorPanelComp } = DevToolPanel; |
| 35 | + |
| 36 | +describe("DevToolPanel.test.mjs", () => { |
| 37 | + it("should call onActivate when click on tab", () => { |
| 38 | + //given |
| 39 | + const onActivate = mockFunction((devTool) => { |
| 40 | + assert.deepEqual(devTool, DevTool.Logs); |
| 41 | + }); |
| 42 | + const props = getDevToolPanelProps(DevTool.Colors, "test logs", onActivate); |
| 43 | + const comp = TestRenderer.create( |
| 44 | + withThemeContext(h(DevToolPanel, props)) |
| 45 | + ).root; |
| 46 | + const [tab1] = comp.findAllByType("text"); |
| 47 | + |
| 48 | + //when |
| 49 | + tab1.props.onClick(); |
| 50 | + |
| 51 | + //then |
| 52 | + assert.deepEqual(onActivate.times, 1); |
| 53 | + }); |
| 54 | + |
| 55 | + it("should render Logs component", () => { |
| 56 | + //given |
| 57 | + const props = getDevToolPanelProps(DevTool.Logs, "test logs"); |
| 58 | + |
| 59 | + //when |
| 60 | + const renderer = TestRenderer.create( |
| 61 | + withThemeContext(h(DevToolPanel, props)) |
| 62 | + ); |
| 63 | + |
| 64 | + //then |
| 65 | + assertDevToolPanel( |
| 66 | + renderer, |
| 67 | + " Logs ", |
| 68 | + h(logPanelComp, { content: props.logContent }) |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + it("should render Inputs component", () => { |
| 73 | + //given |
| 74 | + const props = getDevToolPanelProps(DevTool.Inputs, "test logs"); |
| 75 | + |
| 76 | + //when |
| 77 | + const renderer = TestRenderer.create( |
| 78 | + withThemeContext(h(DevToolPanel, props)) |
| 79 | + ); |
| 80 | + |
| 81 | + //then |
| 82 | + assertDevToolPanel(renderer, " Inputs ", h(inputController)); |
| 83 | + }); |
| 84 | + |
| 85 | + it("should render Colors component", () => { |
| 86 | + //given |
| 87 | + const currTheme = XTerm256Theme; |
| 88 | + const props = getDevToolPanelProps(DevTool.Colors, "test logs"); |
| 89 | + |
| 90 | + //when |
| 91 | + const renderer = TestRenderer.create( |
| 92 | + withThemeContext(h(DevToolPanel, props), currTheme) |
| 93 | + ); |
| 94 | + |
| 95 | + //then |
| 96 | + assertDevToolPanel(renderer, " Colors ", h(colorPanelComp), currTheme); |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +/** |
| 101 | + * @param {DevToolValue} devTool |
| 102 | + * @param {string} logContent |
| 103 | + * @param {(devTool: DevToolValue) => void} onActivate |
| 104 | + * @returns {DevToolPanelProps} |
| 105 | + */ |
| 106 | +function getDevToolPanelProps(devTool, logContent, onActivate = (_) => {}) { |
| 107 | + return { |
| 108 | + devTool, |
| 109 | + logContent, |
| 110 | + onActivate, |
| 111 | + }; |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * @typedef {{label: string, pos: number}} ExpectedTab |
| 116 | + */ |
| 117 | + |
| 118 | +/** |
| 119 | + * @param {string} label |
| 120 | + * @param {number} pos |
| 121 | + * @returns {ExpectedTab} |
| 122 | + */ |
| 123 | +function getExpectedTab(label, pos) { |
| 124 | + return { |
| 125 | + label, |
| 126 | + pos, |
| 127 | + }; |
| 128 | +} |
| 129 | + |
| 130 | +/** |
| 131 | + * @type {Array<ExpectedTab>} |
| 132 | + */ |
| 133 | +const expectedTabs = [ |
| 134 | + getExpectedTab(" Logs ", 0), |
| 135 | + getExpectedTab(" Inputs ", 6), |
| 136 | + getExpectedTab(" Colors ", 14), |
| 137 | +]; |
| 138 | + |
| 139 | +/** |
| 140 | + * @param {TestRenderer.ReactTestRenderer} renderer |
| 141 | + * @param {string} activeTab |
| 142 | + * @param {React.ReactElement} expectedComp |
| 143 | + * @param {ThemeType} currTheme |
| 144 | + */ |
| 145 | +function assertDevToolPanel( |
| 146 | + renderer, |
| 147 | + activeTab, |
| 148 | + expectedComp, |
| 149 | + currTheme = DefaultTheme |
| 150 | +) { |
| 151 | + assert.deepEqual(DevToolPanel.displayName, "DevToolPanel"); |
| 152 | + |
| 153 | + const theme = currTheme.popup.menu; |
| 154 | + const width = expectedTabs |
| 155 | + .map(({ label }) => label.length) |
| 156 | + .reduce((_1, _2) => _1 + _2); |
| 157 | + |
| 158 | + assertComponents( |
| 159 | + renderer.root.children, |
| 160 | + h( |
| 161 | + "box", |
| 162 | + { |
| 163 | + width: "100%", |
| 164 | + height: 1, |
| 165 | + style: theme, |
| 166 | + }, |
| 167 | + h( |
| 168 | + "box", |
| 169 | + { |
| 170 | + width, |
| 171 | + height: 1, |
| 172 | + left: "center", |
| 173 | + }, |
| 174 | + ...expectedTabs.map(({ label, pos }) => { |
| 175 | + const style = label === activeTab ? theme.focus || theme : theme; |
| 176 | + const content = UI.renderText2(style, label); |
| 177 | + |
| 178 | + return h("text", { |
| 179 | + autoFocus: false, |
| 180 | + clickable: true, |
| 181 | + tags: true, |
| 182 | + mouse: true, |
| 183 | + left: pos, |
| 184 | + content, |
| 185 | + }); |
| 186 | + }) |
| 187 | + ) |
| 188 | + ), |
| 189 | + h("box", { top: 1 }, expectedComp) |
| 190 | + ); |
| 191 | +} |
0 commit comments