Files
bodyshop/server/feature-flags/socket-events.test.js

61 lines
1.8 KiB
JavaScript

import { describe, expect, it, vi } from "vitest";
import { createRequire } from "module";
const require = createRequire(import.meta.url);
const {
FEATURE_FLAGS_CHANGED_EVENT,
createFeatureFlagsChangedPayload,
emitFeatureFlagsChanged
} = require("./socket-events");
describe("feature flag socket events", () => {
it("creates a global payload when no bodyshop id is provided", () => {
expect(createFeatureFlagsChangedPayload({ source: "admin", table: "feature_flags", name: "Demo" })).toEqual(
expect.objectContaining({
bodyshopId: null,
name: "Demo",
scope: "global",
source: "admin",
table: "feature_flags"
})
);
});
it("emits bodyshop-scoped changes to the bodyshop room", () => {
const emit = vi.fn();
const req = {
ioHelpers: {
getBodyshopRoom: vi.fn(() => "bodyshop-room-shop-1")
},
ioRedis: {
to: vi.fn(() => ({ emit }))
}
};
const payload = emitFeatureFlagsChanged({
req,
bodyshopId: "shop-1",
source: "hasura",
table: "bodyshop_feature_flags",
name: "Demo"
});
expect(req.ioRedis.to).toHaveBeenCalledWith("bodyshop-room-shop-1");
expect(emit).toHaveBeenCalledWith(FEATURE_FLAGS_CHANGED_EVENT, payload);
expect(payload).toEqual(expect.objectContaining({ bodyshopId: "shop-1", scope: "bodyshop" }));
});
it("broadcasts global changes to all sockets", () => {
const req = {
ioRedis: {
emit: vi.fn()
}
};
const payload = emitFeatureFlagsChanged({ req, source: "admin", table: "feature_flags" });
expect(req.ioRedis.emit).toHaveBeenCalledWith(FEATURE_FLAGS_CHANGED_EVENT, payload);
expect(payload).toEqual(expect.objectContaining({ bodyshopId: null, scope: "global" }));
});
});