|
1 | 1 | const { |
2 | | - evaluate, |
3 | 2 | getMixinsForModule, |
4 | 3 | getShimsForModule, |
| 4 | + generateBundleRequireConfig, |
| 5 | + getRequireConfigFromDir, |
5 | 6 | } = require('../requireConfig'); |
6 | | -const { readFileSync } = require('fs'); |
| 7 | +const { join } = require('path'); |
7 | 8 |
|
8 | | -const rawConfig = readFileSync( |
9 | | - require.resolve('./__fixtures__/luma-requirejs-config'), |
10 | | -); |
| 9 | +const requireConfigDir = join(__dirname, '__fixtures__/luma-requirejs-config'); |
11 | 10 |
|
12 | | -test('evaluate does not throw on simple window property access', () => { |
13 | | - const config = ` |
14 | | - (function(require) { |
15 | | - var foo = window.foo; |
16 | | - })(require); |
17 | | - `; |
18 | | - expect(() => evaluate(config)).not.toThrow(); |
19 | | -}); |
20 | | - |
21 | | -test('evaluate captures all "deps" values', () => { |
22 | | - const config = evaluate(rawConfig); |
23 | | - expect(config.deps).toEqual([ |
24 | | - 'jquery/jquery.mobile.custom', |
25 | | - 'mage/common', |
26 | | - 'mage/dataPost', |
27 | | - 'mage/bootstrap', |
28 | | - 'jquery/jquery-migrate', |
29 | | - 'jquery/jquery.cookie', |
30 | | - 'mage/translate-inline', |
31 | | - 'Magento_Theme/js/responsive', |
32 | | - 'Magento_Theme/js/theme', |
33 | | - ]); |
34 | | -}); |
35 | | - |
36 | | -test('evaluate merges map* values from various configs', () => { |
37 | | - const config = evaluate(rawConfig); |
38 | | - const mapStar = config.map['*']; |
39 | | - expect(mapStar.rowBuilder).toEqual('Magento_Theme/js/row-builder'); |
40 | | - expect(mapStar.ko).toEqual('knockoutjs/knockout'); |
41 | | -}); |
42 | | - |
43 | | -test('evaluate merges paths values from various configs', () => { |
44 | | - const config = evaluate(rawConfig); |
45 | | - const { paths } = config; |
46 | | - expect(paths['jquery/ui']).toEqual('jquery/jquery-ui'); |
47 | | - expect(paths['jquery/validate']).toEqual('jquery/jquery.validate'); |
48 | | -}); |
49 | | - |
50 | | -test('evaluate merges mixins config values from various configs', () => { |
51 | | - const config = evaluate(rawConfig); |
52 | | - const { mixins } = config.config; |
53 | | - |
54 | | - expect(mixins['Magento_Theme/js/view/breadcrumbs']).toEqual({ |
55 | | - 'Magento_Theme/js/view/add-home-breadcrumb': true, |
56 | | - 'Magento_Catalog/js/product/breadcrumbs': true, |
57 | | - }); |
58 | | -}); |
59 | | - |
60 | | -test('getMixinsForModule returns empty list when no mixins found', () => { |
61 | | - const mixins = getMixinsForModule( |
62 | | - 'Magento_Theme/js/foo', |
63 | | - evaluate(rawConfig), |
64 | | - ); |
| 11 | +test('getMixinsForModule returns empty list when no mixins found', async () => { |
| 12 | + const { requireConfig } = await getRequireConfigFromDir(requireConfigDir); |
| 13 | + const mixins = getMixinsForModule('Magento_Theme/js/foo', requireConfig); |
65 | 14 | expect(mixins).toEqual([]); |
66 | 15 | }); |
67 | 16 |
|
68 | | -test('getMixinsForModule finds single matching mixin', () => { |
69 | | - const mixins = getMixinsForModule('jquery/jquery-ui', evaluate(rawConfig)); |
| 17 | +test('getMixinsForModule finds single matching mixin', async () => { |
| 18 | + const { requireConfig } = await getRequireConfigFromDir(requireConfigDir); |
| 19 | + const mixins = getMixinsForModule('jquery/jquery-ui', requireConfig); |
70 | 20 | expect(mixins).toEqual(['jquery/patches/jquery-ui']); |
71 | 21 | }); |
72 | 22 |
|
73 | | -test('getMixinsForModule finds multiple matching mixins', () => { |
| 23 | +test('getMixinsForModule finds multiple matching mixins', async () => { |
| 24 | + const { requireConfig } = await getRequireConfigFromDir(requireConfigDir); |
74 | 25 | const mixins = getMixinsForModule( |
75 | 26 | 'Magento_Theme/js/view/breadcrumbs', |
76 | | - evaluate(rawConfig), |
| 27 | + requireConfig, |
77 | 28 | ); |
78 | 29 | expect(mixins).toEqual([ |
79 | 30 | 'Magento_Theme/js/view/add-home-breadcrumb', |
@@ -132,3 +83,115 @@ test('getShimsForModule handles exports key and deps', () => { |
132 | 83 | deps: ['a', 'b', 'c'], |
133 | 84 | }); |
134 | 85 | }); |
| 86 | + |
| 87 | +test('getRequireConfigFromDir throws descriptive error when config cannot be found', async () => { |
| 88 | + expect.assertions(1); |
| 89 | + try { |
| 90 | + await getRequireConfigFromDir('/does/not/exist'); |
| 91 | + } catch (err) { |
| 92 | + expect(err.message).toContain('Failed reading RequireJS config'); |
| 93 | + } |
| 94 | +}); |
| 95 | + |
| 96 | +test('getRequireConfigFromDir throws descriptive error when config evaluation fails', async () => { |
| 97 | + expect.hasAssertions(); |
| 98 | + const configDir = join(__dirname, '__fixtures__/invalid-requirejs-config'); |
| 99 | + |
| 100 | + try { |
| 101 | + await getRequireConfigFromDir(configDir); |
| 102 | + } catch (err) { |
| 103 | + expect(err.message).toContain('Failed evaluating RequireJS config'); |
| 104 | + expect(err.message).toContain('window.fooPath'); |
| 105 | + } |
| 106 | +}); |
| 107 | + |
| 108 | +test('getRequireConfigFromDir does not throw on simple window property access', async () => { |
| 109 | + const configDir = join( |
| 110 | + __dirname, |
| 111 | + '__fixtures__/require-config-window-access', |
| 112 | + ); |
| 113 | + await getRequireConfigFromDir(configDir); |
| 114 | +}); |
| 115 | + |
| 116 | +test('evaluate captures all "deps" values', async () => { |
| 117 | + const { requireConfig } = await getRequireConfigFromDir(requireConfigDir); |
| 118 | + expect(requireConfig.deps).toEqual([ |
| 119 | + 'jquery/jquery.mobile.custom', |
| 120 | + 'mage/common', |
| 121 | + 'mage/dataPost', |
| 122 | + 'mage/bootstrap', |
| 123 | + 'jquery/jquery-migrate', |
| 124 | + 'jquery/jquery.cookie', |
| 125 | + 'mage/translate-inline', |
| 126 | + 'Magento_Theme/js/responsive', |
| 127 | + 'Magento_Theme/js/theme', |
| 128 | + ]); |
| 129 | +}); |
| 130 | + |
| 131 | +test('evaluate merges map* values from various configs', async () => { |
| 132 | + const { requireConfig } = await getRequireConfigFromDir(requireConfigDir); |
| 133 | + const mapStar = requireConfig.map['*']; |
| 134 | + expect(mapStar.rowBuilder).toEqual('Magento_Theme/js/row-builder'); |
| 135 | + expect(mapStar.ko).toEqual('knockoutjs/knockout'); |
| 136 | +}); |
| 137 | + |
| 138 | +test('evaluate merges paths values from various configs', async () => { |
| 139 | + const { requireConfig } = await getRequireConfigFromDir(requireConfigDir); |
| 140 | + const { paths } = requireConfig; |
| 141 | + expect(paths['jquery/ui']).toEqual('jquery/jquery-ui'); |
| 142 | + expect(paths['jquery/validate']).toEqual('jquery/jquery.validate'); |
| 143 | +}); |
| 144 | + |
| 145 | +test('evaluate merges mixins config values from various configs', async () => { |
| 146 | + const { requireConfig } = await getRequireConfigFromDir(requireConfigDir); |
| 147 | + const { mixins } = requireConfig.config; |
| 148 | + |
| 149 | + expect(mixins['Magento_Theme/js/view/breadcrumbs']).toEqual({ |
| 150 | + 'Magento_Theme/js/view/add-home-breadcrumb': true, |
| 151 | + 'Magento_Catalog/js/product/breadcrumbs': true, |
| 152 | + }); |
| 153 | +}); |
| 154 | + |
| 155 | +test('generateBundleRequireConfig appends bundles data to require config', () => { |
| 156 | + const originalConfig = ` |
| 157 | + (function() { |
| 158 | + require.config({ |
| 159 | + paths: { |
| 160 | + 'foo': 'something/foo' |
| 161 | + } |
| 162 | + }) |
| 163 | + })(); |
| 164 | + `; |
| 165 | + const newConfig = generateBundleRequireConfig( |
| 166 | + originalConfig, |
| 167 | + 'core-bundle', |
| 168 | + ['foo', 'bar', 'bizz'], |
| 169 | + ); |
| 170 | + |
| 171 | + expect(newConfig).toMatchInlineSnapshot(` |
| 172 | + "(function() { |
| 173 | + // Injected by @magento/baler. This config |
| 174 | + // tells RequireJS which modules are in the |
| 175 | + // bundle, to prevent require from trying to |
| 176 | + // load bundled modules from the network |
| 177 | + require.config({ |
| 178 | + bundles: { |
| 179 | + 'balerbundles/core-bundle': [ |
| 180 | + \\"foo\\", |
| 181 | + \\"bar\\", |
| 182 | + \\"bizz\\" |
| 183 | + ] |
| 184 | + } |
| 185 | + }); |
| 186 | + })(); |
| 187 | +
|
| 188 | + (function() { |
| 189 | + require.config({ |
| 190 | + paths: { |
| 191 | + 'foo': 'something/foo' |
| 192 | + } |
| 193 | + }) |
| 194 | + })(); |
| 195 | + " |
| 196 | + `); |
| 197 | +}); |
0 commit comments