Skip to content

Commit

Permalink
add tests and todos, break fns down a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
cgdibble committed Nov 18, 2023
1 parent 283d645 commit b4984a3
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 18 deletions.
51 changes: 49 additions & 2 deletions src/data-collector.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,57 @@
/* @flow */

// import { ENV } from '@paypal/sdk-constants/src';

export const FRAUDNET_FNCLS = "fnparams-dede7cc5-15fd-4c75-a9f4-36c430ee3a99";
export const FRAUDNET_APP_NAME = "SMART_PAYMENT_BUTTONS";

export const loadDataCollector = async (options) => {
// const FRAUDNET_URL = {
// [ENV.LOCAL]: "https://www.msmaster.qa.paypal.com/en_US/m/fb-raw.js",
// [ENV.STAGE]: "https://stage2mb044.qa.paypal.com/fraudnetjsnodeweb/automate/develop/stage_raw.js",
// [ENV.SANDBOX]: "https://c.paypal.com/da/r/fb.js",
// [ENV.PRODUCTION]: "https://c.paypal.com/da/r/fb.js",
// [ENV.TEST]: "https://c.paypal.com/da/r/fb.js",
// };

export const loadDataCollector = async ({
cspNonce = "",
clientMetadataID,
env,
}) => {

Check failure on line 20 in src/data-collector.js

View workflow job for this annotation

GitHub Actions / main

Async arrow function has no 'await' expression
// TODO: Ensure these functions return zalgo promises accordingly. reference fraudnet.js in SPB for pattern
createConfigScript({ cspNonce, clientMetadataID });

Check failure on line 22 in src/data-collector.js

View workflow job for this annotation

GitHub Actions / main

'createConfigScript' was used before it was defined
createFraudnetScript({ cspNonce, env });

Check failure on line 23 in src/data-collector.js

View workflow job for this annotation

GitHub Actions / main

'createFraudnetScript' was used before it was defined

// TODO: test and implement the window.fallback/timeout logic (see fraudnet.js in SPB)
};

export const createConfigScript = ({ cspNonce = "", clientMetadataID }) => {
const fraudnetConfig = {
f: clientMetadataID,
s: FRAUDNET_APP_NAME,
cb1: "fnCallback",
};

const configScript = document.createElement("script");
configScript.setAttribute("nonce", options.cspNonce);

configScript.setAttribute("nonce", cspNonce);
configScript.setAttribute("type", "application/json");
configScript.setAttribute("id", "fconfig");
configScript.setAttribute("fncls", FRAUDNET_FNCLS);
configScript.textContent = JSON.stringify(fraudnetConfig);

document.body.appendChild(configScript);

Check failure on line 43 in src/data-collector.js

View workflow job for this annotation

GitHub Actions / main

document.body() is not supported in Firefox 30
};

export const createFraudnetScript = ({ cspNonce, env }) => {

Check failure on line 46 in src/data-collector.js

View workflow job for this annotation

GitHub Actions / main

'env' is defined but never used
const fraudnetScript = document.createElement("script");

// const fraudnetUrl = FRAUDNET_URL[env]
fraudnetScript.setAttribute("nonce", cspNonce);
// fraudnetScript.setAttribute('src', fraudnetUrl)
fraudnetScript.addEventListener("error", () => {
/* We'll want to resolve here Zalgo style */
});

document.body.appendChild(fraudnetScript);

Check failure on line 56 in src/data-collector.js

View workflow job for this annotation

GitHub Actions / main

document.body() is not supported in Firefox 30
};
74 changes: 58 additions & 16 deletions test/server/data-collector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,86 @@
*/
import {

Check failure on line 4 in test/server/data-collector.test.js

View workflow job for this annotation

GitHub Actions / main

Flow file annotation is missing
loadDataCollector,
createConfigScript,
createFraudnetScript,
FRAUDNET_FNCLS,
FRAUDNET_APP_NAME,
} from "../../src/data-collector";

describe.only("data-collector.js", () => {
test("it create the config element with correct inputs", async () => {
beforeEach(() => {
jest.clearAllMocks();

Check failure on line 14 in test/server/data-collector.test.js

View workflow job for this annotation

GitHub Actions / main

'jest' is not defined
});

const testInputs = {
clientMetadataID: "some-cmid",
fraudnetAppName: "spb-test-name",
env: "unit-tests",
cspNonce: "not-sure-what-this-is-yet-csp-nonce",
queryStringParams: {
/* TBD */
},
};

test("creates both scripts", async () => {
const mockAppendChild = jest.fn();
document.body.appendChild = mockAppendChild;

await loadDataCollector(testInputs);

expect(mockAppendChild).toHaveBeenCalledTimes(2);
});

test("it create and append the config element", async () => {
const mockSetAttribute = jest.fn();
const mockAppendChild = jest.fn();
const mockReturnedElement = {
setAttribute: mockSetAttribute,
};
document.createElement = jest.fn(() => {
return mockReturnedElement;
});
const inputs = {
clientMetadataId: "some-cmid",
fraudnetAppName: "spb-test-name",
env: "unit-tests",
cspNonce: "not-sure-what-this-is-yet-csp-nonce",
queryStringParams: {
/* TBD */
},
};
document.body.appendChild = mockAppendChild;

const expectedScriptConfig = {
f: inputs.clientMetadataId,
f: testInputs.clientMetadataID,
s: FRAUDNET_APP_NAME,
//u: <NOT SURE THIS IS RELEVANT! Does it currently default in spb?>
cb1: "fnCallback",
};
await loadDataCollector(inputs);
// assert script created with certain attributes
expect(document.createElement).toBeCalledWith("script");
expect(mockSetAttribute).toBeCalledWith("nonce", inputs.cspNonce);
await createConfigScript(testInputs);

expect(document.createElement).toHaveBeenNthCalledWith(1, "script");
expect(mockSetAttribute).toBeCalledWith("nonce", testInputs.cspNonce);
expect(mockSetAttribute).toBeCalledWith("type", "application/json");
expect(mockSetAttribute).toBeCalledWith("id", "fconfig");
expect(mockSetAttribute).toBeCalledWith("fncls", FRAUDNET_FNCLS);
expect(mockReturnedElement.textContent).toEqual(
JSON.stringify({ expectedScriptConfig })
JSON.stringify(expectedScriptConfig)
);
expect(mockAppendChild).toBeCalledWith(mockReturnedElement);
});

test("creates fraudnet script with config", async () => {
const mockAppendChild = jest.fn();
const mockListener = jest.fn();
const mockSetAttribute = jest.fn();
const mockReturnedElement = {
setAttribute: mockSetAttribute,
addEventListener: mockListener,
};
document.body.appendChild = mockAppendChild;

document.createElement = jest.fn(() => {
return mockReturnedElement;
});

await createFraudnetScript(testInputs);

expect(document.createElement).toHaveBeenCalledWith("script");
expect(mockSetAttribute).toHaveBeenCalledWith("nonce", testInputs.cspNonce);
// expect(mockSetAttribute).toHaveBeenCalledWith("src", expect.stringContaining("fb.js"))
expect(mockListener).toHaveBeenCalledWith("error", expect.any(Function));
expect(mockAppendChild).toBeCalledWith(mockReturnedElement);
});
});

0 comments on commit b4984a3

Please sign in to comment.