From 16c06ab10da0893f4acbdf26a273bf05bcacc3d5 Mon Sep 17 00:00:00 2001 From: jcao49 Date: Wed, 20 Nov 2024 11:38:01 -0500 Subject: [PATCH] Add tests --- .../api-client-core/spec/GadgetRecord.spec.ts | 175 +++++++++++++++++- 1 file changed, 166 insertions(+), 9 deletions(-) diff --git a/packages/api-client-core/spec/GadgetRecord.spec.ts b/packages/api-client-core/spec/GadgetRecord.spec.ts index 3fa839fa9..36a6e70f9 100644 --- a/packages/api-client-core/spec/GadgetRecord.spec.ts +++ b/packages/api-client-core/spec/GadgetRecord.spec.ts @@ -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 = { @@ -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 = { @@ -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 = { @@ -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"; @@ -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"; @@ -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"; @@ -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"; @@ -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"; @@ -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"; @@ -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(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(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(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(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(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(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(productBaseRecord); + expect(product.changed("publishedAt")).toBe(false); + + product.publishedAt = new Date(newValidDateStr); + expect(product.changed("publishedAt")).toBe(true); + }); }); });