Skip to content

Commit 036c319

Browse files
Fix unit test dates
1 parent 132326d commit 036c319

File tree

1 file changed

+45
-38
lines changed

1 file changed

+45
-38
lines changed

internal/datastore/src/__test__/letter-repository.test.ts

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -458,17 +458,22 @@ describe("LetterRepository", () => {
458458
});
459459

460460
test("successful upsert (update status) returns updated letter", async () => {
461-
const insertLetter: InsertLetter = createLetter("supplier1", "letter1");
462-
463-
const existingLetter = await letterRepository.putLetter(insertLetter);
461+
jest.useFakeTimers();
462+
jest.setSystemTime(new Date(2020, 0, 1));
463+
const letter: InsertLetter = createLetter("supplier1", "letter1");
464+
const existingLetter: Letter = await letterRepository.putLetter(letter);
464465

465-
const result = await letterRepository.upsertLetter({
466+
const updateLetterStatus: UpsertLetter = {
466467
id: "letter1",
467468
supplierId: "supplier1",
468469
status: "REJECTED",
469470
reasonCode: "R01",
470471
reasonText: "R01 text",
471-
});
472+
};
473+
474+
jest.setSystemTime(new Date(2020, 0, 2));
475+
const result: Letter =
476+
await letterRepository.upsertLetter(updateLetterStatus);
472477

473478
expect(result).toEqual(
474479
expect.objectContaining({
@@ -486,14 +491,13 @@ describe("LetterRepository", () => {
486491
expect(Date.parse(result.updatedAt)).toBeGreaterThan(
487492
Date.parse(existingLetter.updatedAt),
488493
);
489-
expect(Date.parse(result.updatedAt)).toBeLessThan(Date.now());
490494
expect(result.createdAt).toBe(existingLetter.createdAt);
491495
expect(result.createdAt).toBe(result.supplierStatusSk);
492-
expect(result.ttl).toBe(existingLetter.ttl);
496+
expect(result.ttl).toBeGreaterThan(existingLetter.ttl);
493497
});
494498

495-
test("successful upsert (insert) returns created letter", async () => {
496-
const upsertLetter: UpsertLetter = {
499+
test("successful upsert (insert letter) returns created letter", async () => {
500+
const insertLetter: UpsertLetter = {
497501
id: "letter1",
498502
status: "PENDING",
499503
specificationId: "specification1",
@@ -502,9 +506,11 @@ describe("LetterRepository", () => {
502506
url: "s3://bucket/letter1.pdf",
503507
};
504508

505-
const beforeInsert = Date.now() - 1; // widen window
509+
const nowTest: Date = new Date(2020, 0, 1);
510+
jest.useFakeTimers();
511+
jest.setSystemTime(nowTest);
506512

507-
const result = await letterRepository.upsertLetter(upsertLetter);
513+
const result: Letter = await letterRepository.upsertLetter(insertLetter);
508514

509515
expect(result).toEqual(
510516
expect.objectContaining({
@@ -517,39 +523,23 @@ describe("LetterRepository", () => {
517523
}),
518524
);
519525

520-
expect(Date.parse(result.updatedAt)).toBeGreaterThan(beforeInsert);
521-
expect(Date.parse(result.updatedAt)).toBeLessThan(Date.now());
526+
expect(Date.parse(result.updatedAt)).toBe(nowTest.valueOf());
522527
expect(result.createdAt).toBe(result.updatedAt);
523528
expect(result.supplierStatusSk).toBe(result.createdAt);
529+
expect(result.ttl).toBe(new Date(2020, 0, 1, 1).valueOf() / 1000);
524530
});
525531

526-
test("unsuccessful upsert should throw error", async () => {
527-
const mockSend = jest.fn().mockResolvedValue({ Items: null });
528-
const mockDdbClient = { send: mockSend } as any;
529-
const repo = new LetterRepository(
530-
mockDdbClient,
531-
{ debug: jest.fn() } as any,
532-
{ lettersTableName: "letters", lettersTtlHours: 1 },
533-
);
534-
535-
await expect(
536-
repo.upsertLetter({
537-
id: "letter1",
538-
status: "PENDING",
539-
supplierId: "supplier1",
540-
}),
541-
).rejects.toThrow("upsertLetter: no attributes returned");
542-
});
543-
544-
test("successful upsert without status", async () => {
532+
test("successful upsert without status change (update url)", async () => {
533+
jest.useFakeTimers();
534+
jest.setSystemTime(new Date(2020, 0, 1));
545535
const insertLetter: InsertLetter = createLetter("supplier1", "letter1");
546-
547536
const existingLetter = await letterRepository.putLetter(insertLetter);
548537

538+
jest.setSystemTime(new Date(2020, 0, 2));
549539
const result = await letterRepository.upsertLetter({
550540
id: "letter1",
551541
supplierId: "supplier1",
552-
url: "s3://updatedBucket/letter1.pdf",
542+
url: "s3://updateToPdf",
553543
});
554544

555545
expect(result).toEqual(
@@ -559,16 +549,33 @@ describe("LetterRepository", () => {
559549
specificationId: "specification1",
560550
groupId: "group1",
561551
supplierId: "supplier1",
562-
url: "s3://updatedBucket/letter1.pdf",
552+
url: "s3://updateToPdf",
563553
supplierStatus: "supplier1#PENDING",
564554
}),
565555
);
566556
expect(Date.parse(result.updatedAt)).toBeGreaterThan(
567557
Date.parse(existingLetter.updatedAt),
568558
);
569-
expect(Date.parse(result.updatedAt)).toBeLessThan(Date.now());
570559
expect(result.createdAt).toBe(existingLetter.createdAt);
571-
expect(result.supplierStatusSk).toBe(existingLetter.supplierStatusSk);
572-
expect(result.ttl).toBe(existingLetter.ttl);
560+
expect(result.createdAt).toBe(result.supplierStatusSk);
561+
expect(result.ttl).toBeGreaterThan(existingLetter.ttl);
562+
});
563+
564+
test("unsuccessful upsert should throw error", async () => {
565+
const mockSend = jest.fn().mockResolvedValue({ Items: null });
566+
const mockDdbClient = { send: mockSend } as any;
567+
const repo = new LetterRepository(
568+
mockDdbClient,
569+
{ debug: jest.fn() } as any,
570+
{ lettersTableName: "letters", lettersTtlHours: 1 },
571+
);
572+
573+
await expect(
574+
repo.upsertLetter({
575+
id: "letter1",
576+
status: "PENDING",
577+
supplierId: "supplier1",
578+
}),
579+
).rejects.toThrow("upsertLetter: no attributes returned");
573580
});
574581
});

0 commit comments

Comments
 (0)