|
| 1 | +import bundleConfigLoader from "./bundle-config-loader"; |
| 2 | + |
| 3 | +const defaultLoaderOptions = { |
| 4 | + angular: false, |
| 5 | + loadCss: true, |
| 6 | + unitTesting: false, |
| 7 | +}; |
| 8 | + |
| 9 | +const defaultTestFiles = { |
| 10 | + "./app-root.xml": true, |
| 11 | + "./app-root.land.xml": true, |
| 12 | + "./app.ts": true, |
| 13 | + "./app.css": true, |
| 14 | + "./app.android.scss": true, |
| 15 | + "./app.ios.scss": true, |
| 16 | + "./app.land.css": true, |
| 17 | + "./components/my-component.css": true, |
| 18 | + "./components/my-component.land.ts": true, |
| 19 | + "./components/my-component.ts": true, |
| 20 | + "./components/my-component.land.xml": true, |
| 21 | + "./components/my-component.xml": true, |
| 22 | + "./components/my-component.land.css": true, |
| 23 | + "./main/main-page.land.css": true, |
| 24 | + "./main/main-page.css": true, |
| 25 | + "./main/main-page.ts": true, |
| 26 | + "./main/main-page.land.xml": true, |
| 27 | + "./main/main-page.xml": true, |
| 28 | + "./main/main-page.land.ts": true, |
| 29 | + "./main/main-page-vm.ts": true, |
| 30 | + |
| 31 | + "./app_component.scss": true, |
| 32 | + "./App_Resources123/some-file.xml": true, |
| 33 | + "./MyApp_Resources/some-file.xml": true, |
| 34 | + "./App_Resources_Nobody_Names_Folders_Like_That_Roska/some-file.xml": true, |
| 35 | + |
| 36 | + "./package.json": false, // do not include package.json files |
| 37 | + "./app.d.ts": false, // do not include ts definitions |
| 38 | + "./_app-common.scss": false, // do not include scss partial files |
| 39 | + "./_app-variables.scss": false, // do not include scss partial files |
| 40 | + "./App_Resources/Android/src/main/res/values/colors.xml": false, // do not include App_Resources |
| 41 | + "./App_Resources/Android/src/main/AndroidManifest.xml": false, // do not include App_Resources |
| 42 | +}; |
| 43 | + |
| 44 | +const loaderOptionsWithIgnore = { |
| 45 | + angular: false, |
| 46 | + loadCss: true, |
| 47 | + unitTesting: false, |
| 48 | + ignoredFiles: ["./application", "./activity"] |
| 49 | +}; |
| 50 | + |
| 51 | +const ignoredTestFiles = { |
| 52 | + "./application.ts": false, |
| 53 | + "./activity.ts": false, |
| 54 | +} |
| 55 | + |
| 56 | +function getRequireContextRegex(source: string): RegExp { |
| 57 | + const requireContextStr = `require.context("~/", true, `; |
| 58 | + |
| 59 | + expect(source).toContain(requireContextStr); |
| 60 | + |
| 61 | + const start = source.indexOf(requireContextStr) + requireContextStr.length; |
| 62 | + const end = source.indexOf(");\n", start); |
| 63 | + const regexStr = source.substring(start, end); |
| 64 | + const regex: RegExp = eval(regexStr); |
| 65 | + |
| 66 | + expect(regex instanceof RegExp).toBeTruthy(); |
| 67 | + return regex; |
| 68 | +} |
| 69 | + |
| 70 | +function assertTestFilesAreMatched(testFiles: { [key: string]: boolean }, regex: RegExp) { |
| 71 | + for (let fileName in testFiles) { |
| 72 | + if (defaultTestFiles[fileName]) { |
| 73 | + expect(fileName).toMatch(regex); |
| 74 | + } |
| 75 | + else { |
| 76 | + expect(fileName).not.toMatch(regex); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +describe("BundleConfigLoader should create require.context", () => { |
| 82 | + it("default case", (done) => { |
| 83 | + |
| 84 | + const loaderContext = { |
| 85 | + callback: (error, source: string, map) => { |
| 86 | + const regex = getRequireContextRegex(source); |
| 87 | + |
| 88 | + assertTestFilesAreMatched(defaultTestFiles, regex); |
| 89 | + |
| 90 | + done(); |
| 91 | + }, |
| 92 | + query: defaultLoaderOptions |
| 93 | + } |
| 94 | + |
| 95 | + bundleConfigLoader.call(loaderContext, " ___CODE___"); |
| 96 | + }) |
| 97 | + |
| 98 | + it("with ignored files", (done) => { |
| 99 | + |
| 100 | + const loaderContext = { |
| 101 | + callback: (error, source: string, map) => { |
| 102 | + const regex = getRequireContextRegex(source); |
| 103 | + const testFiles = { ...defaultTestFiles, ...ignoredTestFiles }; |
| 104 | + |
| 105 | + assertTestFilesAreMatched(testFiles, regex); |
| 106 | + |
| 107 | + done(); |
| 108 | + }, |
| 109 | + query: loaderOptionsWithIgnore |
| 110 | + } |
| 111 | + |
| 112 | + bundleConfigLoader.call(loaderContext, " ___CODE___"); |
| 113 | + }) |
| 114 | +}); |
0 commit comments