-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add unit test for dateConversion, redirectAuth utils
- Loading branch information
1 parent
4d20127
commit f20cf67
Showing
2 changed files
with
138 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,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', | ||
); | ||
}); | ||
}); |
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,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', | ||
); | ||
}); | ||
}); |