|
| 1 | +/** |
| 2 | + * @typedef {import('../../src/popup/ListPopup').ListPopupProps} ListPopupProps |
| 3 | + */ |
| 4 | +import React from "react"; |
| 5 | +import TestRenderer from "react-test-renderer"; |
| 6 | +import assert from "node:assert/strict"; |
| 7 | +import { assertComponent, assertComponents, mockComponent } from "react-assert"; |
| 8 | +import mockFunction from "mock-fn"; |
| 9 | +import DefaultTheme from "../../src/theme/DefaultTheme.mjs"; |
| 10 | +import withThemeContext from "../theme/withThemeContext.mjs"; |
| 11 | +import Popup from "../../src/popup/Popup.mjs"; |
| 12 | +import ModalContent from "../../src/popup/ModalContent.mjs"; |
| 13 | +import WithSize from "../../src/WithSize.mjs"; |
| 14 | +import ListBox from "../../src/ListBox.mjs"; |
| 15 | +import ListPopup from "../../src/popup/ListPopup.mjs"; |
| 16 | + |
| 17 | +const h = React.createElement; |
| 18 | + |
| 19 | +const { describe, it } = await (async () => { |
| 20 | + // @ts-ignore |
| 21 | + const module = process.isBun ? "bun:test" : "node:test"; |
| 22 | + // @ts-ignore |
| 23 | + return process.isBun // @ts-ignore |
| 24 | + ? Promise.resolve({ describe: (_, fn) => fn(), it: test }) |
| 25 | + : import(module); |
| 26 | +})(); |
| 27 | + |
| 28 | +ListPopup.popupComp = mockComponent(Popup); |
| 29 | +ListPopup.modalContentComp = mockComponent(ModalContent); |
| 30 | +ListPopup.withSizeComp = mockComponent(WithSize); |
| 31 | +ListPopup.listBoxComp = mockComponent(ListBox); |
| 32 | + |
| 33 | +const { popupComp, modalContentComp, withSizeComp, listBoxComp } = ListPopup; |
| 34 | + |
| 35 | +describe("ListPopup.test.mjs", () => { |
| 36 | + it("should not call onAction if empty items when onAction", () => { |
| 37 | + //given |
| 38 | + const onAction = mockFunction(); |
| 39 | + const props = { ...getListPopupProps(), items: [], onAction }; |
| 40 | + const comp = TestRenderer.create( |
| 41 | + withThemeContext(h(ListPopup, props)) |
| 42 | + ).root; |
| 43 | + const renderContent = comp.findByType(withSizeComp).props.render(60, 20); |
| 44 | + const resultContent = TestRenderer.create(renderContent).root; |
| 45 | + |
| 46 | + //when |
| 47 | + resultContent.findByType(listBoxComp).props.onAction(1); |
| 48 | + |
| 49 | + //then |
| 50 | + assert.deepEqual(onAction.times, 0); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should call onAction when onAction", () => { |
| 54 | + //given |
| 55 | + const onAction = mockFunction((index) => { |
| 56 | + //then |
| 57 | + assert.deepEqual(index, 1); |
| 58 | + }); |
| 59 | + const props = { ...getListPopupProps(), onAction }; |
| 60 | + const comp = TestRenderer.create( |
| 61 | + withThemeContext(h(ListPopup, props)) |
| 62 | + ).root; |
| 63 | + const renderContent = comp.findByType(withSizeComp).props.render(60, 20); |
| 64 | + const resultContent = TestRenderer.create(renderContent).root; |
| 65 | + |
| 66 | + //when |
| 67 | + resultContent.findByType(listBoxComp).props.onAction(1); |
| 68 | + |
| 69 | + //then |
| 70 | + assert.deepEqual(onAction.times, 1); |
| 71 | + }); |
| 72 | + |
| 73 | + it("should render popup with empty list", () => { |
| 74 | + //given |
| 75 | + const props = { ...getListPopupProps(), items: [] }; |
| 76 | + |
| 77 | + //when |
| 78 | + const result = TestRenderer.create( |
| 79 | + withThemeContext(h(ListPopup, props)) |
| 80 | + ).root; |
| 81 | + |
| 82 | + //then |
| 83 | + assertListPopup(result, props, [], [60, 20], [56, 14]); |
| 84 | + }); |
| 85 | + |
| 86 | + it("should render popup with min size", () => { |
| 87 | + //given |
| 88 | + const props = { ...getListPopupProps(), items: new Array(20).fill("item") }; |
| 89 | + |
| 90 | + //when |
| 91 | + const result = TestRenderer.create( |
| 92 | + withThemeContext(h(ListPopup, props)) |
| 93 | + ).root; |
| 94 | + |
| 95 | + //then |
| 96 | + assertListPopup( |
| 97 | + result, |
| 98 | + props, |
| 99 | + new Array(20).fill(" item "), |
| 100 | + [55, 13], |
| 101 | + [56, 14] |
| 102 | + ); |
| 103 | + }); |
| 104 | + |
| 105 | + it("should render popup with max height", () => { |
| 106 | + //given |
| 107 | + const items = new Array(20).fill("item"); |
| 108 | + const props = { ...getListPopupProps(), items, selected: items.length - 1 }; |
| 109 | + |
| 110 | + //when |
| 111 | + const result = TestRenderer.create( |
| 112 | + withThemeContext(h(ListPopup, props)) |
| 113 | + ).root; |
| 114 | + |
| 115 | + //then |
| 116 | + assertListPopup( |
| 117 | + result, |
| 118 | + props, |
| 119 | + new Array(20).fill(" item "), |
| 120 | + [60, 20], |
| 121 | + [56, 16] |
| 122 | + ); |
| 123 | + }); |
| 124 | + |
| 125 | + it("should render popup with max width", () => { |
| 126 | + //given |
| 127 | + const props = { |
| 128 | + ...getListPopupProps(), |
| 129 | + items: new Array(20).fill( |
| 130 | + "iteeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeem" |
| 131 | + ), |
| 132 | + }; |
| 133 | + |
| 134 | + //when |
| 135 | + const result = TestRenderer.create( |
| 136 | + withThemeContext(h(ListPopup, props)) |
| 137 | + ).root; |
| 138 | + |
| 139 | + //then |
| 140 | + assertListPopup( |
| 141 | + result, |
| 142 | + props, |
| 143 | + new Array(20).fill( |
| 144 | + " ite...eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeem " |
| 145 | + ), |
| 146 | + [60, 20], |
| 147 | + [60, 16] |
| 148 | + ); |
| 149 | + }); |
| 150 | +}); |
| 151 | + |
| 152 | +/** |
| 153 | + * @returns {ListPopupProps} |
| 154 | + */ |
| 155 | +function getListPopupProps() { |
| 156 | + return { |
| 157 | + title: "Test Title", |
| 158 | + items: ["item 1", "item 2"], |
| 159 | + onAction: () => {}, |
| 160 | + onClose: () => {}, |
| 161 | + footer: "test footer", |
| 162 | + }; |
| 163 | +} |
| 164 | + |
| 165 | +/** |
| 166 | + * @param {TestRenderer.ReactTestInstance} result |
| 167 | + * @param {ListPopupProps} props |
| 168 | + * @param {string[]} items |
| 169 | + * @param {number[]} screenSize |
| 170 | + * @param {number[]} expectedSize |
| 171 | + */ |
| 172 | +function assertListPopup(result, props, items, screenSize, expectedSize) { |
| 173 | + assert.deepEqual(ListPopup.displayName, "ListPopup"); |
| 174 | + |
| 175 | + const theme = DefaultTheme.popup.menu; |
| 176 | + const [width, height] = screenSize; |
| 177 | + const [expectedWidth, expectedHeight] = expectedSize; |
| 178 | + const contentWidth = expectedWidth - 2 * (ListPopup.paddingHorizontal + 1); |
| 179 | + const contentHeight = expectedHeight - 2 * (ListPopup.paddingVertical + 1); |
| 180 | + |
| 181 | + const withSizeProps = result.findByType(withSizeComp).props; |
| 182 | + const content = TestRenderer.create(withSizeProps.render(width, height)).root; |
| 183 | + assertComponent( |
| 184 | + content, |
| 185 | + h( |
| 186 | + modalContentComp, |
| 187 | + { |
| 188 | + title: props.title, |
| 189 | + width: expectedWidth, |
| 190 | + height: expectedHeight, |
| 191 | + style: theme, |
| 192 | + padding: ListPopup.padding, |
| 193 | + left: undefined, |
| 194 | + footer: props.footer, |
| 195 | + }, |
| 196 | + h(listBoxComp, { |
| 197 | + left: 1, |
| 198 | + top: 1, |
| 199 | + width: contentWidth, |
| 200 | + height: contentHeight, |
| 201 | + selected: props.selected ?? 0, |
| 202 | + items: items, |
| 203 | + style: theme, |
| 204 | + onAction: () => {}, |
| 205 | + onSelect: props.onSelect, |
| 206 | + }) |
| 207 | + ) |
| 208 | + ); |
| 209 | + |
| 210 | + assertComponents( |
| 211 | + result.children, |
| 212 | + h( |
| 213 | + popupComp, |
| 214 | + { |
| 215 | + onClose: () => {}, |
| 216 | + focusable: undefined, |
| 217 | + onKeypress: undefined, |
| 218 | + }, |
| 219 | + h(withSizeComp, { |
| 220 | + render: mockFunction(), |
| 221 | + }) |
| 222 | + ) |
| 223 | + ); |
| 224 | +} |
0 commit comments