Skip to content

Commit

Permalink
tidying up "partial" tests: cleaning up callbacks, using constants
Browse files Browse the repository at this point in the history
  • Loading branch information
sponglord committed Dec 14, 2023
1 parent 4a910c2 commit 1ba5469
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 109 deletions.
Original file line number Diff line number Diff line change
@@ -1,43 +1,35 @@
import { handleFocus } from './handleFocus';
import ua from '../utils/userAgent';
import { ENCRYPTED_CARD_NUMBER, ENCRYPTED_SECURITY_CODE } from '../../configuration/constants';

ua.__IS_IOS = true;
ua.__IS_IOS = false;

const csfState = { type: 'card', currentFocusObject: null, registerFieldForIos: false };
const csfProps = { rootNode: 'div' };
const csfConfig = {};
const csfCallbacks = { onFocus: null };

const CSFObj = {
csfState,
csfProps,
csfConfig,
csfCallbacks
};

const feedbackObj = {
action: 'focus',
focus: true,
numChars: 0,
fieldType: 'encryptedCardNumber',
fieldType: ENCRYPTED_CARD_NUMBER,
numKey: 896044601
};

let onFocusCallbackObj = null;

csfCallbacks.onFocus = jest.fn(callbackObj => {
onFocusCallbackObj = callbackObj;
console.log('### handleFocus.test::onFocus callback called:: callbackObj', callbackObj);
});

const expectedCallbackObj = {
action: 'focus',
focus: true,
numChars: 0,
fieldType: 'encryptedCardNumber',
fieldType: ENCRYPTED_CARD_NUMBER,
rootNode: 'div',
type: 'card',
currentFocusObject: 'encryptedCardNumber'
currentFocusObject: ENCRYPTED_CARD_NUMBER
};

