Skip to content

Commit

Permalink
Merge pull request #6 from reubenae/remove-moment
Browse files Browse the repository at this point in the history
removed moment as a dependency
  • Loading branch information
skodamarthi committed Oct 1, 2020
2 parents 3a198ca + 236d716 commit 4919173
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 23 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
},
"dependencies": {
"@babel/preset-env": "^7.10.4",
"moment": "^2.24.0",
"uuid": "^3.3.3"
},
"devDependencies": {
Expand Down
14 changes: 12 additions & 2 deletions src/js/common/browserInfoHelper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import moment from "moment";
import globalsUtil from "./globalsUtil";

const ICE_CANDIDATE_IP_INDEX = 4;
Expand Down Expand Up @@ -68,7 +67,18 @@ const validateAndGetScreenDetail = (value) => {
}
};

export const getTimezone = () => `UTC${moment().format("Z")}`;
const getFormattedOffset = () => {
// Date().toString() is in format like "Wed Sep 30 2020 23:11:02 GMT+0100 (British Summer Time)"
// To format the offset, we split on "GMT"
// and then pick the relevant characters based on their position and reformat with a ":"
const offset = new Date().toString().split("GMT")[1];
const hourOffset = `${offset[0]}${offset[1]}${offset[2]}`;
const minuteOffset = `${offset[3]}${offset[4]}`;
const formattedUTC = `${hourOffset}:${minuteOffset}`;
return formattedUTC;
}

export const getTimezone = () => `UTC${getFormattedOffset()}`;
export const getScreenWidth = () =>
validateAndGetScreenDetail(globalsUtil.getScreen().width);
export const getScreenHeight = () =>
Expand Down
13 changes: 10 additions & 3 deletions tests/unit/common/browserInfoHelper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
getTimezone,
resetDeviceIpString,
} from "../../../src/js/common/browserInfoHelper";
import moment from "moment";

describe("BrowserInfoHelper", () => {
afterEach(() => {
Expand Down Expand Up @@ -198,7 +197,7 @@ describe("BrowserInfoHelper", () => {
it("RTCPeerConnection valid", async () => {
webRtcConnectionStub.mockReturnValue(MockRTCPeerConnection);
expect(await getDeviceLocalIPAsString()).toEqual("127.0.0.1");
//Calling the function to return an already calculated deviceIpString
// Calling the function to return an already calculated deviceIpString
expect(await getDeviceLocalIPAsString()).toEqual("127.0.0.1");
});

Expand Down Expand Up @@ -227,7 +226,15 @@ describe("BrowserInfoHelper", () => {
height: 1021,
colorDepth: 17,
});
expect(getTimezone()).toEqual(`UTC${moment().format("Z")}`);
global.Date = class DateMock {
constructor() {
}
toString() {
return "Tue May 14 2019 12:01:58 GMT+0100 (British Summer Time)";
}
};

expect(getTimezone()).toEqual(`UTC+01:00`);
expect(getScreenWidth()).toEqual(1019);
expect(getScreenHeight()).toEqual(1021);
expect(getScreenScalingFactor()).toEqual(2);
Expand Down
16 changes: 9 additions & 7 deletions tests/unit/hmrc/mtdFraudPrevention.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
import globalsUtil from "../../../src/js/common/globalsUtil";
import * as browserInfoHelper from "../../../src/js/common/browserInfoHelper";
import { resetDeviceIpString } from "../../../src/js/common/browserInfoHelper";
import moment from "moment";

describe("FraudPreventionHeaders", () => {
resetDeviceIpString();
Expand Down Expand Up @@ -36,13 +35,18 @@ describe("FraudPreventionHeaders", () => {
height: 1021,
colorDepth: 17,
});
global.Date = class DateMock {
constructor() {
}
toString() {
return "Tue May 14 2019 12:01:58 GMT+0100 (British Summer Time)";
}
};

const {headers, errors} = await getFraudPreventionHeaders();
expect(headers.size).toBe(6);
expect(errors.length).toBe(0);
expect(headers.get("Gov-Client-Timezone")).toBe(
`UTC${moment().format("Z")}`
);
expect(headers.get("Gov-Client-Timezone")).toBe(`UTC+01:00`);
expect(headers.get("Gov-Client-Screens")).toBe(
"width=1019&height=1021&scaling-factor=2&colour-depth=17"
);
Expand Down Expand Up @@ -77,9 +81,7 @@ describe("FraudPreventionHeaders", () => {
const {headers, errors} = await getFraudPreventionHeaders();
expect(headers.size).toBe(5);
expect(errors.length).toBe(1);
expect(headers.get("Gov-Client-Timezone")).toBe(
`UTC${moment().format("Z")}`
);
expect(headers.get("Gov-Client-Timezone")).toBe(`UTC+01:00`);
expect(headers.get("Gov-Client-Screens")).toBe(
"width=1019&height=1021&scaling-factor=2&colour-depth=17"
);
Expand Down
10 changes: 0 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1990,11 +1990,6 @@ arr-union@^3.1.0:
resolved "https://registry.npmjs.intuit.com:443/artifactory/api/npm/npm-intuit/arr-union/-/arr-union-3.1.0.tgz#e39b09aea9def866a8f206e288af63919bae39c4"
integrity sha1-45sJrqne+Gao8gbiiK9jkZuuOcQ=

array-from@^2.1.1:
version "2.1.1"
resolved "https://registry.npmjs.intuit.com:443/artifactory/api/npm/npm-intuit/array-from/-/array-from-2.1.1.tgz#cfe9d8c26628b9dc5aecc62a9f5d8f1f352c1195"
integrity sha1-z+nYwmYoudxa7MYqn12PHzUsEZU=

array-ify@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/array-ify/-/array-ify-1.0.0.tgz#9e528762b4a9066ad163a6962a364418e9626ece"
Expand Down Expand Up @@ -5592,11 +5587,6 @@ mkdirp@^0.5.1, mkdirp@^0.5.3:
dependencies:
minimist "^1.2.5"

moment@^2.24.0:
version "2.26.0"
resolved "https://registry.npmjs.intuit.com:443/artifactory/api/npm/npm-intuit/moment/-/moment-2.26.0.tgz#5e1f82c6bafca6e83e808b30c8705eed0dcbd39a"
integrity sha1-Xh+Cxrr8pug+gIswyHBe7Q3L05o=

move-concurrently@^1.0.1:
version "1.0.1"
resolved "https://registry.npmjs.intuit.com:443/artifactory/api/npm/npm-intuit/move-concurrently/-/move-concurrently-1.0.1.tgz#be2c005fda32e0b29af1f05d7c4b33214c701f92"
Expand Down

0 comments on commit 4919173

Please sign in to comment.