|
| 1 | +/*! |
| 2 | + * Copyright (c) Microsoft Corporation and contributors. All rights reserved. |
| 3 | + * Licensed under the MIT License. |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * The test cases below ensure that type can be successfully imported from all three packages — tree, core-interfaces, and fluid-framework and validate that the imports work as expected. |
| 8 | + * The plan is to remove types {@link @fluidframework/tree#Listenable}, {@link @fluidframework/tree#IsListener}, {@link @fluidframework/tree#Listeners} and {@link @fluidframework/tree#Off} |
| 9 | + * from `@fluidframework/tree` in Fluid Framework 3.0 and instead import them from `fluid-framework` or `@fluidframework/core-interfaces`, |
| 10 | + */ |
| 11 | + |
| 12 | +import { strict as assert } from "node:assert"; |
| 13 | + |
| 14 | +import { createEmitter } from "@fluid-internal/client-utils"; |
| 15 | +import type { Listenable as Listenable_Interfaces } from "@fluidframework/core-interfaces"; |
| 16 | +import type { Listenable as Listenable_Tree } from "@fluidframework/tree"; |
| 17 | +import type { Listenable as Listenable_Framework } from "fluid-framework"; |
| 18 | + |
| 19 | +describe("Test events type imports", () => { |
| 20 | + it("Trigger loaded event using core-interfaces import", async () => { |
| 21 | + const emitter = new MyCompositionClassInterfaces(); |
| 22 | + let count = 0; |
| 23 | + emitter.on("loaded", () => { |
| 24 | + count += 1; |
| 25 | + }); |
| 26 | + |
| 27 | + emitter.triggerLoad(); |
| 28 | + assert.strictEqual(count, 1); |
| 29 | + }); |
| 30 | + |
| 31 | + it("Trigger loaded event using tree import", async () => { |
| 32 | + const emitter = new MyCompositionClassTree(); |
| 33 | + let count = 1; |
| 34 | + emitter.on("loaded", () => { |
| 35 | + count += 1; |
| 36 | + }); |
| 37 | + |
| 38 | + emitter.triggerLoad(); |
| 39 | + assert.strictEqual(count, 2); |
| 40 | + }); |
| 41 | + |
| 42 | + it("Trigger loaded event using fluid-framework import", async () => { |
| 43 | + const emitter = new MyCompositionClassFramework(); |
| 44 | + let count = 2; |
| 45 | + emitter.on("loaded", () => { |
| 46 | + count += 1; |
| 47 | + }); |
| 48 | + |
| 49 | + emitter.triggerLoad(); |
| 50 | + assert.strictEqual(count, 3); |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +/** |
| 55 | + * A set of events with their handlers. |
| 56 | + */ |
| 57 | +interface MyEvents { |
| 58 | + loaded: () => void; |
| 59 | + computed: () => number; |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Example of composing over {@link CustomEventEmitter}. |
| 64 | + */ |
| 65 | +export class MyCompositionClassTree implements Listenable_Tree<MyEvents> { |
| 66 | + private readonly events = createEmitter<MyEvents>(); |
| 67 | + |
| 68 | + private load(): number[] { |
| 69 | + this.events.emit("loaded"); |
| 70 | + const results: number[] = this.events.emitAndCollect("computed"); |
| 71 | + return results; |
| 72 | + } |
| 73 | + |
| 74 | + public triggerLoad(): void { |
| 75 | + this.load(); |
| 76 | + } |
| 77 | + |
| 78 | + public on<K extends keyof MyEvents>(eventName: K, listener: MyEvents[K]): () => void { |
| 79 | + return this.events.on(eventName, listener); |
| 80 | + } |
| 81 | + |
| 82 | + public off<K extends keyof MyEvents>(eventName: K, listener: MyEvents[K]): void { |
| 83 | + return this.events.off(eventName, listener); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Example of composing over {@link CustomEventEmitter}. |
| 89 | + */ |
| 90 | +export class MyCompositionClassFramework implements Listenable_Framework<MyEvents> { |
| 91 | + private readonly events = createEmitter<MyEvents>(); |
| 92 | + |
| 93 | + private load(): number[] { |
| 94 | + this.events.emit("loaded"); |
| 95 | + const results: number[] = this.events.emitAndCollect("computed"); |
| 96 | + return results; |
| 97 | + } |
| 98 | + |
| 99 | + public triggerLoad(): void { |
| 100 | + this.load(); |
| 101 | + } |
| 102 | + |
| 103 | + public on<K extends keyof MyEvents>(eventName: K, listener: MyEvents[K]): () => void { |
| 104 | + return this.events.on(eventName, listener); |
| 105 | + } |
| 106 | + |
| 107 | + public off<K extends keyof MyEvents>(eventName: K, listener: MyEvents[K]): void { |
| 108 | + return this.events.off(eventName, listener); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * Example of composing over {@link CustomEventEmitter}. |
| 114 | + */ |
| 115 | +export class MyCompositionClassInterfaces implements Listenable_Interfaces<MyEvents> { |
| 116 | + private readonly events = createEmitter<MyEvents>(); |
| 117 | + |
| 118 | + private load(): number[] { |
| 119 | + this.events.emit("loaded"); |
| 120 | + const results: number[] = this.events.emitAndCollect("computed"); |
| 121 | + return results; |
| 122 | + } |
| 123 | + |
| 124 | + public triggerLoad(): void { |
| 125 | + this.load(); |
| 126 | + } |
| 127 | + |
| 128 | + public on<K extends keyof MyEvents>(eventName: K, listener: MyEvents[K]): () => void { |
| 129 | + return this.events.on(eventName, listener); |
| 130 | + } |
| 131 | + |
| 132 | + public off<K extends keyof MyEvents>(eventName: K, listener: MyEvents[K]): void { |
| 133 | + return this.events.off(eventName, listener); |
| 134 | + } |
| 135 | +} |
0 commit comments