Skip to content

Commit

Permalink
Add constructFrom tests (#3559)
Browse files Browse the repository at this point in the history
Co-authored-by: Sasha Koss <koss@nocorp.me>
  • Loading branch information
tan75 and kossnocorp committed Mar 12, 2024
1 parent 8271303 commit 3611e63
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/constructFrom/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import assert from "assert";
import { describe, it } from "vitest";
import { constructFrom } from ".";

describe("constructFrom", () => {
it("should create a new Date instance using the constructor from the reference date", () => {
const referenceDate = new Date("2023-10-25T12:00:00");
const value = new Date("2023-10-26T12:00:00");

const result = constructFrom(referenceDate, value);

assert.ok(result instanceof Date);
assert.deepStrictEqual(result, value);
assert.strictEqual(result.constructor, referenceDate.constructor);
});

it("should create a new Date instance using a number as the reference date", () => {
const referenceDate = 1635158400000; // October 25, 2023
const value = new Date("2023-10-26T12:00:00");

const result = constructFrom(referenceDate, value);

assert.ok(result instanceof Date);
assert.deepStrictEqual(result, value);
});

it("should create a new custom Date instance using the constructor from the reference date", () => {
class CustomDate extends Date {}
const referenceDate = new CustomDate("2023-10-25T12:00:00");
const value = new CustomDate("2023-10-26T12:00:00");

const result = constructFrom(referenceDate, value);

assert.ok(result instanceof CustomDate);
assert.deepStrictEqual(result, value);
assert.strictEqual(result.constructor, referenceDate.constructor);
});

it("should create a new Date instance using numbers as both referenceDate and value", () => {
const referenceDate = 1635158400000; // October 25, 2023
const value = 1635244800000; // October 26, 2023

const result = constructFrom(referenceDate, value);

assert.ok(result instanceof Date);
assert.deepStrictEqual(result.getTime(), value);
});
});

0 comments on commit 3611e63

Please sign in to comment.