Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jcao49 committed Nov 20, 2024
1 parent c75063a commit 16c06ab
Showing 1 changed file with 166 additions and 9 deletions.
175 changes: 166 additions & 9 deletions packages/api-client-core/spec/GadgetRecord.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,8 @@ describe("GadgetRecord", () => {
});

describe("record changes for current Date values against previous string value", () => {
it("correctly calculates hasChanges when the current value is the same Date as the previous value", () => {
it("correctly calculates overall record changed when the current value is the same Date as the previous value", () => {
// tests record.changed() with no specific field
const dateStr = "2024-04-19T21:03:37.000Z";

productBaseRecord = {
Expand All @@ -461,7 +462,9 @@ describe("GadgetRecord", () => {
expect(product.changed()).toBe(false);
});

it("correctly calculates record changed when the current value is a Date and is the same date as the previous value", () => {
it("correctly calculates record changed for a certain field when the current value is a Date and is the same date as the previous value", () => {
// tests record.changed("publishedAt") ie changes for a certain field

const dateStr = "2024-04-19T21:03:37.000Z";

productBaseRecord = {
Expand All @@ -478,7 +481,9 @@ describe("GadgetRecord", () => {
expect(product.changed("publishedAt")).toBe(false);
});

it("correctly calculates record changed when the current value is a string and is the same date as the previous value", () => {
it("correctly calculates record changed for a certain field when the current value is a string and is the same date as the previous value", () => {
// tests record.changed("publishedAt") ie changes for a certain field

const dateStr = "2024-04-19T21:03:37.000Z";

productBaseRecord = {
Expand All @@ -495,7 +500,9 @@ describe("GadgetRecord", () => {
expect(product.changed("publishedAt")).toBe(false);
});

it("correctly calculates hasChanges when the current value is a newer Date than the previous value", () => {
it("correctly calculates overall record changed when the current value is a newer Date than the previous value", () => {
// tests record.changed() with no specific field

const oldDateStr = "2024-04-19T21:03:37.000Z";
const newDateStr = "2024-11-19T21:03:37.000Z";

Expand All @@ -513,7 +520,9 @@ describe("GadgetRecord", () => {
expect(product.changed()).toBe(true);
});

it("correctly calculates record changed when the current value is a Date and is a newer date than the previous value", () => {
it("correctly calculates record changed for a certain field when the current value is a Date and is a newer date than the previous value", () => {
// tests record.changed("publishedAt") ie changes for a certain field

const oldDateStr = "2024-04-19T21:03:37.000Z";
const newDateStr = "2024-11-19T21:03:37.000Z";

Expand All @@ -531,7 +540,9 @@ describe("GadgetRecord", () => {
expect(product.changed("publishedAt")).toBe(true);
});

it("correctly calculates record changed when the current value is a string and is a newer date than the previous value", () => {
it("correctly calculates record changed for a certain field when the current value is a string and is a newer date than the previous value", () => {
// tests record.changed("publishedAt") ie changes for a certain field

const oldDateStr = "2024-04-19T21:03:37.000Z";
const newDateStr = "2024-11-19T21:03:37.000Z";

Expand All @@ -549,7 +560,9 @@ describe("GadgetRecord", () => {
expect(product.changed("publishedAt")).toBe(true);
});

it("correctly calculates hasChanges when the current value is an older Date than the previous value", () => {
it("correctly calculates overall record changed when the current value is an older Date than the previous value", () => {
// tests record.changed() with no specific field

const oldDateStr = "2024-04-19T21:03:37.000Z";
const newDateStr = "2024-11-19T21:03:37.000Z";

Expand All @@ -567,7 +580,9 @@ describe("GadgetRecord", () => {
expect(product.changed()).toBe(true);
});

it("correctly calculates record changed when the current value is a Date and is an older date than the previous value", () => {
it("correctly calculates record changed for a certain field when the current value is a Date and is an older date than the previous value", () => {
// tests record.changed("publishedAt") ie changes for a certain field

const oldDateStr = "2024-04-19T21:03:37.000Z";
const newDateStr = "2024-11-19T21:03:37.000Z";

Expand All @@ -585,7 +600,9 @@ describe("GadgetRecord", () => {
expect(product.changed("publishedAt")).toBe(true);
});

it("correctly calculates record changed when the current value is a string and is an older date than the previous value", () => {
it("correctly calculates record changed for a certain field when the current value is a string and is an older date than the previous value", () => {
// tests record.changed("publishedAt") ie changes for a certain field

const oldDateStr = "2024-04-19T21:03:37.000Z";
const newDateStr = "2024-11-19T21:03:37.000Z";

Expand All @@ -602,5 +619,145 @@ describe("GadgetRecord", () => {
product.publishedAt = oldDateStr;
expect(product.changed("publishedAt")).toBe(true);
});

it("correctly calculates record changed for a certain field when the current value is an invalid date string", () => {
// tests record.changed("publishedAt") ie changes for a certain field
// if the current value is an invalid date string and the previous was a valid date then record changed should be true

const oldValidDateStr = "2024-04-19T21:03:37.000Z";

productBaseRecord = {
id: "123",
name: "A cool product",
body: "A description of why it's cool",
publishedAt: oldValidDateStr,
};

const product = new GadgetRecord<SampleBaseRecord>(productBaseRecord);
expect(product.changed("publishedAt")).toBe(false);

product.publishedAt = "invalidDate";
expect(product.changed("publishedAt")).toBe(true);
});

it("correctly calculates record changed for a certain field when the current value is an invalid Date", () => {
// tests record.changed("publishedAt") ie changes for a certain field
// if the current value is an invalid Date and the previous was a valid date then record changed should be true

const oldValidDateStr = "2024-04-19T21:03:37.000Z";

productBaseRecord = {
id: "123",
name: "A cool product",
body: "A description of why it's cool",
publishedAt: oldValidDateStr,
};

const product = new GadgetRecord<SampleBaseRecord>(productBaseRecord);
expect(product.changed("publishedAt")).toBe(false);

product.publishedAt = new Date("invalid");
expect(product.changed("publishedAt")).toBe(true);
});

it("correctly calculates record changed for a certain field when the current and previous values are Dates", () => {
// tests record.changed("publishedAt") ie changes for a certain field

const oldValidDateStr = "2024-04-19T21:03:37.000Z";
const newValidDateStr = "2024-11-19T21:03:37.000Z";

productBaseRecord = {
id: "123",
name: "A cool product",
body: "A description of why it's cool",
publishedAt: new Date(oldValidDateStr),
};

const product = new GadgetRecord<SampleBaseRecord>(productBaseRecord);
expect(product.changed("publishedAt")).toBe(false);

product.publishedAt = new Date(newValidDateStr);
expect(product.changed("publishedAt")).toBe(true);
});

it("correctly calculates record changed for a certain field when the current date as a string does not have a time and is a different date than previous", () => {
// tests record.changed("publishedAt") ie changes for a certain field

const oldValidDateStr = "2024-04-19T21:03:37.000Z";
const newValidDateStr = "2024-11-19"; // new date with no time

productBaseRecord = {
id: "123",
name: "A cool product",
body: "A description of why it's cool",
publishedAt: oldValidDateStr,
};

const product = new GadgetRecord<SampleBaseRecord>(productBaseRecord);
expect(product.changed("publishedAt")).toBe(false);

product.publishedAt = newValidDateStr;
expect(product.changed("publishedAt")).toBe(true);
});

it("correctly calculates record changed for a certain field when the current date as a string does not have a time and is same date as previous", () => {
// tests record.changed("publishedAt") ie changes for a certain field

const oldValidDateStr = "2024-04-19T21:03:37.000Z";
const newValidDateStr = "2024-04-19"; // same date with no time

productBaseRecord = {
id: "123",
name: "A cool product",
body: "A description of why it's cool",
publishedAt: oldValidDateStr,
};

const product = new GadgetRecord<SampleBaseRecord>(productBaseRecord);
expect(product.changed("publishedAt")).toBe(false);

product.publishedAt = newValidDateStr;
expect(product.changed("publishedAt")).toBe(true);
});

it("correctly calculates record changed for a certain field when the current date as a Date does not have a time and is a different date than previous", () => {
// tests record.changed("publishedAt") ie changes for a certain field

const oldValidDateStr = "2024-04-19T21:03:37.000Z";
const newValidDateStr = "2024-11-19"; // new date with no time

productBaseRecord = {
id: "123",
name: "A cool product",
body: "A description of why it's cool",
publishedAt: oldValidDateStr,
};

const product = new GadgetRecord<SampleBaseRecord>(productBaseRecord);
expect(product.changed("publishedAt")).toBe(false);

product.publishedAt = new Date(newValidDateStr);
expect(product.changed("publishedAt")).toBe(true);
});

it("correctly calculates record changed for a certain field when the current date as a Date does not have a time and is same date as previous", () => {
// tests record.changed("publishedAt") ie changes for a certain field

const oldValidDateStr = "2024-04-19T21:03:37.000Z";
const newValidDateStr = "2024-04-19"; // same date with no time

productBaseRecord = {
id: "123",
name: "A cool product",
body: "A description of why it's cool",
publishedAt: oldValidDateStr,
};

const product = new GadgetRecord<SampleBaseRecord>(productBaseRecord);
expect(product.changed("publishedAt")).toBe(false);

product.publishedAt = new Date(newValidDateStr);
expect(product.changed("publishedAt")).toBe(true);
});
});
});

0 comments on commit 16c06ab

Please sign in to comment.