|
| 1 | +import { path } from '@stencil/core/compiler'; |
| 2 | +import { mockConfig, mockStencilSystem, mockBuildCtx, mockCompilerCtx, mockModule } from '@stencil/core/testing'; |
| 3 | +import type * as d from '../../../declarations'; |
| 4 | +import * as outputCustomElementsMod from '../dist-custom-elements'; |
| 5 | +import { stubComponentCompilerMeta } from '../../types/tests/ComponentCompilerMeta.stub'; |
| 6 | +import { generateCustomElementsTypes } from '../dist-custom-elements/custom-elements-types'; |
| 7 | +import { DIST_CUSTOM_ELEMENTS } from '../output-utils'; |
| 8 | +import { join, relative } from 'path'; |
| 9 | + |
| 10 | +const setup = () => { |
| 11 | + const sys = mockStencilSystem(); |
| 12 | + const config: d.Config = mockConfig(sys); |
| 13 | + const compilerCtx = mockCompilerCtx(config); |
| 14 | + const buildCtx = mockBuildCtx(config, compilerCtx); |
| 15 | + const root = config.rootDir; |
| 16 | + config.configPath = '/testing-path'; |
| 17 | + config.srcDir = '/src'; |
| 18 | + config.buildAppCore = true; |
| 19 | + config.rootDir = path.join(root, 'User', 'testing', '/'); |
| 20 | + config.namespace = 'TestApp'; |
| 21 | + config.buildEs5 = true; |
| 22 | + config.globalScript = path.join(root, 'User', 'testing', 'src', 'global.ts'); |
| 23 | + config.outputTargets = [{ type: DIST_CUSTOM_ELEMENTS, dir: 'my-best-dir' }]; |
| 24 | + |
| 25 | + const bundleCustomElementsSpy = jest.spyOn(outputCustomElementsMod, 'bundleCustomElements'); |
| 26 | + |
| 27 | + compilerCtx.moduleMap.set('test', mockModule()); |
| 28 | + |
| 29 | + return { config, compilerCtx, buildCtx, bundleCustomElementsSpy }; |
| 30 | +}; |
| 31 | + |
| 32 | +describe('Custom Elements Typedef generation', () => { |
| 33 | + it('should generate an index.d.ts file corresponding to the index.js file', async () => { |
| 34 | + const componentOne = stubComponentCompilerMeta(); |
| 35 | + const componentTwo = stubComponentCompilerMeta({ |
| 36 | + componentClassName: 'MyBestComponent', |
| 37 | + tagName: 'my-best-component', |
| 38 | + }); |
| 39 | + const { config, compilerCtx, buildCtx } = setup(); |
| 40 | + buildCtx.components = [componentOne, componentTwo]; |
| 41 | + |
| 42 | + const writeFileSpy = jest.spyOn(compilerCtx.fs, 'writeFile'); |
| 43 | + |
| 44 | + await generateCustomElementsTypes(config, compilerCtx, buildCtx, 'types_dir'); |
| 45 | + |
| 46 | + const componentsTypeDirectoryPath = relative('my-best-dir', join('types_dir', 'components')); |
| 47 | + |
| 48 | + const expectedTypedefOutput = [ |
| 49 | + '/* TestApp custom elements */', |
| 50 | + `export { StubCmp as StubCmp } from '${join(componentsTypeDirectoryPath, 'stub-cmp', 'stub-cmp')}';`, |
| 51 | + `export { MyBestComponent as MyBestComponent } from '${join( |
| 52 | + componentsTypeDirectoryPath, |
| 53 | + 'my-best-component', |
| 54 | + 'my-best-component' |
| 55 | + )}';`, |
| 56 | + '', |
| 57 | + '/**', |
| 58 | + ' * Used to manually set the base path where assets can be found.', |
| 59 | + ' * If the script is used as "module", it\'s recommended to use "import.meta.url",', |
| 60 | + ' * such as "setAssetPath(import.meta.url)". Other options include', |
| 61 | + ' * "setAssetPath(document.currentScript.src)", or using a bundler\'s replace plugin to', |
| 62 | + ' * dynamically set the path at build time, such as "setAssetPath(process.env.ASSET_PATH)".', |
| 63 | + ' * But do note that this configuration depends on how your script is bundled, or lack of', |
| 64 | + ' * bundling, and where your assets can be loaded from. Additionally custom bundling', |
| 65 | + ' * will have to ensure the static assets are copied to its build directory.', |
| 66 | + ' */', |
| 67 | + 'export declare const setAssetPath: (path: string) => void;', |
| 68 | + '', |
| 69 | + 'export interface SetPlatformOptions {', |
| 70 | + ' raf?: (c: FrameRequestCallback) => number;', |
| 71 | + ' ael?: (el: EventTarget, eventName: string, listener: EventListenerOrEventListenerObject, options: boolean | AddEventListenerOptions) => void;', |
| 72 | + ' rel?: (el: EventTarget, eventName: string, listener: EventListenerOrEventListenerObject, options: boolean | AddEventListenerOptions) => void;', |
| 73 | + '}', |
| 74 | + 'export declare const setPlatformOptions: (opts: SetPlatformOptions) => void;', |
| 75 | + "export * from '../types_dir/components';", |
| 76 | + '', |
| 77 | + ].join('\n'); |
| 78 | + |
| 79 | + expect(compilerCtx.fs.writeFile).toBeCalledWith(join('my-best-dir', 'index.d.ts'), expectedTypedefOutput, { |
| 80 | + outputTargetType: DIST_CUSTOM_ELEMENTS, |
| 81 | + }); |
| 82 | + |
| 83 | + writeFileSpy.mockRestore(); |
| 84 | + }); |
| 85 | +}); |
0 commit comments