|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +const assert = require("node:assert/strict"); |
| 18 | +const {readFileSync} = require("node:fs"); |
| 19 | +const Module = require("node:module"); |
| 20 | +const path = require("node:path"); |
| 21 | +const {test} = require("node:test"); |
| 22 | +const ts = require("typescript"); |
| 23 | +const React = require("react"); |
| 24 | + |
| 25 | +test("Custom only preserves categories and filters both regular and preferred cards", () => { |
| 26 | + const states = []; |
| 27 | + const effects = []; |
| 28 | + let cursor = 0; |
| 29 | + let mounted = false; |
| 30 | + const react = { |
| 31 | + ...React, |
| 32 | + useState: initial => { |
| 33 | + const index = cursor++; |
| 34 | + if (!(index in states)) states[index] = initial; |
| 35 | + return [states[index], value => { |
| 36 | + states[index] = typeof value === "function" ? value(states[index]) : value; |
| 37 | + }]; |
| 38 | + }, |
| 39 | + useEffect: effect => { |
| 40 | + if (!mounted) effects.push(effect); |
| 41 | + }, |
| 42 | + }; |
| 43 | + const selector = { |
| 44 | + showSelector: true, |
| 45 | + showSteps: true, |
| 46 | + parentId: "route", |
| 47 | + parentDsl: "FromDefinition", |
| 48 | + selectedToggles: ["eip", "components", "kamelets"], |
| 49 | + setShowSelector: () => {}, |
| 50 | + setShowProperties: () => {}, |
| 51 | + setSelectedDsl: () => {}, |
| 52 | + addSelectedToggle: toggle => selector.selectedToggles.push(toggle), |
| 53 | + deleteSelectedToggle: toggle => { |
| 54 | + selector.selectedToggles = selector.selectedToggles.filter(value => value !== toggle); |
| 55 | + }, |
| 56 | + }; |
| 57 | + const elements = [ |
| 58 | + {name: "log", dsl: "LogDefinition", navigation: "eip"}, |
| 59 | + {name: "direct", uri: "direct", navigation: "component"}, |
| 60 | + {name: "custom", navigation: "kamelet"}, |
| 61 | + {name: "other", navigation: "kamelet"}, |
| 62 | + ]; |
| 63 | + let customNames = ["custom"]; |
| 64 | + const mocks = { |
| 65 | + react, |
| 66 | + "@patternfly/react-core": new Proxy({}, {get: (_target, name) => name}), |
| 67 | + "./DslSelector.css": {}, |
| 68 | + "../utils/CamelUi": {CamelUi: { |
| 69 | + getSelectorModelsForParentFiltered: (_parent, navigation) => elements.filter(element => element.navigation === navigation), |
| 70 | + checkFilter: (element, filter) => element.name.includes(filter), |
| 71 | + }}, |
| 72 | + "../utils/DslMetaModel": {}, |
| 73 | + "../DesignerStore": {useSelectorStore: select => select(selector)}, |
| 74 | + "zustand/shallow": {}, |
| 75 | + "@karavan-core/api/ComponentApi": {ComponentApi: {getBlockedComponentNames: () => []}}, |
| 76 | + "@karavan-core/api/KameletApi": {KameletApi: {getCustomKameletNames: () => customNames}}, |
| 77 | + "@patternfly/react-icons/dist/esm/icons/times-icon": "TimesIcon", |
| 78 | + "@patternfly/react-icons/dist/esm/icons/exclamation-circle-icon": "ExclamationCircleIcon", |
| 79 | + "./DslPreferences": { |
| 80 | + getPreferredElements: () => ["LogDefinition", "direct", "custom", "other"], |
| 81 | + }, |
| 82 | + "./DslFastCard": {DslFastCard: "DslFastCard"}, |
| 83 | + "./DslCard": {DslCard: "DslCard"}, |
| 84 | + "usehooks-ts": {useDebounceValue: initial => react.useState(initial)}, |
| 85 | + uuid: {}, |
| 86 | + "@features/project/designer/utils/ValidatorUtils": {}, |
| 87 | + "@stores/ProjectStore": {useFilesStore: select => select({files: []})}, |
| 88 | + }; |
| 89 | + // Exercise the selector TypeScript without a browser or VS Code host. |
| 90 | + const fullPath = path.join(__dirname, "..", "webview", "karavan", "features", "project", "designer", "selector", "DslSelector.tsx"); |
| 91 | + const loaded = new Module(fullPath, module); |
| 92 | + loaded.filename = fullPath; |
| 93 | + loaded.paths = module.paths; |
| 94 | + loaded.require = request => { |
| 95 | + if (request in mocks) return mocks[request]; |
| 96 | + if (request.startsWith("@karavan-core/")) return {}; |
| 97 | + return require(request); |
| 98 | + }; |
| 99 | + const code = ts.transpileModule(readFileSync(fullPath, "utf8"), { |
| 100 | + compilerOptions: { |
| 101 | + module: ts.ModuleKind.CommonJS, |
| 102 | + target: ts.ScriptTarget.ES2022, |
| 103 | + jsx: ts.JsxEmit.React, |
| 104 | + esModuleInterop: true, |
| 105 | + }, |
| 106 | + }).outputText; |
| 107 | + loaded._compile(code, fullPath); |
| 108 | + const render = () => { |
| 109 | + cursor = 0; |
| 110 | + return loaded.exports.DslSelector({onDslSelect: () => {}}); |
| 111 | + }; |
| 112 | + const nodes = tree => { |
| 113 | + if (Array.isArray(tree)) return tree.flatMap(nodes); |
| 114 | + if (!React.isValidElement(tree)) return []; |
| 115 | + return [tree, ...nodes(tree.props.children)]; |
| 116 | + }; |
| 117 | + const cardNames = (tree, type) => nodes(tree).filter(node => node.type === type).map(node => node.props.dsl.name); |
| 118 | + const toggleCustom = (tree, checked) => nodes(tree).find(node => node.type === "Switch").props.onChange({}, checked); |
| 119 | + const assertCards = expected => { |
| 120 | + const tree = render(); |
| 121 | + assert.deepEqual(cardNames(tree, "DslCard"), expected); |
| 122 | + assert.deepEqual(cardNames(tree, "DslFastCard"), expected); |
| 123 | + }; |
| 124 | + render(); |
| 125 | + mounted = true; |
| 126 | + effects.forEach(effect => effect()); |
| 127 | + for (const categories of [["eip", "components", "kamelets"], ["components", "kamelets"], ["kamelets"]]) { |
| 128 | + selector.selectedToggles = [...categories]; |
| 129 | + const tree = render(); |
| 130 | + const before = cardNames(tree, "DslCard"); |
| 131 | + assertCards(before); |
| 132 | + toggleCustom(tree, true); |
| 133 | + assert.deepEqual(selector.selectedToggles, categories); |
| 134 | + assertCards(["custom"]); |
| 135 | + toggleCustom(render(), false); |
| 136 | + assert.deepEqual(selector.selectedToggles, categories); |
| 137 | + assertCards(before); |
| 138 | + } |
| 139 | + |
| 140 | + customNames = []; |
| 141 | + const before = cardNames(render(), "DslCard"); |
| 142 | + toggleCustom(render(), true); |
| 143 | + assertCards([]); |
| 144 | + toggleCustom(render(), false); |
| 145 | + assertCards(before); |
| 146 | + |
| 147 | + customNames = ["custom"]; |
| 148 | + selector.selectedToggles = ["eip", "components", "kamelets"]; |
| 149 | + const search = value => nodes(render()).find(node => node.type === "TextInput").props.onChange({}, value); |
| 150 | + search("other"); |
| 151 | + assertCards(["other"]); |
| 152 | + toggleCustom(render(), true); |
| 153 | + assertCards([]); |
| 154 | + search("custom"); |
| 155 | + assertCards(["custom"]); |
| 156 | + toggleCustom(render(), false); |
| 157 | + assertCards(["custom"]); |
| 158 | + search(""); |
| 159 | + |
| 160 | + for (const categories of [["eip"], ["components"], []]) { |
| 161 | + selector.selectedToggles = [...categories]; |
| 162 | + const expected = elements.filter(element => categories.includes(element.navigation === "component" ? "components" : element.navigation)).map(element => element.name); |
| 163 | + assertCards(expected); |
| 164 | + } |
| 165 | +}); |
0 commit comments