|
| 1 | +import xmlNsLoader from "./xml-namespace-loader"; |
| 2 | +import { convertSlashesInPath } from "./projectHelpers"; |
| 3 | + |
| 4 | +const CODE_FILE = ` |
| 5 | +<Page xmlns="http://www.nativescript.org/tns.xsd"> |
| 6 | + <StackLayout> |
| 7 | + <GridLayout xmlns:chart="nativescript-ui-chart"> |
| 8 | + <chart:RadCartesianChart></chart:RadCartesianChart> |
| 9 | + </GridLayout> |
| 10 | + <GridLayout xmlns:chart="nativescript-ui-chart"> |
| 11 | + <chart:RadCartesianChart></chart:RadCartesianChart> |
| 12 | + </GridLayout> |
| 13 | + </StackLayout> |
| 14 | +</Page> |
| 15 | +`; |
| 16 | + |
| 17 | +interface TestSetup { |
| 18 | + resolveMap: { [path: string]: string }, |
| 19 | + expectedDeps: string[], |
| 20 | + expectedRegs: { name: string, path: string }[], |
| 21 | + ignore?: RegExp, |
| 22 | + assureNoDeps?: boolean, |
| 23 | + expectError?: boolean |
| 24 | +} |
| 25 | + |
| 26 | +function getContext( |
| 27 | + done: DoneFn, |
| 28 | + { resolveMap, expectedDeps, expectedRegs, assureNoDeps, ignore, expectError }: TestSetup) { |
| 29 | + const actualDeps: string[] = []; |
| 30 | + |
| 31 | + const loaderContext = { |
| 32 | + rootContext: "app", |
| 33 | + context: "app/component", |
| 34 | + async: () => (error, source: string) => { |
| 35 | + expectedDeps.forEach(expectedDep => expect(actualDeps).toContain(expectedDep)); |
| 36 | + |
| 37 | + expectedRegs.forEach(({ name, path }) => { |
| 38 | + const regCode = `global.registerModule("${name}", function() { return require("${path}"); });`; |
| 39 | + expect(source).toContain(regCode); |
| 40 | + }) |
| 41 | + |
| 42 | + if (assureNoDeps) { |
| 43 | + expect(actualDeps.length).toBe(0); |
| 44 | + expect(source).not.toContain("global.registerModule"); |
| 45 | + } |
| 46 | + |
| 47 | + if (error && !expectError) { |
| 48 | + done.fail(error) |
| 49 | + } else if (!error && expectError) { |
| 50 | + done.fail("Error expected here") |
| 51 | + } else { |
| 52 | + done(); |
| 53 | + } |
| 54 | + }, |
| 55 | + resolve: (context: string, request: string, callback: (err: Error, result: string) => void) => { |
| 56 | + request = convertSlashesInPath(request); |
| 57 | + if (resolveMap[request]) { |
| 58 | + callback(undefined, resolveMap[request]); |
| 59 | + } else { |
| 60 | + callback(new Error(`Module ${request} not found`), undefined); |
| 61 | + } |
| 62 | + }, |
| 63 | + addDependency: (dep: string) => { |
| 64 | + actualDeps.push(dep); |
| 65 | + }, |
| 66 | + query: { ignore } |
| 67 | + } |
| 68 | + |
| 69 | + return loaderContext; |
| 70 | +} |
| 71 | + |
| 72 | +describe("XmlNamespaceLoader", () => { |
| 73 | + it("with namespace pointing to files", (done) => { |
| 74 | + const resolveMap = { |
| 75 | + "app/nativescript-ui-chart": "app/nativescript-ui-chart.js", |
| 76 | + "app/nativescript-ui-chart.xml": "app/nativescript-ui-chart.xml", |
| 77 | + "app/nativescript-ui-chart.css": "app/nativescript-ui-chart.css", |
| 78 | + }; |
| 79 | + |
| 80 | + const expectedDeps = [ |
| 81 | + "app/nativescript-ui-chart.js", |
| 82 | + "app/nativescript-ui-chart.xml", |
| 83 | + "app/nativescript-ui-chart.css", |
| 84 | + ]; |
| 85 | + |
| 86 | + const expectedRegs = [ |
| 87 | + { name: "nativescript-ui-chart", path: "app/nativescript-ui-chart.js" }, |
| 88 | + { name: "nativescript-ui-chart/RadCartesianChart", path: "app/nativescript-ui-chart.js" }, |
| 89 | + { name: "nativescript-ui-chart/RadCartesianChart.xml", path: "app/nativescript-ui-chart.xml" }, |
| 90 | + { name: "nativescript-ui-chart/RadCartesianChart.css", path: "app/nativescript-ui-chart.css" }, |
| 91 | + ]; |
| 92 | + |
| 93 | + const loaderContext = getContext(done, { resolveMap, expectedDeps, expectedRegs }); |
| 94 | + |
| 95 | + xmlNsLoader.call(loaderContext, CODE_FILE); |
| 96 | + }) |
| 97 | + |
| 98 | + it("with namespace/elementName pointing to files (with package.json)", (done) => { |
| 99 | + const resolveMap = { |
| 100 | + "app/nativescript-ui-chart": "app/nativescript-ui-chart/RadCartesianChart.js", //simulate package.json |
| 101 | + "app/nativescript-ui-chart/RadCartesianChart": "app/nativescript-ui-chart/RadCartesianChart.js", |
| 102 | + "app/nativescript-ui-chart/RadCartesianChart.xml": "app/nativescript-ui-chart/RadCartesianChart.xml", |
| 103 | + "app/nativescript-ui-chart/RadCartesianChart.css": "app/nativescript-ui-chart/RadCartesianChart.css", |
| 104 | + } |
| 105 | + |
| 106 | + const expectedDeps = [ |
| 107 | + "app/nativescript-ui-chart/RadCartesianChart.js", |
| 108 | + "app/nativescript-ui-chart/RadCartesianChart.xml", |
| 109 | + "app/nativescript-ui-chart/RadCartesianChart.css", |
| 110 | + ]; |
| 111 | + |
| 112 | + const expectedRegs = [ |
| 113 | + { name: "nativescript-ui-chart", path: "app/nativescript-ui-chart/RadCartesianChart.js" }, |
| 114 | + { name: "nativescript-ui-chart/RadCartesianChart", path: "app/nativescript-ui-chart/RadCartesianChart.js" }, |
| 115 | + { name: "nativescript-ui-chart/RadCartesianChart.xml", path: "app/nativescript-ui-chart/RadCartesianChart.xml" }, |
| 116 | + { name: "nativescript-ui-chart/RadCartesianChart.css", path: "app/nativescript-ui-chart/RadCartesianChart.css" }, |
| 117 | + ]; |
| 118 | + |
| 119 | + const loaderContext = getContext(done, { resolveMap, expectedDeps, expectedRegs }); |
| 120 | + xmlNsLoader.call(loaderContext, CODE_FILE); |
| 121 | + }) |
| 122 | + |
| 123 | + it("with namespace/elementName pointing to files", (done) => { |
| 124 | + const resolveMap = { |
| 125 | + "app/nativescript-ui-chart/RadCartesianChart": "app/nativescript-ui-chart/RadCartesianChart.js", |
| 126 | + "app/nativescript-ui-chart/RadCartesianChart.xml": "app/nativescript-ui-chart/RadCartesianChart.xml", |
| 127 | + "app/nativescript-ui-chart/RadCartesianChart.css": "app/nativescript-ui-chart/RadCartesianChart.css", |
| 128 | + } |
| 129 | + |
| 130 | + const expectedDeps = [ |
| 131 | + "app/nativescript-ui-chart/RadCartesianChart.js", |
| 132 | + "app/nativescript-ui-chart/RadCartesianChart.xml", |
| 133 | + "app/nativescript-ui-chart/RadCartesianChart.css", |
| 134 | + ]; |
| 135 | + |
| 136 | + const expectedRegs = [ |
| 137 | + { name: "nativescript-ui-chart", path: "app/nativescript-ui-chart/RadCartesianChart.js" }, |
| 138 | + { name: "nativescript-ui-chart/RadCartesianChart", path: "app/nativescript-ui-chart/RadCartesianChart.js" }, |
| 139 | + { name: "nativescript-ui-chart/RadCartesianChart.xml", path: "app/nativescript-ui-chart/RadCartesianChart.xml" }, |
| 140 | + { name: "nativescript-ui-chart/RadCartesianChart.css", path: "app/nativescript-ui-chart/RadCartesianChart.css" }, |
| 141 | + ]; |
| 142 | + |
| 143 | + const loaderContext = getContext(done, { resolveMap, expectedDeps, expectedRegs }); |
| 144 | + xmlNsLoader.call(loaderContext, CODE_FILE); |
| 145 | + }) |
| 146 | + |
| 147 | + it("with namespace/elementName pointing to files - only XML and CSS", (done) => { |
| 148 | + const resolveMap = { |
| 149 | + "app/nativescript-ui-chart/RadCartesianChart.xml": "app/nativescript-ui-chart/RadCartesianChart.xml", |
| 150 | + "app/nativescript-ui-chart/RadCartesianChart.css": "app/nativescript-ui-chart/RadCartesianChart.css", |
| 151 | + } |
| 152 | + |
| 153 | + const expectedDeps = [ |
| 154 | + "app/nativescript-ui-chart/RadCartesianChart.xml", |
| 155 | + "app/nativescript-ui-chart/RadCartesianChart.css", |
| 156 | + ]; |
| 157 | + |
| 158 | + const expectedRegs = [ |
| 159 | + { name: "nativescript-ui-chart/RadCartesianChart.xml", path: "app/nativescript-ui-chart/RadCartesianChart.xml" }, |
| 160 | + { name: "nativescript-ui-chart/RadCartesianChart.css", path: "app/nativescript-ui-chart/RadCartesianChart.css" }, |
| 161 | + ]; |
| 162 | + |
| 163 | + const loaderContext = getContext(done, { resolveMap, expectedDeps, expectedRegs }); |
| 164 | + xmlNsLoader.call(loaderContext, CODE_FILE); |
| 165 | + }) |
| 166 | + |
| 167 | + it("with plugin path", (done) => { |
| 168 | + const resolveMap = { |
| 169 | + "nativescript-ui-chart": "node_module/nativescript-ui-chart/ui-chart.js", |
| 170 | + } |
| 171 | + |
| 172 | + const expectedDeps = [ |
| 173 | + ]; |
| 174 | + |
| 175 | + const expectedRegs = [ |
| 176 | + { name: "nativescript-ui-chart", path: "nativescript-ui-chart" }, |
| 177 | + { name: "nativescript-ui-chart/RadCartesianChart", path: "nativescript-ui-chart" }, |
| 178 | + ]; |
| 179 | + |
| 180 | + const loaderContext = getContext(done, { resolveMap, expectedDeps, expectedRegs }); |
| 181 | + xmlNsLoader.call(loaderContext, CODE_FILE); |
| 182 | + }) |
| 183 | + |
| 184 | + it("with ignored namespace should not add deps or register calls", (done) => { |
| 185 | + const resolveMap = { |
| 186 | + "app/nativescript-ui-chart": "app/nativescript-ui-chart.js", |
| 187 | + "app/nativescript-ui-chart.xml": "app/nativescript-ui-chart.xml", |
| 188 | + "app/nativescript-ui-chart.css": "app/nativescript-ui-chart.css", |
| 189 | + }; |
| 190 | + const expectedDeps = []; |
| 191 | + const expectedRegs = []; |
| 192 | + |
| 193 | + const loaderContext = getContext(done, { resolveMap, expectedDeps, expectedRegs, ignore: /nativescript\-ui\-chart/, assureNoDeps: true }); |
| 194 | + |
| 195 | + xmlNsLoader.call(loaderContext, CODE_FILE); |
| 196 | + }) |
| 197 | + |
| 198 | + it("with XML declaration and Doctype does not fail", (done) => { |
| 199 | + const resolveMap = {}; |
| 200 | + const expectedDeps = []; |
| 201 | + const expectedRegs = []; |
| 202 | + |
| 203 | + const testXml = ` |
| 204 | + <?xml version="1.0" encoding="UTF-8" standalone="no" ?> |
| 205 | + <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 206 | + <!-- comment.xml --> |
| 207 | + <Page xmlns="http://www.nativescript.org/tns.xsd"></Page>`; |
| 208 | + |
| 209 | + const loaderContext = getContext(done, { resolveMap, expectedDeps, expectedRegs, assureNoDeps: true }); |
| 210 | + |
| 211 | + xmlNsLoader.call(loaderContext, testXml); |
| 212 | + }) |
| 213 | + it("with invalid XML fails", (done) => { |
| 214 | + const resolveMap = {}; |
| 215 | + const expectedDeps = []; |
| 216 | + const expectedRegs = []; |
| 217 | + |
| 218 | + const testXml = `<Page xmlns="http://www.nativescript.org/tns.xsd"></PageOpsWrongTagHere>`; |
| 219 | + |
| 220 | + const loaderContext = getContext(done, { resolveMap, expectedDeps, expectedRegs, expectError: true }); |
| 221 | + |
| 222 | + xmlNsLoader.call(loaderContext, testXml); |
| 223 | + }) |
| 224 | +}); |
0 commit comments