From 2b20d30e42e1b1f3e8b983ec886edbbfb3049b88 Mon Sep 17 00:00:00 2001 From: sponglord Date: Fri, 29 Oct 2021 17:15:55 +0200 Subject: [PATCH] Implementing handling an optional expiry date as determined by binLookup (#1328) * Implementing handling an optional expiry date as determined by binLookup * Add 'korean_local_card' brand to playground KCP example * Updating securedFields playground file to work with v5 * Refactor how we detect single date field and clear error (if optional or hidden) * Fixed tests in customcard.unsupportedCard.test.js * Removed unused imports from test * Fix so that ClientFunction runs for test (removed ref to DOM element) * Fixed bug where card could not become valid if dateField eas optional but had an error due to an isValidate call * Also clear isValidated errors for optional/hidden separate date fields (custom card scenario) * Add classes for ExpirationDate component in same format as for CVC * Create e2e Page Model for testing Card Component * Added first e2e tests (using Page Model) for optional expiryDate * Another test added * Improved CardPage.getFromWindow fn * Improved routine to remove DOM elements from state.errors * Added assertion timeouts to 3DS2 tests to allow flexibility in how long it takes for challenge window to load * Slowed down 3rd expiryDate test more so that it works better when part of a batch of tests (testcafe bug) * Mock response fny made generic (part of BasePage) for more easy reuse * Added tests for expiryDatePolicy = 'hidden' * Create type for window.mockBinCount so that e2e tests have a way to know/turn off SDK bin mocking (in case it's accidentally been left on) * Added functionality for e2e tests to know/turn off SDK bin mocking (in case it's accidentally been left on) * Added line to 'hidden' test * Moved checkMocking.test so that it is always the first run * Adding clientScripts to fixture last seems to allow onChange event to always fire (was sometimes failing when entire test suite was run) * Adding code to custom card playground & e2e files to handle 'optional' date & cvc policies * Removed id's from playground & e2e custom card examples. Allowed separate date field example to support dual branding * Added CustomCard Page model & tests for optional expiryDate on the "regular" custom card * Aligning class names for the "separate" custom card * Added new utils for Card pages * Added tests for optional expiryDate on the "separate" custom card * Added utils for retrieving iframe input & aria error fields * Convert customcard.unsupportedCard.test.js to use Custom card Page model (& stripped repeated test code) * onChange function for Cards that maps state.errors to remove DOM nodes - moved to Cards.js (it's just not always picked up from the expiryDate.clientScript.js for some reason) * Comments added * Added assertion in e2e test for undefined object * removing unused code * Replaced zeroPad fn with String.padStart * Mock related functionality moved out from BasePage to a mocks util for the Card related pages & tests --- packages/e2e/app/src/pages/Cards/Cards.js | 19 +- .../src/pages/CustomCards/CustomCards.html | 28 +- .../app/src/pages/CustomCards/CustomCards.js | 2 + .../pages/CustomCards/customCards.config.js | 75 +++-- packages/e2e/tests/_common/cardMocks.js | 44 +++ .../e2e/tests/_common/checkMocking.test.js | 14 + packages/e2e/tests/_models/BasePage.js | 25 ++ .../e2e/tests/_models/CardComponent.page.js | 101 ++++++ .../tests/_models/CustomCardComponent.page.js | 125 ++++++++ .../binLookup.clientScripts.js | 6 + .../expiryDatePolicies.hidden.test.js | 134 ++++++++ .../expiryDatePolicies.optional.test.js | 175 +++++++++++ .../threeDS2/threeDS2.default.size.test.js | 19 +- packages/e2e/tests/cards/utils/cardUtils.js | 38 ++- packages/e2e/tests/cards/utils/kcpUtils.js | 9 +- .../customcard.unsupportedCard.test.js | 296 ++++++------------ ...xpiryDatePolicies.optional.regular.test.js | 166 ++++++++++ ...piryDatePolicies.optional.separate.test.js | 197 ++++++++++++ .../minimumExpiryDate.separate.test.js | 2 - .../tests/customcard/utils/customCardUtils.js | 27 +- packages/e2e/tests/index.js | 10 + packages/e2e/tests/utils/commonUtils.js | 30 +- packages/lib/config/rollup.config.js | 2 + .../Card/components/CardInput/CardInput.tsx | 8 +- .../CardInput/components/CardFields.tsx | 7 +- .../CardInput/components/ExpirationDate.tsx | 11 +- .../components/CardInput/components/types.ts | 11 +- .../Card/components/CardInput/types.ts | 7 +- packages/lib/src/components/Card/types.ts | 3 +- .../SecuredFields/SecuredFieldsProvider.ts | 13 +- .../SecuredFieldsProviderHandlers.ts | 54 +++- .../internal/SecuredFields/defaultProps.ts | 3 +- .../lib/configuration/constants.ts | 19 +- .../SecuredFields/lib/core/AbstractCSF.ts | 3 +- .../lib/core/AbstractSecuredField.ts | 7 +- .../internal/SecuredFields/lib/core/CSF.ts | 8 +- .../lib/core/SecuredField.test.ts | 2 +- .../SecuredFields/lib/core/SecuredField.ts | 49 ++- .../lib/core/createSecuredFields.ts | 2 +- .../lib/core/handleEncryption.ts | 3 +- .../lib/core/handleValidation.ts | 7 +- .../SecuredFields/lib/core/initCSF.ts | 3 +- .../core/utils/handleBrandFromBinLookup.ts | 64 +++- .../lib/core/utils/iframes/setFocusOnFrame.ts | 3 +- .../lib/core/utils/processAutoComplete.ts | 7 +- .../lib/core/utils/processBrand.ts | 14 +- .../lib/core/utils/processErrors.ts | 3 +- .../internal/SecuredFields/lib/types.ts | 14 +- .../SecuredFields/lib/utilities/cardType.ts | 3 +- .../lib/utilities/commonUtils.test.ts | 237 +++++++++++++- .../lib/utilities/commonUtils.ts | 26 +- packages/lib/src/utils/hasOwnProperty.ts | 3 + packages/playground/src/pages/Cards/Cards.js | 2 +- .../pages/SecuredFields/SecuredFields.html | 40 +-- .../SecuredFields/securedFields.config.js | 46 ++- 55 files changed, 1823 insertions(+), 403 deletions(-) create mode 100644 packages/e2e/tests/_common/cardMocks.js create mode 100644 packages/e2e/tests/_common/checkMocking.test.js create mode 100644 packages/e2e/tests/_models/BasePage.js create mode 100644 packages/e2e/tests/_models/CardComponent.page.js create mode 100644 packages/e2e/tests/_models/CustomCardComponent.page.js create mode 100644 packages/e2e/tests/cards/expiryDate/expiryDatePolicies.hidden.test.js create mode 100644 packages/e2e/tests/cards/expiryDate/expiryDatePolicies.optional.test.js create mode 100644 packages/e2e/tests/customcard/expiryDate/expiryDatePolicies.optional.regular.test.js create mode 100644 packages/e2e/tests/customcard/expiryDate/expiryDatePolicies.optional.separate.test.js create mode 100644 packages/lib/src/utils/hasOwnProperty.ts diff --git a/packages/e2e/app/src/pages/Cards/Cards.js b/packages/e2e/app/src/pages/Cards/Cards.js index 3490535394..c3b687904d 100644 --- a/packages/e2e/app/src/pages/Cards/Cards.js +++ b/packages/e2e/app/src/pages/Cards/Cards.js @@ -22,7 +22,24 @@ const initCheckout = async () => { window.card = checkout .create('card', { brands: ['mc', 'visa', 'amex', 'maestro', 'bcmc'], - onChange: state => console.log(state), + onChange: state => { + /** + * Needed now that, for v5, we enhance the securedFields state.errors object with a rootNode prop + * - Testcafe doesn't like a ClientFunction retrieving an object with a DOM node in it!? + * + * AND, for some reason, if you place this onChange function in expiryDate.clientScripts.js it doesn't always get read. + * It'll work when it's part of a small batch but if part of the full test suite it gets ignored - so the tests that rely on + * window.mappedStateErrors fail + */ + if (!!Object.keys(state.errors).length) { + // Replace any rootNode values in the objects in state.errors with an empty string + const nuErrors = Object.entries(state.errors).reduce((acc, [fieldType, error]) => { + acc[fieldType] = error ? { ...error, rootNode: '' } : error; + return acc; + }, {}); + window.mappedStateErrors = nuErrors; + } + }, ...window.cardConfig }) .mount('.card-field'); diff --git a/packages/e2e/app/src/pages/CustomCards/CustomCards.html b/packages/e2e/app/src/pages/CustomCards/CustomCards.html index e17c4414ca..145529c41c 100644 --- a/packages/e2e/app/src/pages/CustomCards/CustomCards.html +++ b/packages/e2e/app/src/pages/CustomCards/CustomCards.html @@ -20,7 +20,7 @@

CustomCard #1

cardCustomCard #1 -