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
1 change: 1 addition & 0 deletions demo/javascript-demo/client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ <h4>This is a demo app using Fullscript.js with a script tag</h4>
feature.mount("treatment-plan-iframe");
feature.on("treatmentPlan.activated", mockCallback);
feature.on("patient.selected", mockCallback);
feature.on("order.checkoutStarted", mockCallback);
}

function mockCallback(data) {
Expand Down
5 changes: 5 additions & 0 deletions demo/react-demo/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const App = () => {
console.log(payload);
};

const handleOrderCheckoutStarted = (payload: EventListenerPayload<"order.checkoutStarted">) => {
console.log(payload);
};

const mountTreatmentPlan = () => {
// ################
// publicKey: fullscript-js-test-public-key-prac
Expand All @@ -40,6 +44,7 @@ const App = () => {
feature.mount("treatment-plan-iframe");
feature.on("treatmentPlan.activated", handleTreatmentPlanActivated);
feature.on("patient.selected", handlePatientSelected);
feature.on("order.checkoutStarted", handleOrderCheckoutStarted);
};

return (
Expand Down
19 changes: 18 additions & 1 deletion src/feature/eventType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const EVENT_TYPES = {
"treatmentPlan.activated": "treatmentPlan.activated",
"patient.selected": "patient.selected",
"order.checkoutStarted": "order.checkoutStarted",
};

type EventType = keyof typeof EVENT_TYPES;
Expand Down Expand Up @@ -60,10 +61,25 @@ type TreatmentPlanPayload = {
};
};

type OrderPayload = {
order: {
id: string;
practitionerPay: boolean;
treatmentPlan: TreatmentPlanPayload["treatmentPlan"];
};
};

// Indexing with `E` means adding an entry to EVENT_TYPES without one here is a compile error
type EventPayloads = {
"treatmentPlan.activated": TreatmentPlanPayload;
"patient.selected": PatientPayload;
"order.checkoutStarted": OrderPayload;
};

type EventListenerPayload<E extends EventType> = {
id: string;
type: E;
data: E extends "patient.selected" ? PatientPayload : TreatmentPlanPayload;
data: EventPayloads[E];
createdAt: string;
clinicId: string;
oauth: {
Expand All @@ -84,6 +100,7 @@ export {
EventListenerPayload,
TreatmentPlanPayload,
PatientPayload,
OrderPayload,
EventType,
EVENT_TYPES,
};
89 changes: 50 additions & 39 deletions src/feature/feature.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Dispatcher, createDispatcher } from "../eventSystem";
import { FeatureType } from "../feature";
import { FullscriptOptions } from "../fullscript";

import { EventType } from "./eventType";
import { FeatureOptions } from "./featureType";

let mockCreateIframe;
Expand Down Expand Up @@ -153,44 +154,54 @@ describe("feature", () => {
});

describe("dispatcher calls", () => {
it("calls registerEventListener on the dispatcher when on is called", () => {
return import("./feature").then(({ getFeature }) => {
const callback = jest.fn();
const eventType = "treatmentPlan.activated";
dispatcher.registerEventListener = jest.fn();
const feature = getFeature(
mockFeatureType,
mockFeatureOptions,
mockFullscriptOptions,
dispatcher
);

feature.on(eventType, callback);
expect(dispatcher.registerEventListener).toHaveBeenCalledWith(
`${mockFrameId}.${eventType}`,
callback
);
});
});

it("calls unregisterEventListener on the dispatcher when off is called", () => {
return import("./feature").then(({ getFeature }) => {
const callback = jest.fn();
const eventType = "treatmentPlan.activated";
dispatcher.unregisterEventListener = jest.fn();
const feature = getFeature(
mockFeatureType,
mockFeatureOptions,
mockFullscriptOptions,
dispatcher
);

feature.off(eventType, callback);
expect(dispatcher.unregisterEventListener).toHaveBeenCalledWith(
`${mockFrameId}.${eventType}`,
callback
);
});
});
const eventTypes: EventType[] = [
"treatmentPlan.activated",
"patient.selected",
"order.checkoutStarted",
];

it.each(eventTypes)(
"calls registerEventListener on the dispatcher when on is called with %s",
eventType => {
return import("./feature").then(({ getFeature }) => {
const callback = jest.fn();
dispatcher.registerEventListener = jest.fn();
const feature = getFeature(
mockFeatureType,
mockFeatureOptions,
mockFullscriptOptions,
dispatcher
);

feature.on(eventType, callback);
expect(dispatcher.registerEventListener).toHaveBeenCalledWith(
`${mockFrameId}.${eventType}`,
callback
);
});
}
);

it.each(eventTypes)(
"calls unregisterEventListener on the dispatcher when off is called with %s",
eventType => {
return import("./feature").then(({ getFeature }) => {
const callback = jest.fn();
dispatcher.unregisterEventListener = jest.fn();
const feature = getFeature(
mockFeatureType,
mockFeatureOptions,
mockFullscriptOptions,
dispatcher
);

feature.off(eventType, callback);
expect(dispatcher.unregisterEventListener).toHaveBeenCalledWith(
`${mockFrameId}.${eventType}`,
callback
);
});
}
);
});
});
1 change: 1 addition & 0 deletions src/feature/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type {
EventListenerPayload,
TreatmentPlanPayload,
PatientPayload,
OrderPayload,
EventType,
} from "./eventType";

Expand Down
13 changes: 8 additions & 5 deletions src/fullscriptJsValidator/fullscriptJsValidator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,14 @@ describe("fullscriptValidator", () => {
});

describe("validateFeatureEventType", () => {
it("does not throw an error if the featureEventType is valid", () => {
expect(() => {
validateFeatureEventType("treatmentPlan.activated");
}).not.toThrow();
});
it.each(["treatmentPlan.activated", "patient.selected", "order.checkoutStarted"])(
"does not throw an error if the featureEventType is %s",
eventType => {
expect(() => {
validateFeatureEventType(eventType);
}).not.toThrow();
}
);

it("throws an error if the featureEventType is not treatmentPlan or a string", () => {
const invalidEventType = "cancelled";
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type {
EventListenerCallback,
TreatmentPlanPayload,
PatientPayload,
OrderPayload,
PatientOptions,
FeatureOptions,
} from "./feature";
Expand Down
Loading