Skip to content

Commit

Permalink
refactor:review changes
Browse files Browse the repository at this point in the history
Signed-off-by: Amitkanswal <amitkanswal7@gmail.com>
  • Loading branch information
Amitkanswal committed Oct 1, 2024
1 parent 49b4bd2 commit 78e9d74
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 211 deletions.
18 changes: 4 additions & 14 deletions __test__/assetSidebarWidget.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import EventEmitter from "wolfy87-eventemitter";
import postRobot from "post-robot";
import AssetSidebarWidget from "../src/AssetSidebarWidget";
import { IAssetSidebarInitData, LocationType } from "../src/types";
import Asset from "../src/stack/api/asset";
import EventRegistration from '../src/EventRegistration';

jest.mock("post-robot", () => ({
__esModule: true,
Expand Down Expand Up @@ -32,38 +30,30 @@ describe("AssetSidebarWidget", () => {
let connection: { sendToParent: (...props: any[]) => any };
let sendToParent;
let emitter: EventEmitter;
let eventRegistration: EventRegistration;

beforeEach(function () {
sendToParent = function () {
return Promise.resolve({ data: {} });
};

emitter = {
on: (_event, cbf) => {
on: (_event: string, cbf: (data: { state: string }) => void) => {
setTimeout(() => {
cbf({ state: "full_width" });
}, 50);
},
} as EventEmitter;
emitEvent: (_s: string, _cb: () => void) => {}
} as unknown as EventEmitter;

jest.spyOn(emitter, "on");

connection = { sendToParent };
jest.spyOn(connection, "sendToParent");

eventRegistration = new EventRegistration({
connection: postRobot,
installationUID: "installationUID",
appUID: "appUID",
locationType: LocationType.ASSET_SIDEBAR_WIDGET,
});

assetSidebarWidget = new AssetSidebarWidget(
mockInitData as IAssetSidebarInitData,
connection as any,
emitter,
eventRegistration
emitter
);
});

Expand Down
9 changes: 3 additions & 6 deletions __test__/entry.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Entry from "../src/entry";
import EventRegistration from '../src/EventRegistration';
import testData from "./data/testData.json";
import { jest } from "@jest/globals";

Expand All @@ -8,7 +7,6 @@ describe("Entry", () => {
let emitter: any;
let entry: Entry;
let sendToParent: any;
let eventRegistration: EventRegistration;

beforeEach(() => {
sendToParent = () => {};
Expand All @@ -34,8 +32,7 @@ describe("Entry", () => {
entry = new Entry(
{ ...testData, changedData } as any,
connection as any,
emitter,
eventRegistration
emitter
);
});

Expand Down Expand Up @@ -136,7 +133,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,eventRegistration ,{
entry = new Entry(testData as any, connection as any, emitter ,{
_internalFlags: {
FieldInstance: fieldInstance,
},
Expand Down Expand Up @@ -174,7 +171,7 @@ describe("Entry", () => {
it("getField within Create page", function () {
const dataWithoutEntry = JSON.parse(JSON.stringify(testData));
dataWithoutEntry.entry = {};
entry = new Entry(dataWithoutEntry, connection as any, emitter, eventRegistration);
entry = new Entry(dataWithoutEntry, connection as any, emitter);
expect(() => entry.getField("invaliduid")).toThrowError(
"The data is unsaved. Save the data before requesting the field."
);
Expand Down
26 changes: 6 additions & 20 deletions __test__/field.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import postRobot from "post-robot";
import Field from "../src/field";
import testData from "./data/testData.json";
import fileFieldData from "./data/fileField.json";
import helpers from "./helpers";
import EventEmitter from "wolfy87-eventemitter";
import EventRegistration from '../src/EventRegistration';

describe("Field", () => {
let connection: { sendToParent: (...props: any[]) => any };
Expand All @@ -14,7 +12,6 @@ describe("Field", () => {
let sendToParentError = function () {
return Promise.reject("sample error");
};
let eventRegistration: EventRegistration;

describe("Generic", () => {
beforeEach(() => {
Expand All @@ -33,13 +30,7 @@ describe("Field", () => {
jest.spyOn(connection, "sendToParent");
jest.spyOn(emitter, "on");
(testData as any).self = true;
eventRegistration = new EventRegistration({
connection:postRobot,
installationUID: "sample",
appUID: "sample",
locationType: "sample",
});
field = new Field(testData as any, connection as any, emitter, eventRegistration);
field = new Field(testData as any, connection as any, emitter);
});

it("init", (done) => {
Expand Down Expand Up @@ -73,8 +64,7 @@ describe("Field", () => {
let newField = new Field(
testData as any,
{ sendToParent: sendToParentError } as any,
emitter,
eventRegistration
emitter
);
await expect((newField as any).setData()).rejects.toMatch(
"sample error"
Expand Down Expand Up @@ -109,14 +99,12 @@ describe("Field", () => {
singleFileField = new Field(
fileFieldData.single as any,
connection as any,
emitter,
eventRegistration
emitter
);
multipleFileField = new Field(
fileFieldData.multiple as any,
connection as any,
emitter,
eventRegistration
emitter
);
});

Expand All @@ -127,14 +115,12 @@ describe("Field", () => {
let emptySingleFileField = new Field(
clonedfileField.single,
connection as any,
emitter,
eventRegistration
emitter
);
let emptyMultipleFileField = new Field(
clonedfileField.multiple,
connection as any,
emitter,
eventRegistration
emitter
);
expect(emptySingleFileField.getData()).toBe(undefined);
expect(emptyMultipleFileField.getData().length).toBe(0);
Expand Down
13 changes: 1 addition & 12 deletions __test__/fieldModifierLocation/entry.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import postRobot from "post-robot";
import FieldModifierLocationEntry from "../../src/fieldModifierLocation/entry";
import testData from "../data/testData.json";
import { IFieldModifierLocationInitData, LocationType } from "../../src/types";
import generateErrorMessages, {
ERROR_MESSAGES,
} from "../../src/utils/errorMessages";
import EventRegistration from '../../src/EventRegistration';

describe("FieldModifierLocationEntry", () => {
let entryInstance: FieldModifierLocationEntry;
let sendToParent: any;
let connection: { sendToParent: (...props: any[]) => any };

let emitter: any;
let eventRegistration: any;
function getEntryInitialData(): IFieldModifierLocationInitData {
return {
type: LocationType.FIELD_MODIFIER_LOCATION,
Expand Down Expand Up @@ -79,13 +76,6 @@ describe("FieldModifierLocationEntry", () => {
beforeEach(() => {
sendToParent = () => {};
connection = { sendToParent };
eventRegistration = new EventRegistration({
connection: postRobot,
installationUID: "installationUID",
appUID: "appUID",
locationType: LocationType.FIELD,
});

emitter = {
on: (_event: any, cbf: (...props: any[]) => void) => {
setTimeout(() => {
Expand All @@ -102,8 +92,7 @@ describe("FieldModifierLocationEntry", () => {
entryInstance = new FieldModifierLocationEntry(
entryIntialData,
connection as any,
emitter,
eventRegistration
emitter
);
});

Expand Down
15 changes: 5 additions & 10 deletions src/AssetSidebarWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import postRobot from "post-robot";
import Asset from "./stack/api/asset";
import { Asset as AssetType } from "./types/stack.types";
import { GenericObjectType } from "./types/common.types";
import EventRegistration from './EventRegistration';

/** Class representing an Asset Sidebar UI Location from Contentstack UI. */

Expand All @@ -17,13 +16,11 @@ class AssetSidebarWidget {
_emitter: EventEmitter;
_connection: typeof postRobot;
_changedData?: GenericObjectType;
private _eventRegistration: EventRegistration;

constructor(
initializationData: IAssetSidebarInitData,
connection: typeof postRobot,
emitter: EventEmitter,
eventRegistration: EventRegistration
emitter: EventEmitter
) {
/**
* Gets the content type of the current asset.
Expand All @@ -36,8 +33,6 @@ class AssetSidebarWidget {

this._connection = connection;

this._eventRegistration = eventRegistration;

const thisAsset = this;

this._emitter.on("assetSave", (event: { data: AssetType }) => {
Expand Down Expand Up @@ -119,9 +114,9 @@ class AssetSidebarWidget {
const assetObj = this;
if (callback && typeof callback === "function") {
assetObj._emitter.on("assetSave", (event: { data: any }) => {
this._eventRegistration.insertEvent("events", "assetSave");
callback(event.data);
});
this._emitter.emitEvent("_eventRegistration",[{name:"assetSave"}]);
} else {
throw Error("Callback must be a function");
}
Expand All @@ -138,10 +133,10 @@ class AssetSidebarWidget {
assetObj._emitter.on(
"assetChange",
(event: { data: AssetType }) => {
this._eventRegistration.insertEvent("events", "assetChange");
callback(event.data);
}
);
this._emitter.emitEvent("_eventRegistration",[{name:"entryChange"}]);
} else {
throw Error("Callback must be a function");
}
Expand All @@ -158,10 +153,10 @@ class AssetSidebarWidget {
assetObj._emitter.on(
"assetPublish",
(event: { data: AssetType }) => {
this._eventRegistration.insertEvent("events", "assetPublish");
callback(event.data);
}
);
this._emitter.emitEvent("_eventRegistration",[{name:"assetPublish"}]);
} else {
throw Error("Callback must be a function");
}
Expand All @@ -178,10 +173,10 @@ class AssetSidebarWidget {
assetObj._emitter.on(
"assetUnPublish",
(event: { data: AssetType }) => {
this._eventRegistration.insertEvent("events", "assetUnPublish");
callback(event.data);
}
);
this._emitter.emitEvent("_eventRegistration",[{name:"assetUnPublish"}]);
} else {
throw Error("Callback must be a function");
}
Expand Down
95 changes: 0 additions & 95 deletions src/EventRegistration.ts

This file was deleted.

Loading

0 comments on commit 78e9d74

Please sign in to comment.