From 7cca20c051ffa44a9bad4e7858786fa56e1c9ac1 Mon Sep 17 00:00:00 2001 From: Abhishek Ezhava Date: Wed, 11 Jun 2025 17:17:10 +0530 Subject: [PATCH 1/2] test: getDraftData --- src/entry.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/entry.ts b/src/entry.ts index 279016a..bcbcb68 100755 --- a/src/entry.ts +++ b/src/entry.ts @@ -17,6 +17,7 @@ import { import { ContentType, PublishDetails, Schema } from "./types/stack.types"; import { GenericObjectType } from "./types/common.types"; import EventRegistry from "./EventRegistry"; +import { onData, onError } from "./utils/utils"; /** Class representing an entry from Contentstack UI. Not available for Dashboard UI Location. */ @@ -94,14 +95,15 @@ class Entry { /** * Gets the draft data of the current entry. * If no changes are available, returns an empty object. - * @return {Object} Returns the draft entry data (_changedData) if available; otherwise, returns an empty object. + * @return {Promise} Returns a promise that resolves to the draft entry data (_changedData) if available; otherwise, returns an empty object. */ - getDraftData() { - if (this._changedData && Object.keys(this._changedData).length > 0) { - return this._changedData; - } else { - return {}; - } + async getDraftData(): Promise { + const changedData = this._changedData || {}; + console.log("changedData", changedData); + return this._connection + .sendToParent("getDraftData", { changedData }) + .then(onData) + .catch(onError); } /** From d8f8ecb860970bd59a3c3f943af695301e02b174 Mon Sep 17 00:00:00 2001 From: Abhishek Ezhava Date: Thu, 12 Jun 2025 18:11:47 +0530 Subject: [PATCH 2/2] fix: test-cases & version update --- __test__/entry.test.ts | 53 ++++++++++++++++++++++++++++++++++++------ package-lock.json | 4 ++-- package.json | 2 +- src/entry.ts | 22 +++++++++++------- 4 files changed, 62 insertions(+), 19 deletions(-) diff --git a/__test__/entry.test.ts b/__test__/entry.test.ts index a7e506b..2548935 100644 --- a/__test__/entry.test.ts +++ b/__test__/entry.test.ts @@ -57,15 +57,54 @@ describe("Entry", () => { expect(testData.entry).toEqual(entry.getData()); }); - it("getDraftData", () => { - entry._changedData = { title: "Draft Title", content: "Draft Content" }; - expect(entry.getDraftData()).toEqual({ - title: "Draft Title", - content: "Draft Content", + describe("getDraftData", () => { + it("should return draft data successfully", async () => { + const mockDraftData = { + title: "Draft Title", + description: "Draft Description", + }; + const sendToParentSpy = jest + .spyOn(connection, "sendToParent") + .mockResolvedValue({ data: mockDraftData }); + + const result = await entry.getDraftData(); + + expect(sendToParentSpy).toHaveBeenCalledWith("getDraftData"); + expect(result).toEqual(mockDraftData); }); - entry._changedData = {}; - expect(entry.getDraftData()).toEqual({}); + it("should return empty object when response data is null", async () => { + const sendToParentSpy = jest + .spyOn(connection, "sendToParent") + .mockResolvedValue({ data: null }); + + const result = await entry.getDraftData(); + + expect(sendToParentSpy).toHaveBeenCalledWith("getDraftData"); + expect(result).toEqual({}); + }); + + it("should return empty object when response data is undefined", async () => { + const sendToParentSpy = jest + .spyOn(connection, "sendToParent") + .mockResolvedValue({ data: undefined }); + + const result = await entry.getDraftData(); + + expect(sendToParentSpy).toHaveBeenCalledWith("getDraftData"); + expect(result).toEqual({}); + }); + + it("should throw error when sendToParent fails", async () => { + const sendToParentSpy = jest + .spyOn(connection, "sendToParent") + .mockRejectedValue(new Error("Connection failed")); + + await expect(entry.getDraftData()).rejects.toThrow( + "Failed to retrieve draft data." + ); + expect(sendToParentSpy).toHaveBeenCalledWith("getDraftData"); + }); }); describe("getField", () => { diff --git a/package-lock.json b/package-lock.json index 99a0c72..6b31b37 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@contentstack/app-sdk", - "version": "2.3.1", + "version": "2.3.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@contentstack/app-sdk", - "version": "2.3.1", + "version": "2.3.2", "license": "MIT", "dependencies": { "axios": "^1.7.9", diff --git a/package.json b/package.json index 30f8cff..5fa7d7c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@contentstack/app-sdk", - "version": "2.3.1", + "version": "2.3.2", "types": "dist/src/index.d.ts", "description": "The Contentstack App SDK allows you to customize your Contentstack applications.", "main": "dist/index.js", diff --git a/src/entry.ts b/src/entry.ts index bcbcb68..14ecd76 100755 --- a/src/entry.ts +++ b/src/entry.ts @@ -93,17 +93,21 @@ class Entry { } /** - * Gets the draft data of the current entry. - * If no changes are available, returns an empty object. - * @return {Promise} Returns a promise that resolves to the draft entry data (_changedData) if available; otherwise, returns an empty object. + * Retrieves the draft data of the current unsaved entry. + * Returns an empty object if there are no changes. + * + * @returns {Promise} The draft entry data or an empty object. */ async getDraftData(): Promise { - const changedData = this._changedData || {}; - console.log("changedData", changedData); - return this._connection - .sendToParent("getDraftData", { changedData }) - .then(onData) - .catch(onError); + try { + const response = + await this._connection.sendToParent( + "getDraftData" + ); + return response?.data ?? {}; + } catch (error) { + throw new Error("Failed to retrieve draft data."); + } } /**