forked from ZenUml/core
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor EventBus import to fix Vue Warning
- Loading branch information
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { vi } from "vitest"; | ||
import EventEmitter from "events"; | ||
import { EventBus, CustomEmit, TrackEvent } from "./EventBus"; | ||
|
||
describe("EventEmitter", () => { | ||
let eventEmitter: EventEmitter; | ||
|
||
beforeEach(() => { | ||
eventEmitter = new EventEmitter(); | ||
}); | ||
|
||
it("should allow multiple listeners for the same event", () => { | ||
const callback1 = vi.fn(); | ||
const callback2 = vi.fn(); | ||
eventEmitter.on("multipleListeners", callback1); | ||
eventEmitter.on("multipleListeners", callback2); | ||
eventEmitter.emit("multipleListeners", "testData"); | ||
expect(callback1).toHaveBeenCalledWith("testData"); | ||
expect(callback2).toHaveBeenCalledWith("testData"); | ||
}); | ||
|
||
it("should not call listeners for unrelated events", () => { | ||
const callback = vi.fn(); | ||
eventEmitter.on("event1", callback); | ||
eventEmitter.emit("event2", "testData"); | ||
expect(callback).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should handle emitting events with no listeners", () => { | ||
expect(() => { | ||
eventEmitter.emit("nonexistentEvent", "testData"); | ||
}).not.toThrow(); | ||
}); | ||
|
||
it("should handle removing a listener that was not added", () => { | ||
const callback = vi.fn(); | ||
expect(() => { | ||
eventEmitter.off("nonexistentEvent", callback); | ||
}).not.toThrow(); | ||
}); | ||
|
||
it("should allow adding and removing multiple listeners", () => { | ||
const callback1 = vi.fn(); | ||
const callback2 = vi.fn(); | ||
eventEmitter.on("multiEvent", callback1); | ||
eventEmitter.on("multiEvent", callback2); | ||
eventEmitter.off("multiEvent", callback1); | ||
eventEmitter.emit("multiEvent", "testData"); | ||
expect(callback1).not.toHaveBeenCalled(); | ||
expect(callback2).toHaveBeenCalledWith("testData"); | ||
}); | ||
}); | ||
|
||
describe("CustomEmit and TrackEvent", () => { | ||
let mockStore: any; | ||
|
||
beforeEach(() => { | ||
mockStore = { commit: vi.fn() }; | ||
vi.spyOn(EventBus, "emit"); | ||
}); | ||
|
||
it("should handle CustomEmit with undefined data", () => { | ||
CustomEmit(mockStore, "testEvent", undefined); | ||
expect(mockStore.commit).toHaveBeenCalledWith("eventEmit", { | ||
event: "testEvent", | ||
data: undefined, | ||
}); | ||
expect(EventBus.emit).toHaveBeenCalledWith("testEvent", undefined); | ||
}); | ||
|
||
it("should handle TrackEvent with complex label object", () => { | ||
const complexLabel = { id: 1, name: "Test" }; | ||
TrackEvent(mockStore, complexLabel, "testAction", "testCategory"); | ||
const expectedTrackData = { | ||
label: JSON.stringify(complexLabel), | ||
action: "testAction", | ||
category: "testCategory", | ||
}; | ||
expect(mockStore.commit).toHaveBeenCalledWith("eventEmit", { | ||
event: "trackEvent", | ||
data: expectedTrackData, | ||
}); | ||
expect(EventBus.emit).toHaveBeenCalledWith("trackEvent", expectedTrackData); | ||
}); | ||
}); |