|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Salesforce, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * SPDX-License-Identifier: BSD-3-Clause |
| 5 | + * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | +import * as fse from 'fs-extra' |
| 8 | +import * as path from 'path' |
| 9 | +import {buildBabelExtensibilityArgs} from './babel/utils' |
| 10 | +import {ApplicationExtensionEntryTuple} from '../types' |
| 11 | + |
| 12 | +jest.mock('fs-extra', () => ({ |
| 13 | + ...jest.requireActual('fs-extra'), |
| 14 | + realpathSync: jest.fn() |
| 15 | +})) |
| 16 | + |
| 17 | +const EXTENSIONS: ApplicationExtensionEntryTuple[] = [ |
| 18 | + ['@salesforce/extension-this', {enabled: true}], |
| 19 | + ['@salesforce/extension-that', {enabled: true}] |
| 20 | +] |
| 21 | +const CONFIG = { |
| 22 | + app: {extensions: EXTENSIONS} |
| 23 | +} |
| 24 | + |
| 25 | +const normalizePath = (posixPath: string) => posixPath.split(path.posix.sep).join(path.sep) |
| 26 | + |
| 27 | +describe('buildBabelExtensibilityArgs', () => { |
| 28 | + const realpathSyncMock = jest.spyOn(fse, 'realpathSync') as jest.Mock |
| 29 | + |
| 30 | + beforeEach(() => { |
| 31 | + realpathSyncMock.mockImplementation((filePath: string) => { |
| 32 | + if (filePath.includes('build-remote-server.js')) { |
| 33 | + return '/absolute/path/to/build-remote-server.js' |
| 34 | + } |
| 35 | + if (filePath.includes('application-extensions.js')) { |
| 36 | + return '/absolute/path/to/application-extensions.js' |
| 37 | + } |
| 38 | + if (filePath.includes(normalizePath('@salesforce/extension-this/src'))) { |
| 39 | + return '/absolute/path/to/@salesforce/extension-this/src' |
| 40 | + } |
| 41 | + if (filePath.includes(normalizePath('@salesforce/extension-that/src'))) { |
| 42 | + return '/absolute/path/to/@salesforce/extension-that/src' |
| 43 | + } |
| 44 | + throw new Error(`Unexpected path: ${filePath}`) |
| 45 | + }) |
| 46 | + }) |
| 47 | + |
| 48 | + afterEach(() => { |
| 49 | + jest.clearAllMocks() |
| 50 | + }) |
| 51 | + |
| 52 | + test('should return the correct Babel arguments string', () => { |
| 53 | + const expectedArgs = `--only "app/**,/absolute/path/to/build-remote-server.js,/absolute/path/to/application-extensions.js,/absolute/path/to/@salesforce/extension-this/src/**,/absolute/path/to/@salesforce/extension-that/src/**"` |
| 54 | + const result = buildBabelExtensibilityArgs(CONFIG) |
| 55 | + expect(result).toBe(expectedArgs) |
| 56 | + }) |
| 57 | + |
| 58 | + test('should call realpathSync with the correct paths for each extension', () => { |
| 59 | + buildBabelExtensibilityArgs(CONFIG) |
| 60 | + |
| 61 | + // Validate that realpathSync was called with the correct paths |
| 62 | + expect(fse.realpathSync).toHaveBeenCalledWith( |
| 63 | + path.resolve( |
| 64 | + 'node_modules/@salesforce/pwa-kit-runtime/ssr/server/build-remote-server.js' |
| 65 | + ) |
| 66 | + ) |
| 67 | + expect(fse.realpathSync).toHaveBeenCalledWith( |
| 68 | + path.resolve( |
| 69 | + 'node_modules/@salesforce/pwa-kit-extension-sdk/express/placeholders/application-extensions.js' |
| 70 | + ) |
| 71 | + ) |
| 72 | + expect(fse.realpathSync).toHaveBeenCalledWith( |
| 73 | + path.resolve('node_modules/@salesforce/extension-this/src') |
| 74 | + ) |
| 75 | + expect(fse.realpathSync).toHaveBeenCalledWith( |
| 76 | + path.resolve('node_modules/@salesforce/extension-that/src') |
| 77 | + ) |
| 78 | + }) |
| 79 | + |
| 80 | + test('should handle an empty list of configured extensions', () => { |
| 81 | + const expectedArgs = `--only "app/**,/absolute/path/to/build-remote-server.js,/absolute/path/to/application-extensions.js"` |
| 82 | + const result = buildBabelExtensibilityArgs({app: {extensions: []}}) |
| 83 | + expect(result).toBe(expectedArgs) |
| 84 | + const result2 = buildBabelExtensibilityArgs({app: {}}) |
| 85 | + expect(result2).toBe(expectedArgs) |
| 86 | + }) |
| 87 | +}) |
0 commit comments