Skip to content

Commit

Permalink
Merge pull request #161 from DhanaSekharM/gov-client-window-size
Browse files Browse the repository at this point in the history
feat: export window size details header for HMRC
  • Loading branch information
reubenae committed Oct 27, 2021
2 parents 3149341 + d4f2e45 commit 4cf8380
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
6 changes: 6 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@ const {headerValue, error} = getGovClientTimezoneHeader();
import getGovClientLocalIPsHeader from 'user-data-for-fraud-prevention';
const {headerValue, error} = await getGovClientLocalIPsHeader();
```

* To get Gov-Client-Window-Size HMRC Fraud prevention header:
```js
import {getGovClientWindowSizeHeader} from 'user-data-for-fraud-prevention';
const {headerValue, error} = getGovClientWindowSizeHeader();
```
15 changes: 14 additions & 1 deletion src/js/hmrc/mtdFraudPrevention.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,17 @@ export const getGovClientLocalIPsHeader = async () => {
} catch (error) {
return { error };
}
}
}

/**
* Returns the value for Gov-Client-Window-Size HMRC Fraud prevention header.
*/
export const getGovClientWindowSizeHeader = () => {
try {
return { headerValue: getWindowSize() };
} catch (error) {
return { error };
}
};


3 changes: 3 additions & 0 deletions src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getGovClientDeviceID,
getGovClientTimezoneHeader,
getGovClientLocalIPsHeader,
getGovClientWindowSizeHeader
} from "./hmrc/mtdFraudPrevention";

exports.fraudPreventionHeadersEnum = fraudPreventionHeadersEnum;
Expand All @@ -22,4 +23,6 @@ exports.getGovClientTimezoneHeader = getGovClientTimezoneHeader;
exports.getGovClientLocalIPsHeader = getGovClientLocalIPsHeader;
exports.getScreenDetails = getScreenDetails;
exports.getGovClientBrowserJSUserAgentHeader = getGovClientBrowserJSUserAgentHeader;

exports.windowDetails = windowDetails;
exports.getGovClientWindowSizeHeader = getGovClientWindowSizeHeader;
42 changes: 42 additions & 0 deletions tests/unit/hmrc/mtdFraudPrevention.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {getGovClientBrowserPluginsHeader} from "../../../src/js/hmrc/mtdFraudPre
import {getGovClientDeviceID} from "../../../src/js/hmrc/mtdFraudPrevention";
import {getGovClientBrowserDoNotTrackHeader} from "../../../src/js/hmrc/mtdFraudPrevention";
import {getGovClientTimezoneHeader} from "../../../src/js/hmrc/mtdFraudPrevention";
import {getGovClientWindowSizeHeader} from "../../../src/js/hmrc/mtdFraudPrevention"

describe("FraudPreventionHeaders", () => {
resetDeviceIpString();
Expand Down Expand Up @@ -330,3 +331,44 @@ describe("getGovClientLocalIPsHeader", () => {
deviceLocalIpMock.mockRestore();
});
});

describe("getGovClientWindowSizeHeader", () => {
it("returns correct headerValue when there is no error", () => {
const windowSpy = jest
.spyOn(window, "window", "get")
.mockImplementation(() => ({
innerWidth: 100,
innerHeight: 100,
}));

const { headerValue } = getGovClientWindowSizeHeader();
expect(headerValue).toBe("width=100&height=100");
windowSpy.mockRestore();
});

it("returns error when there is an error in window width", () => {
const widthSpy = jest
.spyOn(browserInfoHelper, "getWindowWidth")
.mockImplementation(() => {
throw Error("Something went wrong.");
});
const { error } = getGovClientWindowSizeHeader();
expect(error).toEqual(Error("Something went wrong."));
widthSpy.mockRestore();
});

it("returns error when there is an error in window height", () => {
const heightSpy = jest
.spyOn(browserInfoHelper, "getWindowHeight")
.mockImplementation(() => {
throw Error("Something went wrong.");
});
const { error } = getGovClientWindowSizeHeader();
expect(error).toEqual(Error("Something went wrong."));
heightSpy.mockRestore();
});
});




0 comments on commit 4cf8380

Please sign in to comment.