File size: 3,555 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const babel = require( '@babel/core' );

describe( 'babel-plugin-transform-wpcalypso-async', () => {
	function transform( code, options = {} ) {
		return babel.transformSync( code, {
			configFile: false,
			parserOpts: { plugins: [ 'jsx' ] },
			plugins: [ [ require( '..' ), options ] ],
		} ).code;
	}

	describe( '<AsyncLoad require />', () => {
		test( 'should not transform other components', () => {
			const code = transform( '<div require="foo" />' );

			expect( code ).toBe( '<div require="foo" />;' );
		} );

		test( 'should do nothing to other prop strings', () => {
			const code = transform( '<AsyncLoad other="foo" />' );

			expect( code ).toBe( '<AsyncLoad other="foo" />;' );
		} );

		test( 'should not transform non-strings', () => {
			const code = transform( '<AsyncLoad require={ true } />' );

			expect( code ).toBe( '<AsyncLoad require={true} />;' );
		} );

		describe( 'async', () => {
			test( 'should replace a require string prop with hoisting', () => {
				const code = transform( 'export default () => <AsyncLoad require="foo" />;' );

				expect( code ).toBe(
					'var _ref = () => import(/*webpackChunkName: "async-load-foo"*/"foo");\n' +
						'export default () => <AsyncLoad require={_ref} />;'
				);
			} );
		} );

		describe( 'ignore', () => {
			test( 'should replace a require string prop with hoisting', () => {
				const code = transform( 'export default () => <AsyncLoad require="foo" />;', {
					ignore: true,
				} );

				expect( code ).toBe(
					'var _ref = () => {\n' +
						'  throw new Error("ignoring load of: async-load-foo");\n' +
						'};\n' +
						'export default () => <AsyncLoad require={_ref} />;'
				);
			} );
		} );
	} );

	describe( 'asyncRequire', () => {
		test( 'should not transform other call expressions', () => {
			const code = transform( 'requireAsync( "foo" );' );

			expect( code ).toBe( 'requireAsync("foo");' );
		} );

		test( 'should not transform if there is no string argument', () => {
			const code = transform( 'asyncRequire();' );

			expect( code ).toBe( 'asyncRequire();' );
		} );

		describe( 'async', () => {
			test( 'should transform asyncRequire to an import with nice name', () => {
				const code = transform( 'asyncRequire( "foo/bar" );' );

				expect( code ).toBe( 'import(/*webpackChunkName: "async-load-foo-bar"*/"foo/bar");' );
			} );
		} );

		describe( 'ignore', () => {
			test( 'should transform asyncRequire into error-throwing function', () => {
				const code = transform( 'asyncRequire( "foo" );', { ignore: true } );

				expect( code ).toBe( 'Promise.reject(new Error("ignoring load of: async-load-foo"));' );
			} );
		} );
	} );

	test( 'AsyncLoad with further import transformation', () => {
		// Babel plugin that transforms import() into patchedImport()
		const patchImportPlugin = ( { types } ) => ( {
			visitor: {
				Import( path ) {
					path.replaceWith( types.identifier( 'patchedImport' ) );
				},
			},
		} );

		const code = 'export default () => <AsyncLoad require="foo" />;';

		// Use both the AsyncLoad and patchedImport transform
		const transformResult = babel.transformSync( code, {
			configFile: false,
			parserOpts: { plugins: [ 'jsx', 'dynamicImport' ] },
			plugins: [ [ require( '..' ) ], patchImportPlugin ],
		} );

		// Check that the transformed code has been further transformed with patchedImport
		expect( transformResult.code ).toBe(
			'var _ref = () => patchedImport(/*webpackChunkName: "async-load-foo"*/"foo");\n' +
				'export default () => <AsyncLoad require={_ref} />;'
		);
	} );
} );