-
Notifications
You must be signed in to change notification settings - Fork 0
Z flow edit cards fix & docs update #233
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
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
97 changes: 97 additions & 0 deletions
97
projects/ngx/declarative-ui/dashboard/dashboard/dashboard-element-methods.spec.ts
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 |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import { defineDashboardElementMethods } from './dashboard-element-methods'; | ||
| import type { Dashboard } from './dashboard.component'; | ||
|
|
||
| /** | ||
| * Builds a fake custom-element class plus a stub {@link Dashboard} instance, and | ||
| * wires the two together the way `@angular/elements` does (`ngElementStrategy. | ||
| * componentRef.instance`). `connectInstance` lets a test decide whether the | ||
| * element already has a live component behind it. | ||
| */ | ||
| function setup() { | ||
| class FakeElement { | ||
| ngElementStrategy?: { componentRef?: { instance?: Dashboard } }; | ||
| } | ||
| defineDashboardElementMethods(FakeElement as unknown as CustomElementConstructor); | ||
|
|
||
| const instance = { | ||
| // Mimic the "unsaved changes → dialog opened, do not navigate" path. | ||
| requestNavigation: vi.fn(() => false), | ||
| saveEdit: vi.fn(), | ||
| cancelEdit: vi.fn(), | ||
| confirmDiscard: vi.fn(), | ||
| onUnsavedNavSave: vi.fn(), | ||
| onUnsavedNavDiscard: vi.fn(), | ||
| onUnsavedNavCancel: vi.fn(), | ||
| } as unknown as Dashboard; | ||
|
|
||
| const element = new FakeElement() as FakeElement & | ||
| Record<string, (...args: unknown[]) => unknown>; | ||
|
|
||
| const connectInstance = () => { | ||
| element.ngElementStrategy = { componentRef: { instance } }; | ||
| }; | ||
|
|
||
| return { element, instance, connectInstance }; | ||
| } | ||
|
|
||
| const VOID_METHODS = [ | ||
| 'saveEdit', | ||
| 'cancelEdit', | ||
| 'confirmDiscard', | ||
| 'onUnsavedNavSave', | ||
| 'onUnsavedNavDiscard', | ||
| 'onUnsavedNavCancel', | ||
| ] as const; | ||
|
|
||
| describe('defineDashboardElementMethods', () => { | ||
| it('defines requestNavigation and every void handler on the prototype', () => { | ||
| const { element } = setup(); | ||
|
|
||
| expect(typeof element['requestNavigation']).toBe('function'); | ||
| for (const name of VOID_METHODS) { | ||
| expect(typeof element[name]).toBe('function'); | ||
| } | ||
| }); | ||
|
|
||
| describe('when the Angular component instance exists', () => { | ||
| it('delegates requestNavigation to the instance and returns its result', () => { | ||
| const { element, instance, connectInstance } = setup(); | ||
| connectInstance(); | ||
| const proceed = vi.fn(); | ||
|
|
||
| const result = element['requestNavigation'](proceed); | ||
|
|
||
| expect(instance.requestNavigation).toHaveBeenCalledWith(proceed); | ||
| // The stub reports "dialog opened", so the DOM call must NOT navigate. | ||
| expect(result).toBe(false); | ||
| expect(proceed).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it.each(VOID_METHODS)('delegates %s to the instance', (name) => { | ||
| const { element, instance, connectInstance } = setup(); | ||
| connectInstance(); | ||
|
|
||
| element[name](); | ||
|
|
||
| expect(instance[name]).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe('when the Angular component instance is not yet created', () => { | ||
| it('runs the requestNavigation callback synchronously and returns true', () => { | ||
| const { element } = setup(); | ||
| const proceed = vi.fn(); | ||
|
|
||
| const result = element['requestNavigation'](proceed); | ||
|
|
||
| expect(proceed).toHaveBeenCalledTimes(1); | ||
| expect(result).toBe(true); | ||
| }); | ||
|
|
||
| it.each(VOID_METHODS)('no-ops %s without throwing', (name) => { | ||
| const { element } = setup(); | ||
|
|
||
| expect(() => element[name]()).not.toThrow(); | ||
| }); | ||
| }); | ||
| }); |
65 changes: 65 additions & 0 deletions
65
projects/ngx/declarative-ui/dashboard/dashboard/dashboard-element-methods.ts
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 |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import type { Dashboard } from './dashboard.component'; | ||
|
|
||
| /** | ||
| * Public no-argument, `void`-returning handlers on {@link Dashboard} that we | ||
| * forward onto the custom element so non-Angular consumers can drive the | ||
| * edit-mode / unsaved-changes flow directly on the DOM node. | ||
| */ | ||
| const VOID_METHODS = [ | ||
| 'saveEdit', | ||
| 'cancelEdit', | ||
| 'confirmDiscard', | ||
| 'onUnsavedNavSave', | ||
| 'onUnsavedNavDiscard', | ||
| 'onUnsavedNavCancel', | ||
| ] as const satisfies readonly (keyof Dashboard)[]; | ||
|
|
||
| /** Reads the live Angular component instance backing an `@angular/elements` custom element. */ | ||
| function getInstance(element: unknown): Dashboard | undefined { | ||
| return ( | ||
| element as { | ||
| ngElementStrategy?: { componentRef?: { instance?: Dashboard } }; | ||
| } | ||
| ).ngElementStrategy?.componentRef?.instance; | ||
| } | ||
|
|
||
| /** | ||
| * `createCustomElement` only proxies `@Input()`/`output()` — public methods on | ||
| * the component class are NOT reachable from the DOM. This forwards the | ||
| * dashboard's public methods onto the custom-element prototype so that | ||
| * non-Angular consumers (UI5, plain JS, Luigi, etc.) can call them directly on | ||
| * the `<mfp-wc-dashboard>` DOM node. | ||
| * | ||
| * `requestNavigation` needs a synchronous fallback when the Angular component | ||
| * isn't created yet: run the navigation immediately (returning `true`) rather | ||
| * than silently blocking the user — this preserves the original, pre-guard | ||
| * behaviour. The remaining handlers no-op until the component exists. | ||
| */ | ||
| export function defineDashboardElementMethods( | ||
| elementCtor: CustomElementConstructor, | ||
| ): void { | ||
| const proto = elementCtor.prototype; | ||
|
|
||
| Object.defineProperty(proto, 'requestNavigation', { | ||
| value(proceed: () => void): boolean { | ||
| const instance = getInstance(this); | ||
| if (!instance) { | ||
| proceed(); | ||
| return true; | ||
| } | ||
| return instance.requestNavigation(proceed); | ||
| }, | ||
| configurable: true, | ||
| writable: true, | ||
| }); | ||
|
|
||
| for (const name of VOID_METHODS) { | ||
| Object.defineProperty(proto, name, { | ||
| value(): void { | ||
| getInstance(this)?.[name](); | ||
| }, | ||
| configurable: true, | ||
| writable: true, | ||
| }); | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Do not state that the static registration method is forwarded to the DOM node.
Dashboard.registerAngularComponents(types[])is static.defineDashboardElementMethods()only definesrequestNavigationand the void instance handlers onelementCtor.prototype. The current text promises thatelement.registerAngularComponents(...)is available, but it is not.Change “all of the methods above” to “the instance methods above”, or add an explicit static proxy.
🧰 Tools
🪛 LanguageTool
[style] ~222-~222: Consider removing “of” to be more concise
Context: ...p-wc-dashboard.js
) explicitly forwards all of the methods above onto`...(ALL_OF_THE)
🤖 Prompt for AI Agents