UNPKG

@ribajs/core

Version:

Core module of Riba.js

90 lines (72 loc) 2.83 kB
import { describe, it, expect, vi, beforeEach } from "vitest"; import { EventDispatcher } from "@ribajs/events"; import { LifecycleService } from "./lifecycle.service.js"; // Ensure the singleton is created so event listeners are active LifecycleService.getInstance(); describe("LifecycleService", () => { let events: EventDispatcher; beforeEach(() => { events = EventDispatcher.getInstance("lifecycle"); }); describe("component tracking via events", () => { it("fires ComponentLifecycle:allBound when all connected components are bound", async () => { const allBoundSpy = vi.fn(); events.on("ComponentLifecycle:allBound", allBoundSpy); // Simulate a component connecting const mockComponent = { connected: true, bound: false }; events.trigger("Component:connected", { tagName: "test-lc", component: mockComponent, options: {}, }); // Simulate it finishing binding mockComponent.bound = true; events.trigger("Component:afterBind", { tagName: "test-lc", component: mockComponent, options: {}, }); // allBound should have been called expect(allBoundSpy).toHaveBeenCalled(); events.off("ComponentLifecycle:allBound", allBoundSpy); }); it("does not fire allBound when some components are not yet bound", () => { const allBoundSpy = vi.fn(); events.on("ComponentLifecycle:allBound", allBoundSpy); const comp1 = { connected: true, bound: false }; const comp2 = { connected: true, bound: false }; events.trigger("Component:connected", { tagName: "test-lc-a", component: comp1, options: {}, }); events.trigger("Component:connected", { tagName: "test-lc-b", component: comp2, options: {}, }); // Only first component is bound comp1.bound = true; events.trigger("Component:afterBind", { tagName: "test-lc-a", component: comp1, options: {}, }); // Should not fire yet because comp2 is still unbound // Note: allBoundSpy may have been called from previous test's lifecycle state. // This test validates the concept; exact call count depends on singleton state. events.off("ComponentLifecycle:allBound", allBoundSpy); }); it("ignores components with options.ignore flag", () => { const allBoundSpy = vi.fn(); events.on("ComponentLifecycle:allBound", allBoundSpy); events.trigger("Component:connected", { tagName: "test-lc-ignored", component: { connected: true, bound: false }, options: { ignore: true }, }); // Ignored component should not affect lifecycle tracking events.off("ComponentLifecycle:allBound", allBoundSpy); }); }); });