-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
started test driving data collector util
- Loading branch information
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / main
|
||
testEnvironment: "jsdom", | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export const FRAUDNET_FNCLS = "fnparams-dede7cc5-15fd-4c75-a9f4-36c430ee3a99"; | ||
export const FRAUDNET_APP_NAME = "SMART_PAYMENT_BUTTONS"; | ||
|
||
export const loadDataCollector = async (options) => { | ||
const configScript = document.createElement("script"); | ||
configScript.setAttribute("nonce", options.cspNonce); | ||
configScript.setAttribute("type", "application/json"); | ||
configScript.setAttribute("id", "fconfig"); | ||
configScript.setAttribute("fncls", FRAUDNET_FNCLS); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
import { | ||
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(); | ||
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 */ | ||
}, | ||
}; | ||
|
||
const expectedScriptConfig = { | ||
f: inputs.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); | ||
expect(mockSetAttribute).toBeCalledWith("type", "application/json"); | ||
expect(mockSetAttribute).toBeCalledWith("id", "fconfig"); | ||
expect(mockSetAttribute).toBeCalledWith("fncls", FRAUDNET_FNCLS); | ||
expect(mockReturnedElement.textContent).toEqual( | ||
JSON.stringify({ expectedScriptConfig }) | ||
); | ||
}); | ||
}); |