Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions __test__/entry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,56 @@ describe("Entry", () => {
expect(testData.entry).toEqual(entry.getData());
});

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);
});

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", () => {
it("getField undefined", function () {
const uid = "group1.group";
Expand Down Expand Up @@ -133,7 +183,7 @@ describe("Entry", () => {
});
it("should use custom Field instance if internal flag is set", () => {
const fieldInstance: any = jest.fn();
entry = new Entry(testData as any, connection as any, emitter ,{
entry = new Entry(testData as any, connection as any, emitter, {
_internalFlags: {
FieldInstance: fieldInstance,
},
Expand Down Expand Up @@ -182,4 +232,4 @@ describe("Entry", () => {
"Callback must be a function"
);
});
});
});
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
19 changes: 19 additions & 0 deletions src/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */

Expand Down Expand Up @@ -91,6 +92,24 @@ class Entry {
return this._data;
}

/**
* Retrieves the draft data of the current unsaved entry.
* Returns an empty object if there are no changes.
*
* @returns {Promise<GenericObjectType>} The draft entry data or an empty object.
*/
async getDraftData(): Promise<GenericObjectType> {
try {
const response =
await this._connection.sendToParent<GenericObjectType>(
"getDraftData"
);
return response?.data ?? {};
} catch (error) {
throw new Error("Failed to retrieve draft data.");
}
}

/**
*
*
Expand Down
5 changes: 4 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ export enum Region {
UNKNOWN = "UNKNOWN",
NA = "NA",
EU = "EU",
AU = "AU",
AZURE_NA = "AZURE_NA",
AZURE_EU = "AZURE_EU",
GCP_NA = "GCP_NA",
Expand All @@ -264,7 +265,9 @@ export type RegionType =
| "UNKNOWN"
| "NA"
| "EU"
| "AU"
| "AZURE_NA"
| "AZURE_EU"
| "GCP_NA"
| string;
| "GCP_EU"
| (string & {});
Loading