/*! * Copyright 2019 acrazing . All rights reserved. * @since 2019-11-02 16:55:34 */ import * as ts from 'typescript'; import { createLoadableComponentsTransformer, } from './createLoadableComponentsTransformer'; function transform(source: string, options: any = {}): string { const sourceFile = ts.createSourceFile( 'test.tsx', source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX ); const transformerFactory = createLoadableComponentsTransformer(undefined, options); const result = ts.transform(sourceFile, [transformerFactory]); const printer = ts.createPrinter(); const transformedSource = printer.printFile(result.transformed[0]); result.dispose(); return transformedSource; } describe('createLoadableComponentsTransformer', () => { it('should transform basic loadable call', () => { const input = ` const EmptyAdminPage = loadable(() => loadableRetry(() => import("./EmptyAdminPage")), { resolveComponent: ({ EmptyAdminPage }) => EmptyAdminPage, }); `; const result = transform(input); // Check that the transformation happened expect(result).toContain('requireAsync:'); expect(result).toContain('chunkName()'); expect(result).toContain('requireSync(props)'); expect(result).toContain('resolve()'); expect(result).toContain('isReady(props)'); // Check that the original arrow function was replaced with object literal expect(result).not.toContain('() => loadableRetry(() => import("./EmptyAdminPage"))'); console.log('Transformed code:'); console.log(result); }); it('should handle loadable with lib property', () => { const input = ` const Component = loadable.lib(() => import("./library")); `; const result = transform(input); // Check that lib calls are also transformed expect(result).toContain('requireAsync:'); expect(result).toContain('chunkName()'); console.log('Lib transformed code:'); console.log(result); }); it('should not transform non-loadable calls', () => { const input = ` const Component = someOtherFunction(() => import("./test")); `; const result = transform(input); // Should remain unchanged expect(result).toContain('someOtherFunction(() => import("./test"))'); expect(result).not.toContain('requireAsync:'); }); it('should handle complex import with webpack comments', () => { const input = ` const DynamicComponent = loadable(() => loadableRetry(() => import(/* webpackChunkName: "dynamic-component" */ "./DynamicComponent")) ); `; const result = transform(input); // Check transformation expect(result).toContain('requireAsync:'); expect(result).toContain('chunkName()'); // Should preserve or handle webpack comments console.log('Webpack comment transformed code:'); console.log(result); }); });