Skip to content

Commit

Permalink
Fix shape of data events
Browse files Browse the repository at this point in the history
  • Loading branch information
tysoncadenhead committed Aug 6, 2024
1 parent fdfded2 commit 23c8998
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ampt-data-mock",
"version": "1.1.1",
"version": "1.1.2",
"description": "",
"main": "lib/index.js",
"scripts": {
Expand Down
35 changes: 13 additions & 22 deletions src/__tests__/dataEvents.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
import { data, reset } from "../data";

import { handledEvent } from "../helpers";

describe("data events", () => {
afterEach(reset);

it("Should listen to all events", () => {
const spy = jest.fn();
data.on("*", spy);
data.set("FOO:1", { id: "1" });
expect(spy).toHaveBeenCalledWith({
name: "created",
item: { id: "1" },
});
expect(spy).toHaveBeenCalledWith(handledEvent("created", { id: "1" }));
});

it("Should listen to created events", () => {
const spy = jest.fn();
data.on("created", spy);
data.set("FOO:1", { id: "1" });
expect(spy).toHaveBeenCalledWith({
name: "created",
item: { id: "1" },
});
expect(spy).toHaveBeenCalledWith(handledEvent("created", { id: "1" }));
});

it("Should not emit created events to update listeners", () => {
Expand All @@ -36,11 +32,9 @@ describe("data events", () => {
data.set("FOO:1", { id: "1" });
data.set("FOO:1", { id: "2" });

expect(spy).toHaveBeenCalledWith({
name: "updated",
previous: { id: "1" },
item: { id: "2" },
});
expect(spy).toHaveBeenCalledWith(
handledEvent("updated", { id: "2" }, { id: "1" })
);
});

it("Should listen to deleted events", () => {
Expand All @@ -49,10 +43,9 @@ describe("data events", () => {
data.set("FOO:1", { id: "1" });
data.remove("FOO:1");

expect(spy).toHaveBeenLastCalledWith({
name: "deleted",
previous: { id: "1" },
});
expect(spy).toHaveBeenLastCalledWith(
handledEvent("deleted", null, { id: "1" })
);
});

it("Should listen for specific paths", () => {
Expand All @@ -62,7 +55,8 @@ describe("data events", () => {

expect(spy).toHaveBeenCalledWith({
name: "created",
item: { id: "1" },
item: { value: { id: "1" } },
previous: {},
});
});

Expand All @@ -71,9 +65,6 @@ describe("data events", () => {
data.on("*:FOO:BAR:1", spy);
data.set("FOO:BAR:1", { id: "1" });

expect(spy).toHaveBeenCalledWith({
name: "created",
item: { id: "1" },
});
expect(spy).toHaveBeenCalledWith(handledEvent("created", { id: "1" }));
});
});
12 changes: 11 additions & 1 deletion src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,17 @@ const emitToSubscribers = (eventType: string, path: string, data: any) => {
return false;
})
.forEach((subscriber) => {
store.subscriptions[subscriber].forEach((fn) => fn(data));
store.subscriptions[subscriber].forEach((fn) =>
fn({
item: {
value: data.item,
},
previous: {
value: data.previous,
},
name: data.name,
})
);
});
};

Expand Down
19 changes: 19 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
interface IHandledEvent {
name: string;
value: any;
previousValue: any;
}

export const handledEvent = (
name: string,
value?: any,
previousValue?: any
) => {
return {
name,
item: { value: value || undefined },
previous: {
value: previousValue || undefined,
},
};
};
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./data";
export * from "./events";
export * from "./helpers";

0 comments on commit 23c8998

Please sign in to comment.