Skip to content

Commit

Permalink
fix:added change event for extensionFieldChange
Browse files Browse the repository at this point in the history
  • Loading branch information
Amitkanswal committed Oct 1, 2024
1 parent 0b2e1ca commit 49b4bd2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
26 changes: 20 additions & 6 deletions __test__/field.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
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 @@ -12,6 +14,7 @@ describe("Field", () => {
let sendToParentError = function () {
return Promise.reject("sample error");
};
let eventRegistration: EventRegistration;

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

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

Expand All @@ -115,12 +127,14 @@ describe("Field", () => {
let emptySingleFileField = new Field(
clonedfileField.single,
connection as any,
emitter
emitter,
eventRegistration
);
let emptyMultipleFileField = new Field(
clonedfileField.multiple,
connection as any,
emitter
emitter,
eventRegistration
);
expect(emptySingleFileField.getData()).toBe(undefined);
expect(emptyMultipleFileField.getData().length).toBe(0);
Expand Down
7 changes: 6 additions & 1 deletion src/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import postRobot from "post-robot";
import { IFieldInitData, IFieldModifierLocationInitData } from "./types";
import { GenericObjectType } from "./types/common.types";
import { Schema } from "./types/stack.types";
import EventRegistration from './EventRegistration';

const excludedDataTypesForSetField = [
"file",
Expand Down Expand Up @@ -44,11 +45,13 @@ class Field {
_resolvedData: GenericObjectType;
_self: boolean;
_connection: typeof postRobot;
_eventRegistration: EventRegistration;

constructor(
fieldDataObject: IFieldInitData | IFieldModifierLocationInitData,
connection: typeof postRobot,
emitter: EventEmitter
emitter: EventEmitter,
eventRegistration: EventRegistration
) {
/**
* The UID of the current field is defined in the content type of the entry.
Expand All @@ -67,6 +70,7 @@ class Field {
*/
this.schema = fieldDataObject.schema;
this._emitter = emitter;
this._eventRegistration = eventRegistration;

const separatedData = separateResolvedData(this, fieldDataObject.value);

Expand Down Expand Up @@ -167,6 +171,7 @@ class Field {
fieldObj._emitter.on("extensionFieldChange", (event: any) => {
this._data = event.data;
this._resolvedData = event.data;
this._eventRegistration.insertEvent("events", "extensionFieldChange");
callback(event.data);
});
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/uiLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ class UiLocation {
default: {
initializationData.self = true;
this.location.CustomField = {
field: new Field(initializationData, postRobot, emitter),
field: new Field(initializationData, postRobot, emitter, this.eventRegistration),
fieldConfig: initializationData.field_config,
entry: new Entry(
initializationData,
Expand Down

0 comments on commit 49b4bd2

Please sign in to comment.