Skip to content

Commit

Permalink
started test driving data collector util
Browse files Browse the repository at this point in the history
  • Loading branch information
cgdibble committed Nov 17, 2023
1 parent 088c9c9 commit 283d645
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {

Check failure on line 1 in jest.config.js

View workflow job for this annotation

GitHub Actions / main

Flow file annotation is missing

Check failure on line 1 in jest.config.js

View workflow job for this annotation

GitHub Actions / main

Expected "export" or "export default"
testEnvironment: "jsdom",
};
10 changes: 10 additions & 0 deletions src/data-collector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const FRAUDNET_FNCLS = "fnparams-dede7cc5-15fd-4c75-a9f4-36c430ee3a99";

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

View workflow job for this annotation

GitHub Actions / main

Flow file annotation is missing
export const FRAUDNET_APP_NAME = "SMART_PAYMENT_BUTTONS";

export const loadDataCollector = async (options) => {

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

View workflow job for this annotation

GitHub Actions / main

Async arrow function has no 'await' expression
const configScript = document.createElement("script");
configScript.setAttribute("nonce", options.cspNonce);
configScript.setAttribute("type", "application/json");
configScript.setAttribute("id", "fconfig");
configScript.setAttribute("fncls", FRAUDNET_FNCLS);
};
46 changes: 46 additions & 0 deletions test/server/data-collector.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @jest-environment jsdom
*/
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,
FRAUDNET_FNCLS,
FRAUDNET_APP_NAME,
} from "../../src/data-collector";

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

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

View workflow job for this annotation

GitHub Actions / main

'jest' is not defined
const mockReturnedElement = {
setAttribute: mockSetAttribute,
};
document.createElement = jest.fn(() => {

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

View workflow job for this annotation

GitHub Actions / main

'jest' is not defined
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 */
},
};

const expectedScriptConfig = {
f: inputs.clientMetadataId,
s: FRAUDNET_APP_NAME,
//u: <NOT SURE THIS IS RELEVANT! Does it currently default in spb?>

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

View workflow job for this annotation

GitHub Actions / main

Expected space or tab after '//' in comment
cb1: "fnCallback",
};
await loadDataCollector(inputs);
// assert script created with certain attributes
expect(document.createElement).toBeCalledWith("script");

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

View workflow job for this annotation

GitHub Actions / main

'expect' is not defined
expect(mockSetAttribute).toBeCalledWith("nonce", inputs.cspNonce);

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

View workflow job for this annotation

GitHub Actions / main

'expect' is not defined
expect(mockSetAttribute).toBeCalledWith("type", "application/json");
expect(mockSetAttribute).toBeCalledWith("id", "fconfig");
expect(mockSetAttribute).toBeCalledWith("fncls", FRAUDNET_FNCLS);
expect(mockReturnedElement.textContent).toEqual(
JSON.stringify({ expectedScriptConfig })
);
});
});

0 comments on commit 283d645

Please sign in to comment.