Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 368
Add addEventListener/removeEventListener with DOM-model on* semantics#573
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1221,4 +1221,180 @@ describe("isToolVisibilityAppOnly", () => { | ||
| expect(isToolVisibilityAppOnly(tool)).toBe(false); | ||
| }); | ||
| }); | ||
| describe("addEventListener / removeEventListener", () => { | ||
| let app: App; | ||
| let bridge: AppBridge; | ||
| let appTransport: InMemoryTransport; | ||
| let bridgeTransport: InMemoryTransport; | ||
| beforeEach(async () => { | ||
| [appTransport, bridgeTransport] = InMemoryTransport.createLinkedPair(); | ||
| app = new App(testAppInfo, {}, { autoResize: false }); | ||
| bridge = new AppBridge( | ||
| createMockClient() as Client, | ||
| testHostInfo, | ||
| testHostCapabilities, | ||
| ); | ||
| await bridge.connect(bridgeTransport); | ||
| }); | ||
| afterEach(async () => { | ||
| await appTransport.close(); | ||
| await bridgeTransport.close(); | ||
| }); | ||
| it("App.addEventListener fires multiple listeners for the same event", async () => { | ||
| const a: unknown[] = []; | ||
| const b: unknown[] = []; | ||
| app.addEventListener("hostcontextchanged", (p) => a.push(p)); | ||
| app.addEventListener("hostcontextchanged", (p) => b.push(p)); | ||
| await app.connect(appTransport); | ||
| bridge.setHostContext({ theme: "dark" }); | ||
| await flush(); | ||
| expect(a).toEqual([{ theme: "dark" }]); | ||
| expect(b).toEqual([{ theme: "dark" }]); | ||
| }); | ||
| it("App notification setters replace (DOM onclick model)", async () => { | ||
| const a: unknown[] = []; | ||
| const b: unknown[] = []; | ||
| const first = (p: unknown) => a.push(p); | ||
| app.ontoolinput = first; | ||
| expect(app.ontoolinput).toBe(first); | ||
| app.ontoolinput = (p) => b.push(p); | ||
| await app.connect(appTransport); | ||
| await bridge.sendToolInput({ arguments: { x: 1 } }); | ||
| await flush(); | ||
| // Second assignment replaced the first (like el.onclick) | ||
| expect(a).toEqual([]); | ||
| expect(b).toEqual([{ arguments: { x: 1 } }]); | ||
| }); | ||
| it("App notification setter coexists with addEventListener", async () => { | ||
| const a: unknown[] = []; | ||
| const b: unknown[] = []; | ||
| app.ontoolinput = (p) => a.push(p); | ||
| app.addEventListener("toolinput", (p) => b.push(p)); | ||
| await app.connect(appTransport); | ||
| await bridge.sendToolInput({ arguments: { x: 1 } }); | ||
| await flush(); | ||
| // Both the on* handler and addEventListener listener fire | ||
| expect(a).toEqual([{ arguments: { x: 1 } }]); | ||
| expect(b).toEqual([{ arguments: { x: 1 } }]); | ||
| }); | ||
| it("App notification getter returns the on* handler", () => { | ||
| expect(app.ontoolinput).toBeUndefined(); | ||
| const handler = () => {}; | ||
| app.ontoolinput = handler; | ||
| expect(app.ontoolinput).toBe(handler); | ||
| }); | ||
| it("App notification setter can be cleared with undefined", async () => { | ||
| const a: unknown[] = []; | ||
| app.ontoolinput = (p) => a.push(p); | ||
github-code-quality[bot] marked this conversation as resolved.
Fixed
Uh oh!There was an error while loading. Please reload this page. | ||
| expect(app.ontoolinput).toBeDefined(); | ||
| app.ontoolinput = undefined; | ||
| await app.connect(appTransport); | ||
| await bridge.sendToolInput({ arguments: { x: 1 } }); | ||
| await flush(); | ||
| expect(a).toEqual([]); | ||
| expect(app.ontoolinput).toBeUndefined(); | ||
| }); | ||
| it("App.removeEventListener stops a listener from firing", async () => { | ||
| const a: unknown[] = []; | ||
| const listener = (p: unknown) => a.push(p); | ||
| app.addEventListener("toolinput", listener); | ||
| app.removeEventListener("toolinput", listener); | ||
| await app.connect(appTransport); | ||
| await bridge.sendToolInput({ arguments: {} }); | ||
| await flush(); | ||
| expect(a).toEqual([]); | ||
| }); | ||
| it("App.onEventDispatch merges hostcontext before listeners fire", async () => { | ||
| let seen: unknown; | ||
| app.addEventListener("hostcontextchanged", () => { | ||
| seen = app.getHostContext(); | ||
| }); | ||
| await app.connect(appTransport); | ||
| bridge.setHostContext({ theme: "dark" }); | ||
| await flush(); | ||
| expect(seen).toEqual({ theme: "dark" }); | ||
| }); | ||
| it("AppBridge.addEventListener fires multiple listeners", async () => { | ||
| let a = 0; | ||
| let b = 0; | ||
| bridge.addEventListener("initialized", () => a++); | ||
| bridge.addEventListener("initialized", () => b++); | ||
| await app.connect(appTransport); | ||
| expect(a).toBe(1); | ||
| expect(b).toBe(1); | ||
| }); | ||
| it("on* request setters have replace semantics (no throw)", () => { | ||
| app.onteardown = async () => ({}); | ||
| expect(() => { | ||
| app.onteardown = async () => ({}); | ||
| }).not.toThrow(); | ||
| }); | ||
| it("on* request setters have getters", () => { | ||
| expect(app.onteardown).toBeUndefined(); | ||
| const handler = async () => ({}); | ||
| app.onteardown = handler; | ||
| expect(app.onteardown).toBe(handler); | ||
| }); | ||
| it("direct setRequestHandler throws when called twice", () => { | ||
| const bridge2 = new AppBridge( | ||
| createMockClient() as Client, | ||
| testHostInfo, | ||
| testHostCapabilities, | ||
| ); | ||
| bridge2.setRequestHandler( | ||
| // @ts-expect-error — exercising throw path with raw schema | ||
| { shape: { method: { value: "test/method" } } }, | ||
| () => ({}), | ||
| ); | ||
| expect(() => { | ||
| bridge2.setRequestHandler( | ||
| // @ts-expect-error — exercising throw path with raw schema | ||
| { shape: { method: { value: "test/method" } } }, | ||
| () => ({}), | ||
| ); | ||
| }).toThrow(/already registered/); | ||
| }); | ||
| it("direct setNotificationHandler throws for event-mapped methods", () => { | ||
| const app2 = new App(testAppInfo, {}, { autoResize: false }); | ||
| app2.addEventListener("toolinput", () => {}); | ||
| expect(() => { | ||
| app2.setNotificationHandler( | ||
| // @ts-expect-error — exercising throw path with raw schema | ||
| { | ||
| shape: { method: { value: "ui/notifications/tool-input" } }, | ||
| }, | ||
| () => {}, | ||
| ); | ||
| }).toThrow(/already registered/); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.