UNPKG

@stripe/ui-extension-sdk

Version:

The suite of functionality available to UI extensions in Stripe apps

107 lines (104 loc) 6.2 kB
"use strict"; /* Copyright 2020-present, Shopify Inc. Copyright 2022-present, Stripe Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.assertIsNode = assertIsNode; exports.assertIsType = assertIsType; exports.diffs = diffs; exports.printType = printType; exports.diffPropsForNode = diffPropsForNode; exports.pluralize = pluralize; exports.printReceivedWithHighlight = printReceivedWithHighlight; const jest_matcher_utils_1 = require("jest-matcher-utils"); const testing_1 = require("@remote-ui/testing"); function assertIsNode(node, { expectation, isNot }) { if (node == null) { throw new Error((0, jest_matcher_utils_1.matcherErrorMessage)((0, jest_matcher_utils_1.matcherHint)(`.${expectation}`, undefined, undefined, { isNot }), `${(0, jest_matcher_utils_1.RECEIVED_COLOR)('received')} value must be a Root or Element object`, `Received ${(0, jest_matcher_utils_1.RECEIVED_COLOR)('null')}.\nThis usually means that your \`.findX\` method failed to find any matching elements.`)); } if (Array.isArray(node) && node.length > 1 && (0, testing_1.isNode)(node[0])) { throw new Error((0, jest_matcher_utils_1.matcherErrorMessage)((0, jest_matcher_utils_1.matcherHint)(`.${expectation}`, undefined, undefined, { isNot }), `${(0, jest_matcher_utils_1.RECEIVED_COLOR)('received')} value must be a Root or Element object`, `Received an ${(0, jest_matcher_utils_1.RECEIVED_COLOR)('array of Root or Element objects')}.\nThis usually means that you passed in the result of \`.findAllX\`. Pass the result of \`.findX\` instead.`)); } if (!(0, testing_1.isNode)(node)) { throw new Error((0, jest_matcher_utils_1.matcherErrorMessage)((0, jest_matcher_utils_1.matcherHint)(`.${expectation}`, undefined, undefined, { isNot }), `${(0, jest_matcher_utils_1.RECEIVED_COLOR)('received')} value must be a Root or Element object`, (0, jest_matcher_utils_1.printWithType)('Received', node, jest_matcher_utils_1.printReceived))); } } function assertIsType(type, { expectation, isNot }) { if (type == null) { throw new Error((0, jest_matcher_utils_1.matcherErrorMessage)((0, jest_matcher_utils_1.matcherHint)(`.${expectation}`, undefined, undefined, { isNot }), `${(0, jest_matcher_utils_1.RECEIVED_COLOR)('expected')} value must be a string or a valid React component.`, (0, jest_matcher_utils_1.printWithType)('Expected', type, jest_matcher_utils_1.printExpected))); } } function diffs(element, props, expand) { // eslint-disable-next-line no-shadow return element.reduce((diffs, element, index) => { const separator = index === 0 ? '' : '\n\n'; // eslint-disable-next-line @typescript-eslint/no-use-before-define return `${diffs}${separator}${normalizedDiff(element, props, { expand, showLegend: index === 0, })}`; }, ''); } function normalizedDiff(element, props, { expand = false, showLegend = false }) { const result = // eslint-disable-next-line @typescript-eslint/no-use-before-define diffPropsForNode(element, props, { expand, }) || ''; return showLegend ? result : result.split('\n\n')[1]; } function printType(type) { if (typeof type === 'object' && '_context' in type) { const context = type._context; const componentName = type === context.Provider ? 'Provider' : 'Consumer'; const displayName = context.displayName || 'Context'; return `<${displayName}.${componentName} />`; } const displayName = typeof type === 'string' ? type : type.displayName || type.name || 'Component'; return `<${displayName} />`; } function diffPropsForNode(node, props, { expand = false }) { // eslint-disable-next-line @typescript-eslint/no-use-before-define return (0, jest_matcher_utils_1.diff)(props, getObjectSubset(node.props, props), { expand, }); } // Original from https://github.com/facebook/jest/blob/master/packages/expect/src/utils.ts#L107 function getObjectSubset(object, subset) { if (Array.isArray(object)) { if (Array.isArray(subset) && subset.length === object.length) { return subset.map((sub, i) => getObjectSubset(object[i], sub)); } } else if (object instanceof Date) { return object; } else if (typeof object === 'object' && object !== null && typeof subset === 'object' && subset !== null) { const trimmed = {}; Object.keys(subset) .filter((key) => Reflect.has(object, key)) .forEach( // eslint-disable-next-line no-return-assign (key) => (trimmed[key] = getObjectSubset(object[key], subset[key]))); if (Object.keys(trimmed).length > 0) { return trimmed; } } return object; } function pluralize(word, count) { return count === 1 ? word : `${word}s`; } function printReceivedWithHighlight(text, start, length) { return (0, jest_matcher_utils_1.RECEIVED_COLOR)(`"${text.slice(0, start)}${(0, jest_matcher_utils_1.INVERTED_COLOR)(text.slice(start, start + length))}${text.slice(start + length)}"`); } //# sourceMappingURL=utilities.js.map