Skip to content

Commit

Permalink
add unit test for dateConversion, redirectAuth utils
Browse files Browse the repository at this point in the history
  • Loading branch information
MayankBansal12 committed Jan 11, 2025
1 parent 4d20127 commit f20cf67
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
50 changes: 50 additions & 0 deletions tests/unit/utils/date-conversion-test.js
Original file line number Diff line number Diff line change
@@ -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',
);
});
});
88 changes: 88 additions & 0 deletions tests/unit/utils/redirect-auth-test.js
Original file line number Diff line number Diff line change
@@ -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',
);
});
});

0 comments on commit f20cf67

Please sign in to comment.