From f20cf6743bd01b2ba6e160e40df28821fbad96f2 Mon Sep 17 00:00:00 2001 From: Mayank Bansal Date: Sat, 11 Jan 2025 16:49:36 +0530 Subject: [PATCH] add unit test for dateConversion, redirectAuth utils --- tests/unit/utils/date-conversion-test.js | 50 ++++++++++++++ tests/unit/utils/redirect-auth-test.js | 88 ++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 tests/unit/utils/date-conversion-test.js create mode 100644 tests/unit/utils/redirect-auth-test.js diff --git a/tests/unit/utils/date-conversion-test.js b/tests/unit/utils/date-conversion-test.js new file mode 100644 index 00000000..b2020377 --- /dev/null +++ b/tests/unit/utils/date-conversion-test.js @@ -0,0 +1,50 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'website-www/tests/helpers'; +import { getUTCMidnightTimestampFromDate } from 'website-www/utils/date-conversion'; + +module('Unit | Utils | dateConversion', function (hooks) { + setupTest(hooks); + + test('it returns the correct UTC midnight timestamp for a date string', function (assert) { + const result = getUTCMidnightTimestampFromDate('2025-01-10'); + assert.strictEqual( + result, + 1736467200000, + 'Correct timestamp for 2025-01-10', + ); + }); + + test('it works correctly for the Unix Epoch start date', function (assert) { + const result = getUTCMidnightTimestampFromDate('1970-01-01'); + assert.strictEqual( + result, + 0, + 'Correct timestamp for Unix Epoch start date 1970-01-01', + ); + }); + + test('it handles leap years correctly', function (assert) { + const result = getUTCMidnightTimestampFromDate('2024-02-29'); + assert.strictEqual( + result, + 1709164800000, + 'Correct timestamp for leap day 2024-02-29', + ); + }); + + test('it handles the start and end of a year correctly', function (assert) { + const startOfYear = getUTCMidnightTimestampFromDate('2024-01-01'); + const endOfYear = getUTCMidnightTimestampFromDate('2024-12-31'); + + assert.strictEqual( + startOfYear, + 1704067200000, + 'Correct timestamp for start of the year 2024-01-01', + ); + assert.strictEqual( + endOfYear, + 1735603200000, + 'Correct timestamp for end of the year 2024-12-31', + ); + }); +}); diff --git a/tests/unit/utils/redirect-auth-test.js b/tests/unit/utils/redirect-auth-test.js new file mode 100644 index 00000000..eb18c9ff --- /dev/null +++ b/tests/unit/utils/redirect-auth-test.js @@ -0,0 +1,88 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'website-www/tests/helpers'; +import redirectAuth from 'website-www/utils/redirect-auth'; +import { AUTH_URL } from 'website-www/constants/urls'; +import sinon from 'sinon'; + +module('Unit | Utils | redirectAuth', function (hooks) { + setupTest(hooks); + + let windowOpenStub; + + hooks.beforeEach(function () { + windowOpenStub = sinon.stub(window, 'open'); + }); + + hooks.afterEach(function () { + windowOpenStub.restore(); + }); + + test('it exists', function (assert) { + assert.ok(redirectAuth, 'redirectAuth utility should be defined'); + }); + + test('it constructs correct auth URL with state parameter', function (assert) { + redirectAuth(); + + const expectedUrl = `${AUTH_URL}&state=${window.location.href}`; + + assert.ok(windowOpenStub.calledOnce, 'window.open should be called once'); + assert.ok( + windowOpenStub.calledWith(expectedUrl, '_self'), + 'window.open should be called with correct URL and target', + ); + }); + + test('it calls window.open with correct parameters', function (assert) { + redirectAuth(); + + assert.ok( + windowOpenStub.calledWith(sinon.match.string, '_self'), + 'window.open should be called with a URL string and _self target', + ); + + const calledUrl = windowOpenStub.firstCall.args[0]; + assert.ok( + calledUrl.startsWith(AUTH_URL), + 'called URL should start with AUTH_URL', + ); + + assert.ok( + calledUrl.includes('state='), + 'called URL should include state parameter', + ); + }); + + test('it handles when window is undefined', function (assert) { + assert.expect(2); + + const tempWindow = undefined; + + // rewriting the redirect auth for temp window + const redirectAuthTest = function () { + let authUrl = AUTH_URL; + if (typeof tempWindow !== 'undefined' && tempWindow?.location) { + authUrl = `${authUrl}&state=${tempWindow.location.href}`; + tempWindow.open(authUrl, '_self'); + } + }; + + try { + redirectAuthTest(); + assert.ok( + true, + 'function should execute without throwing error when window is undefined', + ); + } catch (error) { + assert.ok( + false, + 'function should not throw error when window is undefined', + ); + } + + assert.notOk( + windowOpenStub.called, + 'window.open should not be called when window is undefined', + ); + }); +});