const handleIOSTouchEvents = jest.fn(() => {});
Expand All @@ -51,21 +43,49 @@ describe('Testing CSFs handleFocus functionality', () => {
beforeEach(() => {
console.log = jest.fn(() => {});

onFocusCallbackObj = null;
csfCallbacks.onFocus = jest.fn(() => {});
});

test('handleFocus should simulate focus event on PAN, when not in iOS scenario', () => {
callOnFocus();

expect(csfState.currentFocusObject).toEqual(ENCRYPTED_CARD_NUMBER);

expect(handleIOSTouchEvents).not.toHaveBeenCalled(); // not iOS

expect(csfCallbacks.onFocus).toHaveBeenCalledTimes(1);
expect(csfCallbacks.onFocus).toHaveBeenCalledWith(expectedCallbackObj);
});

test('handleFocus should simulate focus event', () => {
test('handleFocus should see that repeating focus event on PAN, in iOS scenario, does not see handleIOSTouchEvents, since currentFocusObject has not changed', () => {
ua.__IS_IOS = true;
callOnFocus();

expect(csfState.currentFocusObject).toEqual('encryptedCardNumber');
expect(csfState.currentFocusObject).toEqual(ENCRYPTED_CARD_NUMBER);

expect(handleIOSTouchEvents).toHaveBeenCalled();
expect(handleIOSTouchEvents).not.toHaveBeenCalled();

expect(csfCallbacks.onFocus).toHaveBeenCalledTimes(1);
expect(onFocusCallbackObj).toEqual(expectedCallbackObj);
expect(csfCallbacks.onFocus).toHaveBeenCalledWith(expectedCallbackObj);
});

test('handleFocus should simulate blur event', () => {
test('handleFocus should simulate focus moving to CVC, in iOS scenario', () => {
ua.__IS_IOS = true;
feedbackObj.fieldType = ENCRYPTED_SECURITY_CODE;
expectedCallbackObj.fieldType = ENCRYPTED_SECURITY_CODE;
expectedCallbackObj.currentFocusObject = ENCRYPTED_SECURITY_CODE;

callOnFocus();

expect(csfState.currentFocusObject).toEqual(ENCRYPTED_SECURITY_CODE);

expect(handleIOSTouchEvents).toHaveBeenCalled(); // iOS scenario

expect(csfCallbacks.onFocus).toHaveBeenCalledTimes(1);
expect(csfCallbacks.onFocus).toHaveBeenCalledWith(expectedCallbackObj);
});

test('handleFocus should simulate blur event (on CVC field)', () => {
feedbackObj.focus = false;
expectedCallbackObj.currentFocusObject = null;
expectedCallbackObj.focus = false;
Expand All @@ -74,7 +94,7 @@ describe('Testing CSFs handleFocus functionality', () => {

expect(csfState.currentFocusObject).toEqual(null);

expect(csfCallbacks.onFocus).toHaveBeenCalledTimes(2);
expect(onFocusCallbackObj).toEqual(expectedCallbackObj);
expect(csfCallbacks.onFocus).toHaveBeenCalledTimes(1);
expect(csfCallbacks.onFocus).toHaveBeenCalledWith(expectedCallbackObj);
});
});
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
import { handleIframeConfigFeedback } from './handleIframeConfigFeedback';
import { ENCRYPTED_CARD_NUMBER, ENCRYPTED_PWD_FIELD } from '../../configuration/constants';

const csfState = { type: 'card', isConfigured: false, originalNumIframes: 3, iframeConfigCount: 0 };
const csfProps = {};
const csfConfig = {};
const csfCallbacks = { onAdditionalSFConfig: null };

const CSFObj = {
csfState,
csfProps,
csfConfig,
csfCallbacks
};

const feedbackObj = {
action: 'config',
fieldType: 'encryptedCardNumber',
fieldType: ENCRYPTED_CARD_NUMBER,
numKey: 1244301008
};

let onAdditionalSFConfigCallbackObj = null;

csfCallbacks.onAdditionalSFConfig = jest.fn(callbackObj => {
onAdditionalSFConfigCallbackObj = callbackObj;
console.log('### handleIframeConfigFeedback.test::onAdditionalSFConfig callback called:: callbackObj', callbackObj);
});

const isConfigured = jest.fn(() => console.log('### handleIframeConfigFeedback.test:::: isConfigured is called'));
let isConfigured;

const callHandleIframeConfigFeedback = () => {
// @ts-ignore - test is faking setup object
Expand All @@ -35,6 +25,10 @@ const callHandleIframeConfigFeedback = () => {
describe('Testing CSFs handleIframeConfigFeedback functionality', () => {
beforeEach(() => {
console.log = jest.fn(() => {});

isConfigured = jest.fn(() => {});

csfCallbacks.onAdditionalSFConfig = jest.fn(() => {});
});

test('isConfigured should not be called since the iframe count is still below the total', () => {
Expand All @@ -50,24 +44,24 @@ describe('Testing CSFs handleIframeConfigFeedback functionality', () => {

const res = callHandleIframeConfigFeedback();

expect(isConfigured).toHaveBeenCalled();
expect(isConfigured).toHaveBeenCalledTimes(1);

expect(res).toEqual(true);
});

test('isConfigured should be not called again since we are now mocking an additional securedField being added i.e. KCP scenario', () => {
csfState.isConfigured = true;
feedbackObj.fieldType = 'encryptedPassword';
feedbackObj.fieldType = ENCRYPTED_PWD_FIELD;

const res = callHandleIframeConfigFeedback();

// no new call to isConfigured
expect(isConfigured).toHaveBeenCalledTimes(1);
expect(isConfigured).not.toHaveBeenCalled();

expect(csfCallbacks.onAdditionalSFConfig).toHaveBeenCalledTimes(1);
expect(onAdditionalSFConfigCallbackObj).toEqual({
expect(csfCallbacks.onAdditionalSFConfig).toHaveBeenCalledWith({
additionalIframeConfigured: true,
fieldType: 'encryptedPassword',
fieldType: ENCRYPTED_PWD_FIELD,
type: 'card'
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,9 @@ const CSFObj = {
csfCallbacks
};

let onConfigSuccessCallbackObj = null;

let consoleError = null;

csfCallbacks.onConfigSuccess = jest.fn(callbackObj => {
onConfigSuccessCallbackObj = callbackObj;
});

const validateForm = jest.fn(() => console.log('### isConfigured.test:::: validateForm is called'));
let validateForm;

const callIsConfigured = () => {
// @ts-ignore - test is faking setup object
Expand All @@ -37,14 +31,17 @@ describe('Testing CSFs isConfigured functionality', () => {
consoleError = error;
});

onConfigSuccessCallbackObj = null;
validateForm = jest.fn(() => {});

csfCallbacks.onConfigSuccess = jest.fn(() => {});
});

test('onConfigSuccess callback should be called since this is the purpose of this function, but validateForm is not called since we are dealing with 3 securedFields', () => {
test('onConfigSuccess callback should be called since this is the purpose of this function, but validateForm is not called since we are dealing with 3 securedFields & not a recurringCard', () => {
const res = callIsConfigured();

expect(csfCallbacks.onConfigSuccess).toHaveBeenCalledTimes(1);
expect(onConfigSuccessCallbackObj).toEqual({ iframesConfigured: true, type: 'card', rootNode: 'div' });
expect(csfCallbacks.onConfigSuccess).toHaveBeenCalledWith({ iframesConfigured: true, type: 'card', rootNode: 'div' });
// expect(onConfigSuccessCallbackObj).toEqual({ iframesConfigured: true, type: 'card', rootNode: 'div' });

expect(validateForm).not.toHaveBeenCalled();

Expand All @@ -55,8 +52,8 @@ describe('Testing CSFs isConfigured functionality', () => {
csfState.numIframes = 1;
const res = callIsConfigured();

expect(csfCallbacks.onConfigSuccess).toHaveBeenCalledTimes(2);
expect(onConfigSuccessCallbackObj).toEqual({ iframesConfigured: true, type: 'card', rootNode: 'div' });
expect(csfCallbacks.onConfigSuccess).toHaveBeenCalledTimes(1);
expect(csfCallbacks.onConfigSuccess).toHaveBeenCalledWith({ iframesConfigured: true, type: 'card', rootNode: 'div' });

expect(validateForm).not.toHaveBeenCalled();

Expand All @@ -68,20 +65,20 @@ describe('Testing CSFs isConfigured functionality', () => {
csfState.type = 'visa';
const res = callIsConfigured();

expect(csfCallbacks.onConfigSuccess).toHaveBeenCalledTimes(3);
expect(onConfigSuccessCallbackObj).toEqual({ iframesConfigured: true, type: 'visa', rootNode: 'div' });
expect(csfCallbacks.onConfigSuccess).toHaveBeenCalledTimes(1);
expect(csfCallbacks.onConfigSuccess).toHaveBeenCalledWith({ iframesConfigured: true, type: 'visa', rootNode: 'div' });

expect(validateForm).not.toHaveBeenCalled();

expect(res).toEqual(true);
});

test('validateForm should not be called since we are dealing with a recurring card of an unknown txvariant', () => {
test("validateForm should not be called since we are dealing with a recurring card of an unknown txvariant so we don't know what it's cvcPolicy is", () => {
csfState.type = 'visa_tokenised_subvariant';
const res = callIsConfigured();

expect(csfCallbacks.onConfigSuccess).toHaveBeenCalledTimes(4);
expect(onConfigSuccessCallbackObj).toEqual({ iframesConfigured: true, type: 'visa_tokenised_subvariant', rootNode: 'div' });
expect(csfCallbacks.onConfigSuccess).toHaveBeenCalledTimes(1);
expect(csfCallbacks.onConfigSuccess).toHaveBeenCalledWith({ iframesConfigured: true, type: 'visa_tokenised_subvariant', rootNode: 'div' });

expect(validateForm).not.toHaveBeenCalled();

Expand All @@ -92,8 +89,8 @@ describe('Testing CSFs isConfigured functionality', () => {
csfState.type = 'laser';
const res = callIsConfigured();

expect(csfCallbacks.onConfigSuccess).toHaveBeenCalledTimes(5);
expect(onConfigSuccessCallbackObj).toEqual({ iframesConfigured: true, type: 'laser', rootNode: 'div' });
expect(csfCallbacks.onConfigSuccess).toHaveBeenCalledTimes(1);
expect(csfCallbacks.onConfigSuccess).toHaveBeenCalledWith({ iframesConfigured: true, type: 'laser', rootNode: 'div' });

expect(validateForm).toHaveBeenCalled();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import postMessageToIframe from '../utils/iframes/postMessageToIframe';
import { postMessageToAllIframes } from './postMessageToAllIframes';
import { ENCRYPTED_CARD_NUMBER, ENCRYPTED_EXPIRY_DATE, ENCRYPTED_SECURITY_CODE } from '../../configuration/constants';

jest.mock('../utils/iframes/postMessageToIframe');

const mockedPostMessageToIframe = postMessageToIframe as jest.Mock;

const csfState = {
// brand: { brand: null, cvcPolicy: 'required' },
type: 'card',
securedFields: { encryptedCardNumber: { numKey: 654321 }, encryptedExpiryDate: { numKey: 654321 }, encryptedSecurityCode: { numKey: 654321 } }
};
Expand All @@ -23,7 +23,7 @@ const dataObj = {

const expectedPostMsgDataObj = {
txVariant: 'card',
fieldType: 'encryptedCardNumber',
fieldType: ENCRYPTED_CARD_NUMBER,
numKey: 654321,
destroy: true
};
Expand Down Expand Up @@ -52,17 +52,17 @@ describe('Testing postMessageToAllIframes fny', () => {
expect(res).toEqual(false);
});

test('Calling postMessageToAllIframes with no data object will not lead to any calls to postMessageToIframe', () => {
test('Calling postMessageToAllIframes with a data object will lead to calls to postMessageToIframe for all existing securedFields', () => {
const res = callPostMessageToAllIframes(dataObj);

expect(postMessageToIframeMock).toHaveBeenCalledTimes(3);
expect(postMessageToIframeMock).toHaveBeenCalledWith(expectedPostMsgDataObj);
expect(postMessageToIframeMock).toHaveBeenCalledWith(expectedPostMsgDataObj); // PAN SF

expectedPostMsgDataObj.fieldType = 'encryptedExpiryDate';
expect(postMessageToIframeMock).toHaveBeenCalledWith(expectedPostMsgDataObj);
expectedPostMsgDataObj.fieldType = ENCRYPTED_EXPIRY_DATE;
expect(postMessageToIframeMock).toHaveBeenCalledWith(expectedPostMsgDataObj); // Date SF

expectedPostMsgDataObj.fieldType = 'encryptedSecurityCode';
expect(postMessageToIframeMock).toHaveBeenCalledWith(expectedPostMsgDataObj);
expectedPostMsgDataObj.fieldType = ENCRYPTED_SECURITY_CODE;
expect(postMessageToIframeMock).toHaveBeenCalledWith(expectedPostMsgDataObj); // CVC SF

expect(res).toEqual(true);
});
Expand Down
Loading

0 comments on commit 1ba5469

Please sign in to comment.