Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sonalideshpandemsft committed Dec 16, 2024
1 parent 27ec0d6 commit bc86952
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/apps/presence-tracker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"@fluidframework/driver-definitions": "workspace:~",
"@fluidframework/fluid-static": "workspace:~",
"@fluidframework/runtime-utils": "workspace:~",
"@fluidframework/tree": "workspace:~",
"fluid-framework": "workspace:~",
"process": "^0.11.10"
},
Expand Down
129 changes: 129 additions & 0 deletions examples/apps/presence-tracker/tests/events.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/

import { strict as assert } from "node:assert";

import { createEmitter } from "@fluid-internal/client-utils";
import type { Listenable as Listenable_Interfaces } from "@fluidframework/core-interfaces";
import type { Listenable as Listenable_Tree } from "@fluidframework/tree";
import type { Listenable as Listenable_Framework } from "fluid-framework";

describe("Test events type imports", () => {
it("Trigger loaded event using core-interfaces import", async () => {
const emitter = new MyCompositionClassInterfaces();
let count = 0;
emitter.on("loaded", () => {
count += 1;
});

emitter.triggerLoad();
assert.strictEqual(count, 1);
});

it("Trigger loaded event using tree import", async () => {
const emitter = new MyCompositionClassTree();
let count = 1;
emitter.on("loaded", () => {
count += 1;
});

emitter.triggerLoad();
assert.strictEqual(count, 2);
});

it("Trigger loaded event using fluid-framework import", async () => {
const emitter = new MyCompositionClassFramework();
let count = 2;
emitter.on("loaded", () => {
count += 1;
});

emitter.triggerLoad();
assert.strictEqual(count, 3);
});
});

/**
* A set of events with their handlers.
*/
interface MyEvents {
loaded: () => void;
computed: () => number;
}

/**
* Example of composing over {@link CustomEventEmitter}.
*/
export class MyCompositionClassTree implements Listenable_Tree<MyEvents> {
private readonly events = createEmitter<MyEvents>();

private load(): number[] {
this.events.emit("loaded");
const results: number[] = this.events.emitAndCollect("computed");
return results;
}

public triggerLoad(): void {
this.load();
}

public on<K extends keyof MyEvents>(eventName: K, listener: MyEvents[K]): () => void {
return this.events.on(eventName, listener);
}

public off<K extends keyof MyEvents>(eventName: K, listener: MyEvents[K]): void {
return this.events.off(eventName, listener);
}
}

/**
* Example of composing over {@link CustomEventEmitter}.
*/
export class MyCompositionClassFramework implements Listenable_Framework<MyEvents> {
private readonly events = createEmitter<MyEvents>();

private load(): number[] {
this.events.emit("loaded");
const results: number[] = this.events.emitAndCollect("computed");
return results;
}

public triggerLoad(): void {
this.load();
}

public on<K extends keyof MyEvents>(eventName: K, listener: MyEvents[K]): () => void {
return this.events.on(eventName, listener);
}

public off<K extends keyof MyEvents>(eventName: K, listener: MyEvents[K]): void {
return this.events.off(eventName, listener);
}
}

/**
* Example of composing over {@link CustomEventEmitter}.
*/
export class MyCompositionClassInterfaces implements Listenable_Interfaces<MyEvents> {
private readonly events = createEmitter<MyEvents>();

private load(): number[] {
this.events.emit("loaded");
const results: number[] = this.events.emitAndCollect("computed");
return results;
}

public triggerLoad(): void {
this.load();
}

public on<K extends keyof MyEvents>(eventName: K, listener: MyEvents[K]): () => void {
return this.events.on(eventName, listener);
}

public off<K extends keyof MyEvents>(eventName: K, listener: MyEvents[K]): void {
return this.events.off(eventName, listener);
}
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bc86952

Please sign in to comment.