|
| 1 | +// :copyright: Copyright (c) 2023 ftrack |
| 2 | + |
| 3 | +import { Event } from "../source/event"; |
| 4 | +import { describe, expect, it } from "vitest"; |
| 5 | + |
| 6 | +describe("Event class", () => { |
| 7 | + it("should initialize with correct topic and data", () => { |
| 8 | + const event = new Event("testTopic", { key: "value" }); |
| 9 | + const data = event.getData(); |
| 10 | + expect(data.topic).toBe("testTopic"); |
| 11 | + expect(data.data).toEqual({ key: "value" }); |
| 12 | + }); |
| 13 | + |
| 14 | + it("should have default properties", () => { |
| 15 | + const event = new Event("testTopic", { key: "value" }); |
| 16 | + const data = event.getData(); |
| 17 | + expect(data.target).toBe(""); |
| 18 | + expect(data.inReplyToEvent).toBeNull(); |
| 19 | + }); |
| 20 | + |
| 21 | + it("should set properties from options", () => { |
| 22 | + const event = new Event( |
| 23 | + "testTopic", |
| 24 | + { key: "value" }, |
| 25 | + { target: "sampleTarget", customOption: "customValue" } |
| 26 | + ); |
| 27 | + const data = event.getData(); |
| 28 | + expect(data.target).toBe("sampleTarget"); |
| 29 | + expect(data.customOption).toBe("customValue"); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should generate unique UUID", () => { |
| 33 | + const event1 = new Event("testTopic", { key: "value" }); |
| 34 | + const event2 = new Event("testTopic", { key: "value" }); |
| 35 | + const data1 = event1.getData(); |
| 36 | + const data2 = event2.getData(); |
| 37 | + const uuidRegexExp = |
| 38 | + /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/gi; |
| 39 | + expect(data1.id).not.toBe(data2.id); |
| 40 | + expect(uuidRegexExp.test(data1.id)).toBe(true); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should add source to event data", () => { |
| 44 | + const event = new Event("testTopic", { key: "value" }); |
| 45 | + const source = { |
| 46 | + id: "testId", |
| 47 | + applicationId: "testApplicationId", |
| 48 | + user: { |
| 49 | + username: "testUser", |
| 50 | + }, |
| 51 | + }; |
| 52 | + event.addSource(source); |
| 53 | + const data = event.getData(); |
| 54 | + expect(data.source).toBe(source); |
| 55 | + }); |
| 56 | + describe("prepareSource Method", () => { |
| 57 | + it("should prepare and add source to event data", () => { |
| 58 | + const event = new Event("testTopic", { key: "value" }); |
| 59 | + event.prepareSource({ newKey: "newValue" }); |
| 60 | + const data = event.getData(); |
| 61 | + expect(data.source).toEqual({ newKey: "newValue" }); |
| 62 | + }); |
| 63 | + |
| 64 | + it("should not override existing source when preparing a new source", () => { |
| 65 | + const event = new Event( |
| 66 | + "testTopic", |
| 67 | + { key: "value" }, |
| 68 | + { source: { oldKey: "oldValue" } } |
| 69 | + ); |
| 70 | + event.prepareSource({ oldKey: "newValue", newKey: "newValue" }); |
| 71 | + const data = event.getData(); |
| 72 | + expect(data.source).toEqual({ |
| 73 | + oldKey: "oldValue", |
| 74 | + newKey: "newValue", |
| 75 | + }); |
| 76 | + }); |
| 77 | + it("should handle source undefined", () => { |
| 78 | + const event = new Event( |
| 79 | + "testTopic", |
| 80 | + { key: "value" }, |
| 81 | + { source: undefined } |
| 82 | + ); |
| 83 | + event.prepareSource({ newKey: "newValue" }); |
| 84 | + const data = event.getData(); |
| 85 | + expect(data.source).toEqual({ |
| 86 | + newKey: "newValue", |
| 87 | + }); |
| 88 | + }); |
| 89 | + it("Prepare should handle source null", () => { |
| 90 | + const event = new Event("testTopic", { key: "value" }, { source: null }); |
| 91 | + event.prepareSource({ newKey: "newValue" }); |
| 92 | + const data = event.getData(); |
| 93 | + expect(data.source).toEqual({ |
| 94 | + newKey: "newValue", |
| 95 | + }); |
| 96 | + }); |
| 97 | + }); |
| 98 | +}); |
0 commit comments