@paypal/accelerated-checkout-loader
Advanced tools
Comparing version 1.1.0 to 2.0.0-beta.1
@@ -8,4 +8,5 @@ export declare const FL_NAMESPACE = "fastlane"; | ||
readonly AXO_ASSET_PATH: "connect-boba"; | ||
readonly AXO_ESM_PATH: "esm"; | ||
readonly LOCALE_PATH: string; | ||
readonly CDNX_PROD: "https://www.paypalobjects.com"; | ||
}; |
@@ -11,3 +11,3 @@ import { BtModuleKeys, BtModuleLoadConfig } from "./types"; | ||
*/ | ||
export declare function safeLoadBtModule(loadConfig: BtModuleLoadConfig, version: string, minified?: boolean): Promise<true | void | HTMLScriptElement>; | ||
export declare function safeLoadBtModule(loadConfig: BtModuleLoadConfig, version: string, minified?: boolean, enableAmdSupport?: boolean): Promise<true | void | HTMLScriptElement>; | ||
declare global { | ||
@@ -14,0 +14,0 @@ interface Window { |
@@ -8,2 +8,3 @@ import { LoadAxoOptions } from "./types"; | ||
declare function loadAxo(options: LoadAxoOptions): Promise<{ | ||
fastlane: unknown; | ||
metadata: { | ||
@@ -10,0 +11,0 @@ localeUrl: string; |
@@ -56,2 +56,23 @@ /*! ***************************************************************************** | ||
var CDNX_PROD = "https://www.paypalobjects.com"; | ||
var AXO_ASSET_NAME = { | ||
minified: "axo", | ||
unminified: "axo.unminified", | ||
}; | ||
var AXO_ASSET_PATH = "connect-boba"; | ||
var AXO_ESM_PATH = "esm"; | ||
var LOCALE_PATH = "".concat(AXO_ASSET_PATH, "/locales/"); | ||
var constants = { | ||
AXO_ASSET_NAME: AXO_ASSET_NAME, | ||
AXO_ASSET_PATH: AXO_ASSET_PATH, | ||
AXO_ESM_PATH: AXO_ESM_PATH, | ||
LOCALE_PATH: LOCALE_PATH, | ||
CDNX_PROD: CDNX_PROD, | ||
}; | ||
var AxoSupportedPlatforms = { | ||
BT: "BT", | ||
PPCP: "PPCP", | ||
}; | ||
var dist = {}; | ||
@@ -131,22 +152,2 @@ | ||
var CDNX_PROD = "https://www.paypalobjects.com"; | ||
var ASSET_NAME = { | ||
minified: "axo.min", | ||
unminified: "axo", | ||
}; | ||
var FL_NAMESPACE = "fastlane"; | ||
var ASSET_PATH = "connect-boba"; | ||
var LOCALE_PATH = "".concat(ASSET_PATH, "/locales/"); | ||
var constants = { | ||
AXO_ASSET_NAME: ASSET_NAME, | ||
AXO_ASSET_PATH: ASSET_PATH, | ||
LOCALE_PATH: LOCALE_PATH, | ||
CDNX_PROD: CDNX_PROD, | ||
}; | ||
var AxoSupportedPlatforms = { | ||
BT: "BT", | ||
PPCP: "PPCP", | ||
}; | ||
/** | ||
@@ -160,12 +161,2 @@ * Checks if the current environment is an AMD environment. | ||
} | ||
/** | ||
* Checks if the current environment is a RequireJS environment. | ||
* | ||
* @returns {boolean} True if the environment is RequireJS, false otherwise. | ||
*/ | ||
function isRequireJsEnv() { | ||
return (isAmdEnv() && | ||
typeof window.requirejs === "function" && | ||
typeof window.requirejs.config === "function"); | ||
} | ||
@@ -181,5 +172,6 @@ /** | ||
*/ | ||
function safeLoadBtModule(loadConfig, version, minified) { | ||
function safeLoadBtModule(loadConfig, version, minified, enableAmdSupport) { | ||
var _a, _b; | ||
if (minified === void 0) { minified = true; } | ||
if (enableAmdSupport === void 0) { enableAmdSupport = false; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -201,3 +193,3 @@ var bt, existingVersion; | ||
} | ||
return [2 /*return*/, loadBtModule(loadConfig, version, minified)]; | ||
return [2 /*return*/, loadBtModule(loadConfig, version, minified, enableAmdSupport)]; | ||
}); | ||
@@ -213,5 +205,6 @@ }); | ||
*/ | ||
function loadBtModule(loadConfig, version, minified) { | ||
function loadBtModule(loadConfig, version, minified, enableAmdSupport) { | ||
if (minified === void 0) { minified = true; } | ||
if (isAmdEnv()) { | ||
if (enableAmdSupport === void 0) { enableAmdSupport = false; } | ||
if (enableAmdSupport && isAmdEnv()) { | ||
var module_1 = minified | ||
@@ -283,2 +276,56 @@ ? loadConfig.amdModule.minified | ||
function isEsmSupported() { | ||
var script = document.createElement("script"); | ||
return "noModule" in script; | ||
} | ||
function loadModule(url) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, import(url)]; | ||
}); | ||
}); | ||
} | ||
function loadScriptWithExport(_a) { | ||
var src = _a.src; _a.name; var id = _a.id, exportedName = _a.exportedName; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _this = this; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: return [4 /*yield*/, new Promise(function (resolve, reject) { | ||
var script = document.createElement("script"); | ||
script.type = "text/javascript"; | ||
script.src = src; | ||
if (id) { | ||
script.id = id; | ||
} | ||
script.addEventListener("load", function () { return __awaiter(_this, void 0, void 0, function () { | ||
var exportedSymbol; | ||
return __generator(this, function (_a) { | ||
if (exportedName) { | ||
exportedSymbol = exportedName | ||
.split(".") | ||
.reduce(function (result, key) { return result === null || result === void 0 ? void 0 : result[key]; }, window); | ||
resolve(exportedSymbol); | ||
} | ||
else { | ||
resolve(undefined); | ||
} | ||
return [2 /*return*/]; | ||
}); | ||
}); }); | ||
script.addEventListener("error", function () { | ||
reject(new Error("Failed to load fastlane.")); | ||
}); | ||
script.addEventListener("abort", function () { | ||
reject(new Error("fastlane load was aborted.")); | ||
}); | ||
document.getElementsByTagName("head")[0].appendChild(script); | ||
})]; | ||
case 1: return [2 /*return*/, _b.sent()]; | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
@@ -291,3 +338,3 @@ * Loads accelerated checkout components. | ||
return __awaiter(this, void 0, void 0, function () { | ||
var btSdkVersion, minified, assetUrl, localeUrl; | ||
var btSdkVersion, minified, assetUrl, localeUrl, enableAmdSupport, fastlane; | ||
return __generator(this, function (_a) { | ||
@@ -298,8 +345,8 @@ switch (_a.label) { | ||
btSdkVersion = options.btSdkVersion, minified = options.minified; | ||
assetUrl = getAssetsUrl(options); | ||
assetUrl = getAssetPath(options); | ||
localeUrl = getLocaleUrl(options); | ||
enableAmdSupport = options.platform === AxoSupportedPlatforms.BT; | ||
if (!(options.platform === AxoSupportedPlatforms.BT)) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, Promise.all([ | ||
safeLoadBtModule(btModulesLoadConfig.hostedFields, btSdkVersion, minified), | ||
loadAXOScript(assetUrl, minified), | ||
safeLoadBtModule(btModulesLoadConfig.hostedFields, btSdkVersion, enableAmdSupport, minified), | ||
])]; | ||
@@ -314,3 +361,2 @@ case 1: | ||
safeLoadBtModule(btModulesLoadConfig.hostedFields, btSdkVersion, minified), | ||
loadAXOScript(assetUrl, minified), | ||
])]; | ||
@@ -321,3 +367,6 @@ case 3: | ||
case 4: throw new Error("unsupported axo platform"); | ||
case 5: return [2 /*return*/, { metadata: { localeUrl: localeUrl } }]; | ||
case 5: return [4 /*yield*/, loadAXOScript(assetUrl)]; | ||
case 6: | ||
fastlane = _a.sent(); | ||
return [2 /*return*/, { fastlane: fastlane, metadata: { localeUrl: localeUrl } }]; | ||
} | ||
@@ -333,26 +382,22 @@ }); | ||
function loadAXOScript(url, minified) { | ||
var _a; | ||
if (minified === void 0) { minified = true; } | ||
if (isAmdEnv()) { | ||
// AMD environment | ||
if (isRequireJsEnv()) { | ||
// Let's configure RequireJS | ||
requirejs.config({ | ||
paths: (_a = {}, | ||
_a[FL_NAMESPACE] = url, | ||
_a), | ||
}); | ||
} | ||
var moduleName_1 = "".concat(FL_NAMESPACE, "/").concat(minified | ||
? constants.AXO_ASSET_NAME.minified | ||
: constants.AXO_ASSET_NAME.unminified); | ||
return new Promise(function (resolve, reject) { | ||
window.require([moduleName_1], resolve, reject); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var fastlane; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!isEsmSupported()) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, loadModule(url)]; | ||
case 1: | ||
fastlane = (_a.sent()).default; | ||
window.braintree = window.braintree || {}; | ||
window.braintree.fastlane = fastlane; | ||
return [2 /*return*/, fastlane]; | ||
case 2: return [2 /*return*/, loadScriptWithExport({ | ||
id: "axo-id", | ||
src: url, | ||
name: "Fastlane", | ||
exportedName: "braintree.fastlane", | ||
})]; | ||
} | ||
}); | ||
} | ||
// Not an AMD environment | ||
return loadScript_1({ | ||
id: "axo-id", | ||
src: url, | ||
forceScriptReload: true, | ||
}); | ||
@@ -366,6 +411,6 @@ } | ||
function generateAssetUrl(_a) { | ||
var assetUrl = _a.assetUrl, bundleId = _a.bundleId; | ||
var assetPath = _a.assetPath, bundleId = _a.bundleId; | ||
return bundleId | ||
? "https://cdn-".concat(bundleId, ".static.engineering.dev.paypalinc.com/").concat(assetUrl) | ||
: "".concat(constants.CDNX_PROD, "/").concat(assetUrl); | ||
? "https://cdn-".concat(bundleId, ".static.engineering.dev.paypalinc.com/").concat(assetPath) | ||
: "".concat(constants.CDNX_PROD, "/").concat(assetPath); | ||
} | ||
@@ -377,3 +422,3 @@ /** | ||
*/ | ||
function getAssetsUrl(options) { | ||
function getAssetPath(options) { | ||
var _a; | ||
@@ -383,7 +428,5 @@ var assetName = (options === null || options === void 0 ? void 0 : options.minified) !== false | ||
: constants.AXO_ASSET_NAME.unminified; | ||
var assetUrl = isAmdEnv() | ||
? constants.AXO_ASSET_PATH | ||
: "".concat(constants.AXO_ASSET_PATH, "/").concat(assetName, ".js"); | ||
var assetPath = "".concat(constants.AXO_ASSET_PATH, "/").concat(isEsmSupported() ? constants.AXO_ESM_PATH + "/" : "").concat(assetName, ".js"); | ||
return generateAssetUrl({ | ||
assetUrl: assetUrl, | ||
assetPath: assetPath, | ||
bundleId: (_a = options === null || options === void 0 ? void 0 : options.metadata) === null || _a === void 0 ? void 0 : _a.bundleIdOverride, | ||
@@ -400,3 +443,3 @@ }); | ||
return generateAssetUrl({ | ||
assetUrl: constants.LOCALE_PATH, | ||
assetPath: constants.LOCALE_PATH, | ||
bundleId: (_a = options === null || options === void 0 ? void 0 : options.metadata) === null || _a === void 0 ? void 0 : _a.bundleIdOverride, | ||
@@ -403,0 +446,0 @@ }); |
@@ -62,2 +62,23 @@ (function (global, factory) { | ||
var CDNX_PROD = "https://www.paypalobjects.com"; | ||
var AXO_ASSET_NAME = { | ||
minified: "axo", | ||
unminified: "axo.unminified", | ||
}; | ||
var AXO_ASSET_PATH = "connect-boba"; | ||
var AXO_ESM_PATH = "esm"; | ||
var LOCALE_PATH = "".concat(AXO_ASSET_PATH, "/locales/"); | ||
var constants = { | ||
AXO_ASSET_NAME: AXO_ASSET_NAME, | ||
AXO_ASSET_PATH: AXO_ASSET_PATH, | ||
AXO_ESM_PATH: AXO_ESM_PATH, | ||
LOCALE_PATH: LOCALE_PATH, | ||
CDNX_PROD: CDNX_PROD, | ||
}; | ||
var AxoSupportedPlatforms = { | ||
BT: "BT", | ||
PPCP: "PPCP", | ||
}; | ||
var dist = {}; | ||
@@ -137,22 +158,2 @@ | ||
var CDNX_PROD = "https://www.paypalobjects.com"; | ||
var ASSET_NAME = { | ||
minified: "axo.min", | ||
unminified: "axo", | ||
}; | ||
var FL_NAMESPACE = "fastlane"; | ||
var ASSET_PATH = "connect-boba"; | ||
var LOCALE_PATH = "".concat(ASSET_PATH, "/locales/"); | ||
var constants = { | ||
AXO_ASSET_NAME: ASSET_NAME, | ||
AXO_ASSET_PATH: ASSET_PATH, | ||
LOCALE_PATH: LOCALE_PATH, | ||
CDNX_PROD: CDNX_PROD, | ||
}; | ||
var AxoSupportedPlatforms = { | ||
BT: "BT", | ||
PPCP: "PPCP", | ||
}; | ||
/** | ||
@@ -166,12 +167,2 @@ * Checks if the current environment is an AMD environment. | ||
} | ||
/** | ||
* Checks if the current environment is a RequireJS environment. | ||
* | ||
* @returns {boolean} True if the environment is RequireJS, false otherwise. | ||
*/ | ||
function isRequireJsEnv() { | ||
return (isAmdEnv() && | ||
typeof window.requirejs === "function" && | ||
typeof window.requirejs.config === "function"); | ||
} | ||
@@ -187,5 +178,6 @@ /** | ||
*/ | ||
function safeLoadBtModule(loadConfig, version, minified) { | ||
function safeLoadBtModule(loadConfig, version, minified, enableAmdSupport) { | ||
var _a, _b; | ||
if (minified === void 0) { minified = true; } | ||
if (enableAmdSupport === void 0) { enableAmdSupport = false; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -207,3 +199,3 @@ var bt, existingVersion; | ||
} | ||
return [2 /*return*/, loadBtModule(loadConfig, version, minified)]; | ||
return [2 /*return*/, loadBtModule(loadConfig, version, minified, enableAmdSupport)]; | ||
}); | ||
@@ -219,5 +211,6 @@ }); | ||
*/ | ||
function loadBtModule(loadConfig, version, minified) { | ||
function loadBtModule(loadConfig, version, minified, enableAmdSupport) { | ||
if (minified === void 0) { minified = true; } | ||
if (isAmdEnv()) { | ||
if (enableAmdSupport === void 0) { enableAmdSupport = false; } | ||
if (enableAmdSupport && isAmdEnv()) { | ||
var module_1 = minified | ||
@@ -289,2 +282,56 @@ ? loadConfig.amdModule.minified | ||
function isEsmSupported() { | ||
var script = document.createElement("script"); | ||
return "noModule" in script; | ||
} | ||
function loadModule(url) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, import(url)]; | ||
}); | ||
}); | ||
} | ||
function loadScriptWithExport(_a) { | ||
var src = _a.src; _a.name; var id = _a.id, exportedName = _a.exportedName; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _this = this; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: return [4 /*yield*/, new Promise(function (resolve, reject) { | ||
var script = document.createElement("script"); | ||
script.type = "text/javascript"; | ||
script.src = src; | ||
if (id) { | ||
script.id = id; | ||
} | ||
script.addEventListener("load", function () { return __awaiter(_this, void 0, void 0, function () { | ||
var exportedSymbol; | ||
return __generator(this, function (_a) { | ||
if (exportedName) { | ||
exportedSymbol = exportedName | ||
.split(".") | ||
.reduce(function (result, key) { return result === null || result === void 0 ? void 0 : result[key]; }, window); | ||
resolve(exportedSymbol); | ||
} | ||
else { | ||
resolve(undefined); | ||
} | ||
return [2 /*return*/]; | ||
}); | ||
}); }); | ||
script.addEventListener("error", function () { | ||
reject(new Error("Failed to load fastlane.")); | ||
}); | ||
script.addEventListener("abort", function () { | ||
reject(new Error("fastlane load was aborted.")); | ||
}); | ||
document.getElementsByTagName("head")[0].appendChild(script); | ||
})]; | ||
case 1: return [2 /*return*/, _b.sent()]; | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
@@ -297,3 +344,3 @@ * Loads accelerated checkout components. | ||
return __awaiter(this, void 0, void 0, function () { | ||
var btSdkVersion, minified, assetUrl, localeUrl; | ||
var btSdkVersion, minified, assetUrl, localeUrl, enableAmdSupport, fastlane; | ||
return __generator(this, function (_a) { | ||
@@ -304,8 +351,8 @@ switch (_a.label) { | ||
btSdkVersion = options.btSdkVersion, minified = options.minified; | ||
assetUrl = getAssetsUrl(options); | ||
assetUrl = getAssetPath(options); | ||
localeUrl = getLocaleUrl(options); | ||
enableAmdSupport = options.platform === AxoSupportedPlatforms.BT; | ||
if (!(options.platform === AxoSupportedPlatforms.BT)) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, Promise.all([ | ||
safeLoadBtModule(btModulesLoadConfig.hostedFields, btSdkVersion, minified), | ||
loadAXOScript(assetUrl, minified), | ||
safeLoadBtModule(btModulesLoadConfig.hostedFields, btSdkVersion, enableAmdSupport, minified), | ||
])]; | ||
@@ -320,3 +367,2 @@ case 1: | ||
safeLoadBtModule(btModulesLoadConfig.hostedFields, btSdkVersion, minified), | ||
loadAXOScript(assetUrl, minified), | ||
])]; | ||
@@ -327,3 +373,6 @@ case 3: | ||
case 4: throw new Error("unsupported axo platform"); | ||
case 5: return [2 /*return*/, { metadata: { localeUrl: localeUrl } }]; | ||
case 5: return [4 /*yield*/, loadAXOScript(assetUrl)]; | ||
case 6: | ||
fastlane = _a.sent(); | ||
return [2 /*return*/, { fastlane: fastlane, metadata: { localeUrl: localeUrl } }]; | ||
} | ||
@@ -339,26 +388,22 @@ }); | ||
function loadAXOScript(url, minified) { | ||
var _a; | ||
if (minified === void 0) { minified = true; } | ||
if (isAmdEnv()) { | ||
// AMD environment | ||
if (isRequireJsEnv()) { | ||
// Let's configure RequireJS | ||
requirejs.config({ | ||
paths: (_a = {}, | ||
_a[FL_NAMESPACE] = url, | ||
_a), | ||
}); | ||
} | ||
var moduleName_1 = "".concat(FL_NAMESPACE, "/").concat(minified | ||
? constants.AXO_ASSET_NAME.minified | ||
: constants.AXO_ASSET_NAME.unminified); | ||
return new Promise(function (resolve, reject) { | ||
window.require([moduleName_1], resolve, reject); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var fastlane; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!isEsmSupported()) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, loadModule(url)]; | ||
case 1: | ||
fastlane = (_a.sent()).default; | ||
window.braintree = window.braintree || {}; | ||
window.braintree.fastlane = fastlane; | ||
return [2 /*return*/, fastlane]; | ||
case 2: return [2 /*return*/, loadScriptWithExport({ | ||
id: "axo-id", | ||
src: url, | ||
name: "Fastlane", | ||
exportedName: "braintree.fastlane", | ||
})]; | ||
} | ||
}); | ||
} | ||
// Not an AMD environment | ||
return loadScript_1({ | ||
id: "axo-id", | ||
src: url, | ||
forceScriptReload: true, | ||
}); | ||
@@ -372,6 +417,6 @@ } | ||
function generateAssetUrl(_a) { | ||
var assetUrl = _a.assetUrl, bundleId = _a.bundleId; | ||
var assetPath = _a.assetPath, bundleId = _a.bundleId; | ||
return bundleId | ||
? "https://cdn-".concat(bundleId, ".static.engineering.dev.paypalinc.com/").concat(assetUrl) | ||
: "".concat(constants.CDNX_PROD, "/").concat(assetUrl); | ||
? "https://cdn-".concat(bundleId, ".static.engineering.dev.paypalinc.com/").concat(assetPath) | ||
: "".concat(constants.CDNX_PROD, "/").concat(assetPath); | ||
} | ||
@@ -383,3 +428,3 @@ /** | ||
*/ | ||
function getAssetsUrl(options) { | ||
function getAssetPath(options) { | ||
var _a; | ||
@@ -389,7 +434,5 @@ var assetName = (options === null || options === void 0 ? void 0 : options.minified) !== false | ||
: constants.AXO_ASSET_NAME.unminified; | ||
var assetUrl = isAmdEnv() | ||
? constants.AXO_ASSET_PATH | ||
: "".concat(constants.AXO_ASSET_PATH, "/").concat(assetName, ".js"); | ||
var assetPath = "".concat(constants.AXO_ASSET_PATH, "/").concat(isEsmSupported() ? constants.AXO_ESM_PATH + "/" : "").concat(assetName, ".js"); | ||
return generateAssetUrl({ | ||
assetUrl: assetUrl, | ||
assetPath: assetPath, | ||
bundleId: (_a = options === null || options === void 0 ? void 0 : options.metadata) === null || _a === void 0 ? void 0 : _a.bundleIdOverride, | ||
@@ -406,3 +449,3 @@ }); | ||
return generateAssetUrl({ | ||
assetUrl: constants.LOCALE_PATH, | ||
assetPath: constants.LOCALE_PATH, | ||
bundleId: (_a = options === null || options === void 0 ? void 0 : options.metadata) === null || _a === void 0 ? void 0 : _a.bundleIdOverride, | ||
@@ -409,0 +452,0 @@ }); |
@@ -5,4 +5,4 @@ /// <reference types="requirejs" /> | ||
}; | ||
export type GenerateAssetUrl = { | ||
assetUrl: string; | ||
export type GenerateAssetUrlParams = { | ||
assetPath: string; | ||
bundleId?: string; | ||
@@ -9,0 +9,0 @@ }; |
{ | ||
"name": "@paypal/accelerated-checkout-loader", | ||
"version": "1.1.0", | ||
"version": "2.0.0-beta.1", | ||
"description": "Loads accelerated checkout script", | ||
@@ -5,0 +5,0 @@ "main": "dist/loader.umd.js", |
@@ -1,20 +0,30 @@ | ||
import { isAmdEnv, isRequireJsEnv } from "../lib/amd-utils"; | ||
import { isAmdEnv } from "../lib/amd-utils"; | ||
import { constants, FL_NAMESPACE } from "../constants"; | ||
import assetLoader from "@braintree/asset-loader"; | ||
import loadAxo from "../loadAxoScript"; | ||
import { AxoSupportedPlatforms } from "../types"; | ||
import { btModulesLoadConfig } from "../lib/safeLoadBtModule/types"; | ||
import { isEsmSupported, loadModule } from "../lib/esm-utils"; | ||
import { loadScriptWithExport } from "../lib/script-utils"; | ||
jest.mock("@braintree/asset-loader", () => { | ||
return { | ||
loadScript: jest.fn(), | ||
loadStylesheet: jest.fn(), | ||
loadScript: jest.fn().mockResolvedValue(undefined), | ||
loadStylesheet: jest.fn().mockResolvedValue(undefined), | ||
}; | ||
}); | ||
jest.mock("../lib/esm-utils"); | ||
const mockIsEsmSupported = isEsmSupported as jest.MockedFn<typeof isAmdEnv>; | ||
const mockLoadModule = loadModule as jest.MockedFn<typeof loadModule>; | ||
jest.mock("../lib/amd-utils"); | ||
const mockIsAmdEnv = isAmdEnv as jest.MockedFn<typeof isAmdEnv>; | ||
const mockIsRequireJsEnv = isRequireJsEnv as jest.MockedFn< | ||
typeof isRequireJsEnv | ||
jest.mock("../lib/script-utils"); | ||
const mockLoadScriptWithExport = loadScriptWithExport as jest.MockedFn< | ||
typeof loadScriptWithExport | ||
>; | ||
import loadAxo from "../loadAxoScript"; | ||
describe("loadAxoScript", () => { | ||
@@ -25,16 +35,25 @@ let testContext; | ||
const originalPerformance = performance; | ||
const originalGetElementsByTagName = document.getElementsByTagName; | ||
let mockFastlane; | ||
beforeEach(() => { | ||
testContext = {}; | ||
testContext.mockLoadResponse = { | ||
metadata: { | ||
localeUrl: "https://www.paypalobjects.com/connect-boba/locales/", | ||
mockFastlane = jest.fn(); | ||
testContext = { | ||
mockLoadResponse: { | ||
fastlane: mockFastlane, | ||
metadata: { | ||
localeUrl: "https://www.paypalobjects.com/connect-boba/locales/", | ||
}, | ||
}, | ||
}; | ||
testContext.client = { | ||
getVersion: () => { | ||
return version; | ||
client: { | ||
getVersion: () => { | ||
return version; | ||
}, | ||
}, | ||
}; | ||
mockLoadModule.mockResolvedValue({ default: mockFastlane }); | ||
mockLoadScriptWithExport.mockResolvedValue(mockFastlane); | ||
// Mock performance API | ||
@@ -57,2 +76,6 @@ Object.defineProperty(globalThis, "performance", { | ||
}); | ||
Object.defineProperty(document, "getElementsByTagName", { | ||
value: originalGetElementsByTagName, | ||
}); | ||
}); | ||
@@ -79,4 +102,4 @@ | ||
it("should call loadScript with the minified URL when specified", async () => { | ||
const promise = loadAxo({ | ||
it("should load Bt hcf and axo when platform is bt", async () => { | ||
await loadAxo({ | ||
platform: AxoSupportedPlatforms.BT, | ||
@@ -87,98 +110,5 @@ btSdkVersion: version, | ||
expect(promise).resolves.toStrictEqual(testContext.mockLoadResponse); | ||
expect(assetLoader.loadScript).toHaveBeenCalled(); | ||
expect(assetLoader.loadScript).toHaveBeenCalledTimes(1); | ||
expect(assetLoader.loadScript).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
src: `${constants.CDNX_PROD}/${constants.AXO_ASSET_PATH}/${constants.AXO_ASSET_NAME.minified}.js`, | ||
}) | ||
); | ||
}); | ||
it("should load the unminified URL when specified", () => { | ||
const promise = loadAxo({ | ||
platform: AxoSupportedPlatforms.BT, | ||
btSdkVersion: version, | ||
minified: false, | ||
}); | ||
expect(promise).resolves.toStrictEqual(testContext.mockLoadResponse); | ||
expect(assetLoader.loadScript).toHaveBeenCalled(); | ||
expect(assetLoader.loadScript).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
src: `${constants.CDNX_PROD}/${constants.AXO_ASSET_PATH}/${constants.AXO_ASSET_NAME.unminified}.js`, | ||
}) | ||
); | ||
}); | ||
it("should default to the minified script when no environment is specified", () => { | ||
const promise = loadAxo({ | ||
platform: AxoSupportedPlatforms.BT, | ||
btSdkVersion: version, | ||
}); | ||
expect(promise).resolves.toStrictEqual(testContext.mockLoadResponse); | ||
expect(assetLoader.loadScript).toHaveBeenCalled(); | ||
expect(assetLoader.loadScript).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
src: `${constants.CDNX_PROD}/${constants.AXO_ASSET_PATH}/${constants.AXO_ASSET_NAME.minified}.js`, | ||
}) | ||
); | ||
}); | ||
it("should load the bundle override URL when specified", () => { | ||
const bundleId = "2bc4343c92655"; | ||
const promise = loadAxo({ | ||
platform: AxoSupportedPlatforms.BT, | ||
btSdkVersion: version, | ||
metadata: { bundleIdOverride: bundleId }, | ||
}); | ||
expect(promise).resolves.toStrictEqual({ | ||
metadata: { | ||
localeUrl: `https://cdn-${bundleId}.static.engineering.dev.paypalinc.com/${constants.LOCALE_PATH}`, | ||
}, | ||
}); | ||
expect(assetLoader.loadScript).toHaveBeenCalled(); | ||
expect(assetLoader.loadScript).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
src: `https://cdn-${bundleId}.static.engineering.dev.paypalinc.com/${constants.AXO_ASSET_PATH}/${constants.AXO_ASSET_NAME.minified}.js`, | ||
}) | ||
); | ||
}); | ||
it("should call loadScript with the minified URL and bundle override URL when specified", async () => { | ||
const bundleId = "9df9369c92655"; | ||
const promise = loadAxo({ | ||
platform: AxoSupportedPlatforms.BT, | ||
btSdkVersion: version, | ||
minified: true, | ||
metadata: { bundleIdOverride: bundleId }, | ||
}); | ||
expect(promise).resolves.toStrictEqual({ | ||
metadata: { | ||
localeUrl: `https://cdn-${bundleId}.static.engineering.dev.paypalinc.com/${constants.LOCALE_PATH}`, | ||
}, | ||
}); | ||
expect(assetLoader.loadScript).toHaveBeenCalled(); | ||
expect(assetLoader.loadScript).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
src: `https://cdn-${bundleId}.static.engineering.dev.paypalinc.com/${constants.AXO_ASSET_PATH}/${constants.AXO_ASSET_NAME.minified}.js`, | ||
}) | ||
); | ||
}); | ||
it("should load Bt hcf and axo when platform is bt", () => { | ||
const promise = loadAxo({ | ||
platform: AxoSupportedPlatforms.BT, | ||
btSdkVersion: version, | ||
minified: true, | ||
}); | ||
expect(promise).resolves.toStrictEqual({ | ||
metadata: { | ||
localeUrl: `${constants.CDNX_PROD}/${constants.LOCALE_PATH}`, | ||
}, | ||
}); | ||
expect(assetLoader.loadScript).toHaveBeenCalledTimes(2); | ||
expect(assetLoader.loadScript).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
id: `hcf-${version}`, | ||
@@ -188,11 +118,6 @@ src: `https://js.braintreegateway.com/web/${version}/js/hosted-fields.min.js`, | ||
); | ||
expect(assetLoader.loadScript).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
src: `${constants.CDNX_PROD}/${constants.AXO_ASSET_PATH}/${constants.AXO_ASSET_NAME.minified}.js`, | ||
}) | ||
); | ||
}); | ||
it("should load Bt client, hcf and Axo when platform is ppcp", () => { | ||
const promise = loadAxo({ | ||
it("should load Bt client, hcf and Axo when platform is ppcp", async () => { | ||
await loadAxo({ | ||
platform: AxoSupportedPlatforms.PPCP, | ||
@@ -203,10 +128,4 @@ btSdkVersion: version, | ||
expect(promise).resolves.toStrictEqual({ | ||
metadata: { | ||
localeUrl: `${constants.CDNX_PROD}/${constants.LOCALE_PATH}`, | ||
}, | ||
}); | ||
expect(assetLoader.loadScript).toHaveBeenCalledTimes(2); | ||
expect(assetLoader.loadScript).toHaveBeenCalledTimes(3); | ||
expect(assetLoader.loadScript).toHaveBeenCalledWith( | ||
@@ -224,91 +143,97 @@ expect.objectContaining({ | ||
); | ||
expect(assetLoader.loadScript).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
src: `${constants.CDNX_PROD}/${constants.AXO_ASSET_PATH}/${constants.AXO_ASSET_NAME.minified}.js`, | ||
}) | ||
); | ||
}); | ||
}); | ||
describe("in an AMD environment", () => { | ||
const originalRequire = require; | ||
let mockRequire: jest.Mock; | ||
beforeEach(() => { | ||
mockIsAmdEnv.mockReturnValue(true); | ||
mockIsRequireJsEnv.mockReturnValue(false); | ||
mockRequire = jest.fn().mockImplementation((deps, cb) => { | ||
cb(); | ||
describe("when ESM is supported", () => { | ||
beforeAll(() => { | ||
mockIsEsmSupported.mockReturnValue(true); | ||
}); | ||
Object.defineProperty(globalThis, "require", { | ||
value: mockRequire, | ||
writable: true, | ||
}); | ||
}); | ||
afterEach(() => { | ||
Object.defineProperty(globalThis, "require", { | ||
value: originalRequire, | ||
it("should call loadModule with the minified URL when specified", async () => { | ||
const promise = loadAxo({ | ||
platform: AxoSupportedPlatforms.BT, | ||
btSdkVersion: version, | ||
minified: true, | ||
}); | ||
await expect(promise).resolves.toStrictEqual( | ||
testContext.mockLoadResponse | ||
); | ||
expect(mockLoadModule).toHaveBeenCalled(); | ||
expect(mockLoadModule).toHaveBeenCalledWith( | ||
`${constants.CDNX_PROD}/${constants.AXO_ASSET_PATH}/${constants.AXO_ESM_PATH}/${constants.AXO_ASSET_NAME.minified}.js` | ||
); | ||
}); | ||
}); | ||
describe("with Require.js specifically", () => { | ||
let mockRequireJs; | ||
let mockRequireJsConfig; | ||
it("should load the unminified URL when specified", async () => { | ||
const promise = loadAxo({ | ||
platform: AxoSupportedPlatforms.BT, | ||
btSdkVersion: version, | ||
minified: false, | ||
}); | ||
beforeEach(() => { | ||
mockIsRequireJsEnv.mockReturnValue(true); | ||
mockRequireJs = jest.fn(); | ||
mockRequireJsConfig = jest.fn(); | ||
mockRequireJs.config = mockRequireJsConfig; | ||
Object.defineProperty(globalThis, "requirejs", { | ||
value: mockRequireJs, | ||
writable: true, | ||
}); | ||
await expect(promise).resolves.toStrictEqual( | ||
testContext.mockLoadResponse | ||
); | ||
expect(mockLoadModule).toHaveBeenCalled(); | ||
expect(mockLoadModule).toHaveBeenCalledWith( | ||
`${constants.CDNX_PROD}/${constants.AXO_ASSET_PATH}/${constants.AXO_ESM_PATH}/${constants.AXO_ASSET_NAME.unminified}.js` | ||
); | ||
}); | ||
afterEach(() => { | ||
Object.defineProperty(globalThis, "requirejs", { | ||
value: undefined, | ||
it("should default to the minified script when no environment is specified", async () => { | ||
const promise = loadAxo({ | ||
platform: AxoSupportedPlatforms.BT, | ||
btSdkVersion: version, | ||
}); | ||
await expect(promise).resolves.toStrictEqual( | ||
testContext.mockLoadResponse | ||
); | ||
expect(mockLoadModule).toHaveBeenCalled(); | ||
expect(mockLoadModule).toHaveBeenCalledWith( | ||
`${constants.CDNX_PROD}/${constants.AXO_ASSET_PATH}/${constants.AXO_ESM_PATH}/${constants.AXO_ASSET_NAME.minified}.js` | ||
); | ||
}); | ||
it("configures Require.js with the fastlane namespace", async () => { | ||
await loadAxo({ | ||
it("should load the bundle override URL when specified", async () => { | ||
const bundleId = "2bc4343c92655"; | ||
const promise = loadAxo({ | ||
platform: AxoSupportedPlatforms.BT, | ||
btSdkVersion: version, | ||
metadata: { bundleIdOverride: bundleId }, | ||
}); | ||
expect(mockRequireJsConfig).toHaveBeenCalledWith({ | ||
paths: { | ||
[FL_NAMESPACE]: "https://www.paypalobjects.com/connect-boba", | ||
await expect(promise).resolves.toStrictEqual({ | ||
fastlane: mockFastlane, | ||
metadata: { | ||
localeUrl: `https://cdn-${bundleId}.static.engineering.dev.paypalinc.com/${constants.LOCALE_PATH}`, | ||
}, | ||
}); | ||
expect(mockLoadModule).toHaveBeenCalled(); | ||
expect(mockLoadModule).toHaveBeenCalledWith( | ||
`https://cdn-${bundleId}.static.engineering.dev.paypalinc.com/${constants.AXO_ASSET_PATH}/${constants.AXO_ESM_PATH}/${constants.AXO_ASSET_NAME.minified}.js` | ||
); | ||
}); | ||
}); | ||
it("should load the minified AMD modules", async () => { | ||
const promise = loadAxo({ | ||
platform: AxoSupportedPlatforms.BT, | ||
btSdkVersion: version, | ||
minified: true, | ||
describe("when ESM is NOT supported", () => { | ||
beforeAll(() => { | ||
mockIsEsmSupported.mockReturnValue(false); | ||
}); | ||
await expect(promise).resolves.toStrictEqual( | ||
testContext.mockLoadResponse | ||
); | ||
expect(mockRequire).toHaveBeenCalledWith( | ||
[btModulesLoadConfig.hostedFields.amdModule.minified], | ||
expect.any(Function), | ||
expect.any(Function) | ||
); | ||
expect(mockRequire).toHaveBeenCalledWith( | ||
[`${FL_NAMESPACE}/axo.min`], | ||
expect.any(Function), | ||
expect.any(Function) | ||
); | ||
expect(assetLoader.loadScript).not.toHaveBeenCalled(); | ||
it("should call loadScript with the minified URL when specified", async () => { | ||
const promise = loadAxo({ | ||
platform: AxoSupportedPlatforms.BT, | ||
btSdkVersion: version, | ||
minified: true, | ||
}); | ||
await expect(promise).resolves.toStrictEqual( | ||
testContext.mockLoadResponse | ||
); | ||
}); | ||
}); | ||
it("should load the unminified AMD modules", async () => { | ||
it("should load the unminified URL when specified", async () => { | ||
const promise = loadAxo({ | ||
@@ -323,8 +248,8 @@ platform: AxoSupportedPlatforms.BT, | ||
); | ||
expect(mockRequire).toHaveBeenCalledWith( | ||
[`${FL_NAMESPACE}/axo`], | ||
expect.any(Function), | ||
expect.any(Function) | ||
expect(mockLoadScriptWithExport).toHaveBeenCalled(); | ||
expect(mockLoadScriptWithExport).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
src: `${constants.CDNX_PROD}/${constants.AXO_ASSET_PATH}/${constants.AXO_ASSET_NAME.unminified}.js`, | ||
}) | ||
); | ||
expect(assetLoader.loadScript).not.toHaveBeenCalled(); | ||
}); | ||
@@ -341,8 +266,8 @@ | ||
); | ||
expect(mockRequire).toHaveBeenCalledWith( | ||
[`${FL_NAMESPACE}/axo.min`], | ||
expect.any(Function), | ||
expect.any(Function) | ||
expect(mockLoadScriptWithExport).toHaveBeenCalled(); | ||
expect(mockLoadScriptWithExport).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
src: `${constants.CDNX_PROD}/${constants.AXO_ASSET_PATH}/${constants.AXO_ASSET_NAME.minified}.js`, | ||
}) | ||
); | ||
expect(assetLoader.loadScript).not.toHaveBeenCalled(); | ||
}); | ||
@@ -359,2 +284,3 @@ | ||
await expect(promise).resolves.toStrictEqual({ | ||
fastlane: mockFastlane, | ||
metadata: { | ||
@@ -364,8 +290,8 @@ localeUrl: `https://cdn-${bundleId}.static.engineering.dev.paypalinc.com/${constants.LOCALE_PATH}`, | ||
}); | ||
expect(mockRequire).toHaveBeenCalledWith( | ||
[`${FL_NAMESPACE}/axo.min`], | ||
expect.any(Function), | ||
expect.any(Function) | ||
expect(mockLoadScriptWithExport).toHaveBeenCalled(); | ||
expect(mockLoadScriptWithExport).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
src: `https://cdn-${bundleId}.static.engineering.dev.paypalinc.com/${constants.AXO_ASSET_PATH}/${constants.AXO_ASSET_NAME.minified}.js`, | ||
}) | ||
); | ||
expect(assetLoader.loadScript).not.toHaveBeenCalled(); | ||
}); | ||
@@ -383,2 +309,3 @@ | ||
await expect(promise).resolves.toStrictEqual({ | ||
fastlane: mockFastlane, | ||
metadata: { | ||
@@ -388,7 +315,34 @@ localeUrl: `https://cdn-${bundleId}.static.engineering.dev.paypalinc.com/${constants.LOCALE_PATH}`, | ||
}); | ||
expect(assetLoader.loadScript).not.toHaveBeenCalled(); | ||
expect(mockLoadScriptWithExport).toHaveBeenCalled(); | ||
expect(mockLoadScriptWithExport).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
src: `https://cdn-${bundleId}.static.engineering.dev.paypalinc.com/${constants.AXO_ASSET_PATH}/${constants.AXO_ASSET_NAME.minified}.js`, | ||
}) | ||
); | ||
}); | ||
}); | ||
it("should load Bt hcf and axo when platform is bt", async () => { | ||
const promise = loadAxo({ | ||
describe("in an AMD environment", () => { | ||
const originalRequire = require; | ||
let mockRequire: jest.Mock; | ||
beforeEach(() => { | ||
mockIsAmdEnv.mockReturnValue(true); | ||
mockRequire = jest.fn().mockImplementation((deps, cb) => { | ||
cb(); | ||
}); | ||
Object.defineProperty(globalThis, "require", { | ||
value: mockRequire, | ||
writable: true, | ||
}); | ||
}); | ||
afterEach(() => { | ||
Object.defineProperty(globalThis, "require", { | ||
value: originalRequire, | ||
}); | ||
}); | ||
it("should load Bt hcf when platform is bt", async () => { | ||
await loadAxo({ | ||
platform: AxoSupportedPlatforms.BT, | ||
@@ -399,9 +353,3 @@ btSdkVersion: version, | ||
await expect(promise).resolves.toStrictEqual({ | ||
metadata: { | ||
localeUrl: `${constants.CDNX_PROD}/${constants.LOCALE_PATH}`, | ||
}, | ||
}); | ||
expect(mockRequire).toHaveBeenCalledTimes(2); | ||
expect(mockRequire).toHaveBeenCalledTimes(1); | ||
expect(mockRequire).toHaveBeenCalledWith( | ||
@@ -412,40 +360,218 @@ [`${btModulesLoadConfig.hostedFields.amdModule.minified}`], | ||
); | ||
expect(mockRequire).toHaveBeenCalledWith( | ||
[`${FL_NAMESPACE}/axo.min`], | ||
expect.any(Function), | ||
expect.any(Function) | ||
); | ||
}); | ||
it("should load Bt client, hcf and Axo when platform is ppcp", async () => { | ||
const promise = loadAxo({ | ||
platform: AxoSupportedPlatforms.PPCP, | ||
btSdkVersion: version, | ||
minified: true, | ||
describe("when ESM is NOT supported", () => { | ||
beforeAll(() => { | ||
mockIsEsmSupported.mockReturnValue(false); | ||
}); | ||
await expect(promise).resolves.toStrictEqual({ | ||
metadata: { | ||
localeUrl: `${constants.CDNX_PROD}/${constants.LOCALE_PATH}`, | ||
}, | ||
it("should load the minified AMD modules", async () => { | ||
const promise = loadAxo({ | ||
platform: AxoSupportedPlatforms.BT, | ||
btSdkVersion: version, | ||
minified: true, | ||
}); | ||
await expect(promise).resolves.toStrictEqual( | ||
testContext.mockLoadResponse | ||
); | ||
expect(mockRequire).toHaveBeenCalledWith( | ||
[btModulesLoadConfig.hostedFields.amdModule.minified], | ||
expect.any(Function), | ||
expect.any(Function) | ||
); | ||
expect(mockLoadScriptWithExport).toHaveBeenCalled(); | ||
expect(mockLoadScriptWithExport).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
src: `${constants.CDNX_PROD}/${constants.AXO_ASSET_PATH}/${constants.AXO_ASSET_NAME.minified}.js`, | ||
}) | ||
); | ||
}); | ||
expect(mockRequire).toHaveBeenCalledTimes(3); | ||
expect(mockRequire).toHaveBeenCalledWith( | ||
[`${btModulesLoadConfig.hostedFields.amdModule.minified}`], | ||
expect.any(Function), | ||
expect.any(Function) | ||
); | ||
expect(mockRequire).toHaveBeenCalledWith( | ||
[`${btModulesLoadConfig.client.amdModule.minified}`], | ||
expect.any(Function), | ||
expect.any(Function) | ||
); | ||
expect(mockRequire).toHaveBeenCalledWith( | ||
[`${FL_NAMESPACE}/axo.min`], | ||
expect.any(Function), | ||
expect.any(Function) | ||
); | ||
it("should load the unminified AMD modules", async () => { | ||
const promise = loadAxo({ | ||
platform: AxoSupportedPlatforms.BT, | ||
btSdkVersion: version, | ||
minified: false, | ||
}); | ||
await expect(promise).resolves.toStrictEqual( | ||
testContext.mockLoadResponse | ||
); | ||
expect(mockLoadScriptWithExport).toHaveBeenCalled(); | ||
expect(mockLoadScriptWithExport).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
src: `${constants.CDNX_PROD}/${constants.AXO_ASSET_PATH}/${constants.AXO_ASSET_NAME.unminified}.js`, | ||
}) | ||
); | ||
}); | ||
it("should default to the minified script when no environment is specified", async () => { | ||
const promise = loadAxo({ | ||
platform: AxoSupportedPlatforms.BT, | ||
btSdkVersion: version, | ||
}); | ||
await expect(promise).resolves.toStrictEqual( | ||
testContext.mockLoadResponse | ||
); | ||
expect(mockLoadScriptWithExport).toHaveBeenCalled(); | ||
expect(mockLoadScriptWithExport).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
src: `${constants.CDNX_PROD}/${constants.AXO_ASSET_PATH}/${constants.AXO_ASSET_NAME.minified}.js`, | ||
}) | ||
); | ||
}); | ||
it("should load the bundle override URL when specified", async () => { | ||
const bundleId = "2bc4343c92655"; | ||
const promise = loadAxo({ | ||
platform: AxoSupportedPlatforms.BT, | ||
btSdkVersion: version, | ||
metadata: { bundleIdOverride: bundleId }, | ||
}); | ||
await expect(promise).resolves.toStrictEqual({ | ||
fastlane: mockFastlane, | ||
metadata: { | ||
localeUrl: `https://cdn-${bundleId}.static.engineering.dev.paypalinc.com/${constants.LOCALE_PATH}`, | ||
}, | ||
}); | ||
expect(mockLoadScriptWithExport).toHaveBeenCalled(); | ||
expect(mockLoadScriptWithExport).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
src: `https://cdn-${bundleId}.static.engineering.dev.paypalinc.com/${constants.AXO_ASSET_PATH}/${constants.AXO_ASSET_NAME.minified}.js`, | ||
}) | ||
); | ||
}); | ||
it("should call loadScript with the minified URL and bundle override URL when specified", async () => { | ||
const bundleId = "9df9369c92655"; | ||
const promise = loadAxo({ | ||
platform: AxoSupportedPlatforms.BT, | ||
btSdkVersion: version, | ||
minified: true, | ||
metadata: { bundleIdOverride: bundleId }, | ||
}); | ||
await expect(promise).resolves.toStrictEqual({ | ||
fastlane: mockFastlane, | ||
metadata: { | ||
localeUrl: `https://cdn-${bundleId}.static.engineering.dev.paypalinc.com/${constants.LOCALE_PATH}`, | ||
}, | ||
}); | ||
expect(mockLoadScriptWithExport).toHaveBeenCalled(); | ||
expect(mockLoadScriptWithExport).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
src: `https://cdn-${bundleId}.static.engineering.dev.paypalinc.com/${constants.AXO_ASSET_PATH}/${constants.AXO_ASSET_NAME.minified}.js`, | ||
}) | ||
); | ||
}); | ||
}); | ||
describe("when ESM is supported", () => { | ||
beforeAll(() => { | ||
mockIsEsmSupported.mockReturnValue(true); | ||
}); | ||
it("should load the minified AMD modules", async () => { | ||
const promise = loadAxo({ | ||
platform: AxoSupportedPlatforms.BT, | ||
btSdkVersion: version, | ||
minified: true, | ||
}); | ||
await expect(promise).resolves.toStrictEqual( | ||
testContext.mockLoadResponse | ||
); | ||
expect(mockRequire).toHaveBeenCalledWith( | ||
[btModulesLoadConfig.hostedFields.amdModule.minified], | ||
expect.any(Function), | ||
expect.any(Function) | ||
); | ||
await expect(promise).resolves.toStrictEqual( | ||
testContext.mockLoadResponse | ||
); | ||
expect(mockLoadModule).toHaveBeenCalled(); | ||
expect(mockLoadModule).toHaveBeenCalledWith( | ||
`${constants.CDNX_PROD}/${constants.AXO_ASSET_PATH}/${constants.AXO_ESM_PATH}/${constants.AXO_ASSET_NAME.minified}.js` | ||
); | ||
}); | ||
it("should load the unminified AMD modules", async () => { | ||
const promise = loadAxo({ | ||
platform: AxoSupportedPlatforms.BT, | ||
btSdkVersion: version, | ||
minified: false, | ||
}); | ||
await expect(promise).resolves.toStrictEqual( | ||
testContext.mockLoadResponse | ||
); | ||
await expect(promise).resolves.toStrictEqual( | ||
testContext.mockLoadResponse | ||
); | ||
expect(mockLoadModule).toHaveBeenCalled(); | ||
expect(mockLoadModule).toHaveBeenCalledWith( | ||
`${constants.CDNX_PROD}/${constants.AXO_ASSET_PATH}/${constants.AXO_ESM_PATH}/${constants.AXO_ASSET_NAME.unminified}.js` | ||
); | ||
}); | ||
it("should default to the minified script when no environment is specified", async () => { | ||
const promise = loadAxo({ | ||
platform: AxoSupportedPlatforms.BT, | ||
btSdkVersion: version, | ||
}); | ||
await expect(promise).resolves.toStrictEqual( | ||
testContext.mockLoadResponse | ||
); | ||
expect(mockLoadModule).toHaveBeenCalled(); | ||
expect(mockLoadModule).toHaveBeenCalledWith( | ||
`${constants.CDNX_PROD}/${constants.AXO_ASSET_PATH}/${constants.AXO_ESM_PATH}/${constants.AXO_ASSET_NAME.minified}.js` | ||
); | ||
}); | ||
it("should load the bundle override URL when specified", async () => { | ||
const bundleId = "2bc4343c92655"; | ||
const promise = loadAxo({ | ||
platform: AxoSupportedPlatforms.BT, | ||
btSdkVersion: version, | ||
metadata: { bundleIdOverride: bundleId }, | ||
}); | ||
await expect(promise).resolves.toStrictEqual({ | ||
fastlane: mockFastlane, | ||
metadata: { | ||
localeUrl: `https://cdn-${bundleId}.static.engineering.dev.paypalinc.com/${constants.LOCALE_PATH}`, | ||
}, | ||
}); | ||
expect(mockLoadModule).toHaveBeenCalled(); | ||
expect(mockLoadModule).toHaveBeenCalledWith( | ||
`https://cdn-${bundleId}.static.engineering.dev.paypalinc.com/${constants.AXO_ASSET_PATH}/${constants.AXO_ESM_PATH}/${constants.AXO_ASSET_NAME.minified}.js` | ||
); | ||
}); | ||
it("should call loadScript with the minified URL and bundle override URL when specified", async () => { | ||
const bundleId = "9df9369c92655"; | ||
const promise = loadAxo({ | ||
platform: AxoSupportedPlatforms.BT, | ||
btSdkVersion: version, | ||
minified: true, | ||
metadata: { bundleIdOverride: bundleId }, | ||
}); | ||
await expect(promise).resolves.toStrictEqual({ | ||
fastlane: mockFastlane, | ||
metadata: { | ||
localeUrl: `https://cdn-${bundleId}.static.engineering.dev.paypalinc.com/${constants.LOCALE_PATH}`, | ||
}, | ||
}); | ||
expect(mockLoadModule).toHaveBeenCalled(); | ||
expect(mockLoadModule).toHaveBeenCalledWith( | ||
`https://cdn-${bundleId}.static.engineering.dev.paypalinc.com/${constants.AXO_ASSET_PATH}/${constants.AXO_ESM_PATH}/${constants.AXO_ASSET_NAME.minified}.js` | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
const CDNX_PROD = "https://www.paypalobjects.com"; | ||
const ASSET_NAME = { | ||
minified: "axo.min", | ||
unminified: "axo", | ||
const AXO_ASSET_NAME = { | ||
minified: "axo", | ||
unminified: "axo.unminified", | ||
}; | ||
@@ -10,11 +10,14 @@ | ||
const ASSET_PATH = "connect-boba"; | ||
const AXO_ASSET_PATH = "connect-boba"; | ||
const LOCALE_PATH = `${ASSET_PATH}/locales/`; | ||
const AXO_ESM_PATH = "esm"; | ||
const LOCALE_PATH = `${AXO_ASSET_PATH}/locales/`; | ||
export const constants = { | ||
AXO_ASSET_NAME: ASSET_NAME, | ||
AXO_ASSET_PATH: ASSET_PATH, | ||
AXO_ASSET_NAME, | ||
AXO_ASSET_PATH, | ||
AXO_ESM_PATH, | ||
LOCALE_PATH, | ||
CDNX_PROD, | ||
} as const; |
@@ -118,5 +118,17 @@ import { safeLoadBtModule } from ".."; | ||
it("should attempt to load BT module", async () => { | ||
it("should call loadScript by default", async () => { | ||
await safeLoadBtModule(btModulesLoadConfig.client, version); | ||
expect(assetLoader.loadScript).toHaveBeenCalled(); | ||
expect(mockRequire).not.toHaveBeenCalled(); | ||
}); | ||
it("should attempt to load BT module", async () => { | ||
await safeLoadBtModule( | ||
btModulesLoadConfig.client, | ||
version, | ||
undefined, | ||
true | ||
); | ||
expect(assetLoader.loadScript).not.toHaveBeenCalled(); | ||
@@ -137,3 +149,8 @@ expect(mockRequire).toHaveBeenCalledWith( | ||
try { | ||
await safeLoadBtModule(btModulesLoadConfig.client, version); | ||
await safeLoadBtModule( | ||
btModulesLoadConfig.client, | ||
version, | ||
undefined, | ||
true | ||
); | ||
} catch (error) { | ||
@@ -140,0 +157,0 @@ expect(error).toBe(mockError); |
@@ -17,3 +17,4 @@ import { loadScript } from "@braintree/asset-loader"; | ||
version: string, | ||
minified = true | ||
minified = true, | ||
enableAmdSupport = false | ||
) { | ||
@@ -39,3 +40,3 @@ const bt = getBraintree(); | ||
return loadBtModule(loadConfig, version, minified); | ||
return loadBtModule(loadConfig, version, minified, enableAmdSupport); | ||
} | ||
@@ -53,5 +54,6 @@ | ||
version: string, | ||
minified = true | ||
minified = true, | ||
enableAmdSupport = false | ||
) { | ||
if (isAmdEnv()) { | ||
if (enableAmdSupport && isAmdEnv()) { | ||
const module = minified | ||
@@ -58,0 +60,0 @@ ? loadConfig.amdModule.minified |
@@ -1,6 +0,5 @@ | ||
import { loadScript } from "@braintree/asset-loader"; | ||
import { constants, FL_NAMESPACE } from "./constants"; | ||
import { | ||
LoadAxoOptions, | ||
GenerateAssetUrl, | ||
GenerateAssetUrlParams, | ||
AxoSupportedPlatforms, | ||
@@ -10,3 +9,4 @@ } from "./types"; | ||
import { btModulesLoadConfig } from "./lib/safeLoadBtModule/types"; | ||
import { isAmdEnv, isRequireJsEnv } from "./lib/amd-utils"; | ||
import { isEsmSupported, loadModule } from "./lib/esm-utils"; | ||
import { loadScriptWithExport } from "./lib/script-utils"; | ||
@@ -21,4 +21,5 @@ /** | ||
const { btSdkVersion, minified } = options; | ||
const assetUrl = getAssetsUrl(options); | ||
const assetUrl = getAssetPath(options); | ||
const localeUrl = getLocaleUrl(options); | ||
const enableAmdSupport = options.platform === AxoSupportedPlatforms.BT; | ||
@@ -30,5 +31,5 @@ if (options.platform === AxoSupportedPlatforms.BT) { | ||
btSdkVersion, | ||
enableAmdSupport, | ||
minified | ||
), | ||
loadAXOScript(assetUrl, minified), | ||
]); | ||
@@ -43,3 +44,2 @@ } else if (options.platform === AxoSupportedPlatforms.PPCP) { | ||
), | ||
loadAXOScript(assetUrl, minified), | ||
]); | ||
@@ -50,3 +50,5 @@ } else { | ||
return { metadata: { localeUrl } }; | ||
const fastlane = await loadAXOScript(assetUrl, minified); | ||
return { fastlane, metadata: { localeUrl } }; | ||
} | ||
@@ -59,30 +61,15 @@ | ||
*/ | ||
function loadAXOScript(url: string, minified = true) { | ||
if (isAmdEnv()) { | ||
// AMD environment | ||
if (isRequireJsEnv()) { | ||
// Let's configure RequireJS | ||
requirejs.config({ | ||
paths: { | ||
[FL_NAMESPACE]: url, | ||
}, | ||
}); | ||
} | ||
const moduleName = `${FL_NAMESPACE}/${ | ||
minified | ||
? constants.AXO_ASSET_NAME.minified | ||
: constants.AXO_ASSET_NAME.unminified | ||
}`; | ||
return new Promise<void>((resolve, reject) => { | ||
window.require([moduleName], resolve, reject); | ||
}); | ||
async function loadAXOScript(url: string, minified = true) { | ||
if (isEsmSupported()) { | ||
const { default: fastlane } = await loadModule(url); | ||
window.braintree = window.braintree || {}; | ||
window.braintree.fastlane = fastlane; | ||
return fastlane; | ||
} | ||
// Not an AMD environment | ||
return loadScript({ | ||
return loadScriptWithExport({ | ||
id: "axo-id", | ||
src: url, | ||
forceScriptReload: true, | ||
name: "Fastlane", | ||
exportedName: "braintree.fastlane", | ||
}); | ||
@@ -96,6 +83,9 @@ } | ||
*/ | ||
function generateAssetUrl({ assetUrl, bundleId }: GenerateAssetUrl): string { | ||
function generateAssetUrl({ | ||
assetPath, | ||
bundleId, | ||
}: GenerateAssetUrlParams): string { | ||
return bundleId | ||
? `https://cdn-${bundleId}.static.engineering.dev.paypalinc.com/${assetUrl}` | ||
: `${constants.CDNX_PROD}/${assetUrl}`; | ||
? `https://cdn-${bundleId}.static.engineering.dev.paypalinc.com/${assetPath}` | ||
: `${constants.CDNX_PROD}/${assetPath}`; | ||
} | ||
@@ -108,3 +98,3 @@ | ||
*/ | ||
function getAssetsUrl(options?: LoadAxoOptions): string { | ||
function getAssetPath(options?: LoadAxoOptions): string { | ||
const assetName = | ||
@@ -115,7 +105,8 @@ options?.minified !== false | ||
const assetUrl = isAmdEnv() | ||
? constants.AXO_ASSET_PATH | ||
: `${constants.AXO_ASSET_PATH}/${assetName}.js`; | ||
const assetPath = `${constants.AXO_ASSET_PATH}/${ | ||
isEsmSupported() ? constants.AXO_ESM_PATH + "/" : "" | ||
}${assetName}.js`; | ||
return generateAssetUrl({ | ||
assetUrl, | ||
assetPath, | ||
bundleId: options?.metadata?.bundleIdOverride, | ||
@@ -132,3 +123,3 @@ }); | ||
return generateAssetUrl({ | ||
assetUrl: constants.LOCALE_PATH, | ||
assetPath: constants.LOCALE_PATH, | ||
bundleId: options?.metadata?.bundleIdOverride, | ||
@@ -135,0 +126,0 @@ }); |
@@ -5,4 +5,4 @@ type LoadAxoOptionsMetaData = { | ||
export type GenerateAssetUrl = { | ||
assetUrl: string; | ||
export type GenerateAssetUrlParams = { | ||
assetPath: string; | ||
bundleId?: string; | ||
@@ -9,0 +9,0 @@ }; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
87772
28
2060
2