Skip to content

Commit 9c1ef77

Browse files
committed
Introduce createDateString helper function instead of modifying Date.prototype
1 parent f425557 commit 9c1ef77

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

tests/interfaces/records/handlers/records-query.spec.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,12 @@ import { constructRecordsWriteIndexes } from '../../../../src/interfaces/records
2828
import { DataStream, DidResolver, Dwn, HdKey, KeyDerivationScheme, Records } from '../../../../src/index.js';
2929
import { DateSort, RecordsQuery } from '../../../../src/interfaces/records/messages/records-query.js';
3030

31-
declare global {
32-
interface Date {
33-
toTemporalInstant: typeof toTemporalInstant;
34-
}
35-
}
36-
Date.prototype.toTemporalInstant = toTemporalInstant;
37-
3831
chai.use(chaiAsPromised);
3932

33+
function createDateString(d: Date): string {
34+
return toTemporalInstant.call(d).toString({ smallestUnit: 'microseconds' });
35+
}
36+
4037
describe('RecordsQueryHandler.handle()', () => {
4138
describe('functional tests', () => {
4239
let didResolver: DidResolver;
@@ -194,9 +191,9 @@ describe('RecordsQueryHandler.handle()', () => {
194191

195192
it('should be able to range query by `dateCreated`', async () => {
196193
// scenario: 3 records authored by alice, created on first of 2021, 2022, and 2023 respectively, only the first 2 records share the same schema
197-
const firstDayOf2021 = new Date(2021, 1, 1).toTemporalInstant().toString({ smallestUnit: 'microseconds' });
198-
const firstDayOf2022 = new Date(2022, 1, 1).toTemporalInstant().toString({ smallestUnit: 'microseconds' });
199-
const firstDayOf2023 = new Date(2023, 1, 1).toTemporalInstant().toString({ smallestUnit: 'microseconds' });
194+
const firstDayOf2021 = createDateString(new Date(2021, 1, 1));
195+
const firstDayOf2022 = createDateString(new Date(2022, 1, 1));
196+
const firstDayOf2023 = createDateString(new Date(2023, 1, 1));
200197
const alice = await DidKeyResolver.generate();
201198
const write1 = await TestDataGenerator.generateRecordsWrite({ requester: alice, dateCreated: firstDayOf2021, dateModified: firstDayOf2021 });
202199
const write2 = await TestDataGenerator.generateRecordsWrite({ requester: alice, dateCreated: firstDayOf2022, dateModified: firstDayOf2022 });
@@ -211,7 +208,7 @@ describe('RecordsQueryHandler.handle()', () => {
211208
expect(writeReply3.status.code).to.equal(202);
212209

213210
// testing `from` range
214-
const lastDayOf2021 = new Date(2021, 12, 31).toTemporalInstant().toString({ smallestUnit: 'microseconds' });
211+
const lastDayOf2021 = createDateString(new Date(2021, 12, 31));
215212
const recordsQuery1 = await TestDataGenerator.generateRecordsQuery({
216213
requester : alice,
217214
filter : { dateCreated: { from: lastDayOf2021 } },
@@ -223,7 +220,7 @@ describe('RecordsQueryHandler.handle()', () => {
223220
expect(reply1.entries![1].encodedData).to.equal(Encoder.bytesToBase64Url(write3.dataBytes!));
224221

225222
// testing `to` range
226-
const lastDayOf2022 = new Date(2022, 12, 31).toTemporalInstant().toString({ smallestUnit: 'microseconds' });
223+
const lastDayOf2022 = createDateString(new Date(2022, 12, 31));
227224
const recordsQuery2 = await TestDataGenerator.generateRecordsQuery({
228225
requester : alice,
229226
filter : { dateCreated: { to: lastDayOf2022 } },
@@ -235,7 +232,7 @@ describe('RecordsQueryHandler.handle()', () => {
235232
expect(reply2.entries![1].encodedData).to.equal(Encoder.bytesToBase64Url(write2.dataBytes!));
236233

237234
// testing `from` and `to` range
238-
const lastDayOf2023 = new Date(2023, 12, 31).toTemporalInstant().toString({ smallestUnit: 'microseconds' });
235+
const lastDayOf2023 = createDateString(new Date(2023, 12, 31));
239236
const recordsQuery3 = await TestDataGenerator.generateRecordsQuery({
240237
requester : alice,
241238
filter : { dateCreated: { from: lastDayOf2022, to: lastDayOf2023 } },
@@ -258,9 +255,9 @@ describe('RecordsQueryHandler.handle()', () => {
258255

259256
it('should be able use range and exact match queries at the same time', async () => {
260257
// scenario: 3 records authored by alice, created on first of 2021, 2022, and 2023 respectively, only the first 2 records share the same schema
261-
const firstDayOf2021 = new Date(2021, 1, 1).toTemporalInstant().toString({ smallestUnit: 'microseconds' });
262-
const firstDayOf2022 = new Date(2022, 1, 1).toTemporalInstant().toString({ smallestUnit: 'microseconds' });
263-
const firstDayOf2023 = new Date(2023, 1, 1).toTemporalInstant().toString({ smallestUnit: 'microseconds' });
258+
const firstDayOf2021 = createDateString(new Date(2021, 1, 1));
259+
const firstDayOf2022 = createDateString(new Date(2022, 1, 1));
260+
const firstDayOf2023 = createDateString(new Date(2023, 1, 1));
264261
const alice = await DidKeyResolver.generate();
265262
const schema = '2021And2022Schema';
266263
const write1 = await TestDataGenerator.generateRecordsWrite({
@@ -282,8 +279,8 @@ describe('RecordsQueryHandler.handle()', () => {
282279
expect(writeReply3.status.code).to.equal(202);
283280

284281
// testing range criterion with another exact match
285-
const lastDayOf2021 = new Date(2021, 12, 31).toTemporalInstant().toString({ smallestUnit: 'microseconds' });
286-
const lastDayOf2023 = new Date(2023, 12, 31).toTemporalInstant().toString({ smallestUnit: 'microseconds' });
282+
const lastDayOf2021 = createDateString(new Date(2021, 12, 31));
283+
const lastDayOf2023 = createDateString(new Date(2023, 12, 31));
287284
const recordsQuery5 = await TestDataGenerator.generateRecordsQuery({
288285
requester : alice,
289286
filter : {

0 commit comments

Comments
 (0)