|
37 | 37 | * read for stored documents. |
38 | 38 | */ |
39 | 39 |
|
40 | | -import { describe, it, expect, vi, beforeEach, afterEach, type Mock } from 'vitest'; |
| 40 | +import { describe, it, expect, vi, beforeEach, afterEach, type Mock, type MockInstance } from 'vitest'; |
41 | 41 | import { render, screen, fireEvent, waitFor, within, cleanup } from '@testing-library/react'; |
42 | 42 | import '@testing-library/jest-dom'; |
43 | 43 | import React from 'react'; |
@@ -389,3 +389,168 @@ describe('action:menu — the member shape of `actions` (the MEMBER-PIN directio |
389 | 389 | expect(run.mock.calls[0][0]).toMatchObject({ type: 'run', name: 'go', target: 'member-target', objectName: 'account' }); |
390 | 390 | }); |
391 | 391 | }); |
| 392 | + |
| 393 | +// ── objectstack#37266 — the shared label guard ────────────────────────────── |
| 394 | +// |
| 395 | +// `action:menu` and `action:group` read every label, their own and each |
| 396 | +// member's, through `renderableLabel` (`../renderable-label.ts`), the guard |
| 397 | +// `action:button` reads through (ruling A on objectstack#36717). A locale map |
| 398 | +// reaches a renderer only from metadata that skipped `validateComponentProps`; |
| 399 | +// it renders no label text, prints no `[object Object]`, is not translated, |
| 400 | +// and the menu no longer throws. Text and accessible names only: both |
| 401 | +// renderers spread undeclared node keys onto the DOM as attributes. |
| 402 | + |
| 403 | +describe('action:group — a non-string label renders no text and never throws (objectstack#37266)', () => { |
| 404 | + // Scoped to this describe: the label pins read every `console.warn`, and the |
| 405 | + // file's own `beforeEach` spies only on `console.error`. |
| 406 | + const LOCALE_MAP = { en: 'Approve', 'zh-CN': '批准' }; |
| 407 | + let warn: MockInstance<(...args: unknown[]) => void>; |
| 408 | + beforeEach(() => { |
| 409 | + warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 410 | + }); |
| 411 | + afterEach(() => { |
| 412 | + warn.mockRestore(); |
| 413 | + }); |
| 414 | + const labelWarnings = () => warn.mock.calls.filter((args) => String(args[0]).includes('non-string label')); |
| 415 | + const assertNothingLeaked = () => { |
| 416 | + // Text and accessible names only: the host's undeclared-prop spread writes every |
| 417 | + // node key onto the DOM as an attribute (`actions="[object Object]"` on main for |
| 418 | + // any group), which is not a label read and is out of this guard's reach. |
| 419 | + expect(document.body.textContent).not.toContain('[object Object]'); |
| 420 | + for (const el of document.querySelectorAll('[aria-label]')) { |
| 421 | + expect(el.getAttribute('aria-label')).not.toContain('[object Object]'); |
| 422 | + } |
| 423 | + expect(document.body.textContent).not.toContain('批准'); |
| 424 | + expect(document.body.textContent).not.toContain('Approve'); |
| 425 | + }; |
| 426 | + |
| 427 | + it('inline: a locale-map member label renders a button with no text, which still runs with no label', async () => { |
| 428 | + mount({ |
| 429 | + type: 'action:group', |
| 430 | + id: 'group_label_inline', |
| 431 | + actions: [{ name: 'approve', label: LOCALE_MAP, type: 'run' }], |
| 432 | + }); |
| 433 | + const button = screen.getByRole('button'); |
| 434 | + expect(button.textContent).toBe(''); |
| 435 | + assertNothingLeaked(); |
| 436 | + |
| 437 | + fireEvent.click(button); |
| 438 | + await waitFor(() => expect(run).toHaveBeenCalledTimes(1)); |
| 439 | + expect((run.mock.calls[0][0] as ActionDef).label).toBeUndefined(); |
| 440 | + |
| 441 | + const warned = labelWarnings().map((args) => String(args[0])); |
| 442 | + expect(warned).toHaveLength(1); |
| 443 | + expect(warned[0]).toContain('action:group "approve"'); |
| 444 | + }); |
| 445 | + |
| 446 | + it('dropdown: a locale-map label on the group and on a member falls back to the defaults', async () => { |
| 447 | + mount({ |
| 448 | + type: 'action:group', |
| 449 | + id: 'group_label_dropdown', |
| 450 | + display: 'dropdown', |
| 451 | + label: LOCALE_MAP, |
| 452 | + actions: [{ name: 'approve', label: LOCALE_MAP, type: 'run' }], |
| 453 | + }); |
| 454 | + // The trigger's unlabelled default, and the member's `name`. |
| 455 | + const trigger = screen.getByRole('button'); |
| 456 | + expect(trigger).toHaveTextContent('Actions'); |
| 457 | + await openMenu(trigger); |
| 458 | + expect(screen.getByRole('menuitem')).toHaveTextContent('approve'); |
| 459 | + assertNothingLeaked(); |
| 460 | + |
| 461 | + const warned = labelWarnings().map((args) => String(args[0])); |
| 462 | + expect(warned.some((m) => m.includes('action:group "group_label_dropdown"'))).toBe(true); |
| 463 | + }); |
| 464 | + |
| 465 | + it('CONTROL: string labels render as written in both display modes', async () => { |
| 466 | + mount({ |
| 467 | + type: 'action:group', |
| 468 | + id: 'group_label_string', |
| 469 | + actions: [{ name: 'approve', label: 'Approve', type: 'run' }], |
| 470 | + }); |
| 471 | + expect(screen.getByRole('button')).toHaveTextContent('Approve'); |
| 472 | + cleanup(); |
| 473 | + |
| 474 | + mount({ |
| 475 | + type: 'action:group', |
| 476 | + id: 'group_label_string_dd', |
| 477 | + display: 'dropdown', |
| 478 | + label: 'Row', |
| 479 | + actions: [{ name: 'approve', label: 'Approve', type: 'run' }], |
| 480 | + }); |
| 481 | + const trigger = screen.getByRole('button'); |
| 482 | + expect(trigger).toHaveTextContent('Row'); |
| 483 | + await openMenu(trigger); |
| 484 | + expect(screen.getByRole('menuitem')).toHaveTextContent('Approve'); |
| 485 | + expect(labelWarnings()).toHaveLength(0); |
| 486 | + }); |
| 487 | +}); |
| 488 | + |
| 489 | +describe('action:menu — a non-string label renders no text and never throws (objectstack#37266)', () => { |
| 490 | + // Scoped to this describe: the label pins read every `console.warn`, and the |
| 491 | + // file's own `beforeEach` spies only on `console.error`. |
| 492 | + const LOCALE_MAP = { en: 'Approve', 'zh-CN': '批准' }; |
| 493 | + let warn: MockInstance<(...args: unknown[]) => void>; |
| 494 | + beforeEach(() => { |
| 495 | + warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 496 | + }); |
| 497 | + afterEach(() => { |
| 498 | + warn.mockRestore(); |
| 499 | + }); |
| 500 | + const labelWarnings = () => warn.mock.calls.filter((args) => String(args[0]).includes('non-string label')); |
| 501 | + |
| 502 | + it('a locale-map label on the menu and on a member: no throw, no `[object Object]`, nothing translated', async () => { |
| 503 | + mount({ |
| 504 | + type: 'action:menu', |
| 505 | + id: 'menu_label_guard', |
| 506 | + label: LOCALE_MAP, |
| 507 | + actions: [{ name: 'approve', label: LOCALE_MAP, type: 'run' }], |
| 508 | + }); |
| 509 | + |
| 510 | + // The trigger falls back to its default accessible name, with no label text. |
| 511 | + const trigger = screen.getByRole('button', { name: 'More actions' }); |
| 512 | + expect(trigger.textContent).toBe(''); |
| 513 | + |
| 514 | + // The member falls back to its `name`, as an unlabelled member always has. |
| 515 | + const menu = await openMenu(trigger); |
| 516 | + const item = screen.getByRole('menuitem'); |
| 517 | + expect(item).toHaveTextContent('approve'); |
| 518 | + expect(menu.textContent).not.toContain('[object Object]'); |
| 519 | + // Text and accessible names only: the host's undeclared-prop spread writes every |
| 520 | + // node key onto the DOM as an attribute (`actions="[object Object]"` on main for |
| 521 | + // any menu), which is not a label read and is out of this guard's reach. |
| 522 | + expect(document.body.textContent).not.toContain('[object Object]'); |
| 523 | + expect(trigger.getAttribute('aria-label')).toBe('More actions'); |
| 524 | + expect(document.body.textContent).not.toContain('批准'); |
| 525 | + expect(document.body.textContent).not.toContain('Approve'); |
| 526 | + |
| 527 | + // The member still runs, and the runner is handed no label. |
| 528 | + fireEvent.click(item); |
| 529 | + await waitFor(() => expect(run).toHaveBeenCalledTimes(1)); |
| 530 | + expect((run.mock.calls[0][0] as ActionDef).label).toBeUndefined(); |
| 531 | + |
| 532 | + // One development warning per component: the menu, and its member. |
| 533 | + const warned = labelWarnings().map((args) => String(args[0])); |
| 534 | + expect(warned).toHaveLength(2); |
| 535 | + expect(warned.some((m) => m.includes('action:menu "menu_label_guard"'))).toBe(true); |
| 536 | + expect(warned.some((m) => m.includes('action:menu "approve"'))).toBe(true); |
| 537 | + }); |
| 538 | + |
| 539 | + it('CONTROL: string labels render as written, and the runner is handed the member label', async () => { |
| 540 | + mount({ |
| 541 | + type: 'action:menu', |
| 542 | + id: 'menu_label_string', |
| 543 | + label: 'Row', |
| 544 | + actions: [{ name: 'approve', label: 'Approve', type: 'run' }], |
| 545 | + }); |
| 546 | + const trigger = screen.getByRole('button', { name: 'Row' }); |
| 547 | + expect(trigger).toHaveTextContent('Row'); |
| 548 | + await openMenu(trigger); |
| 549 | + const item = screen.getByRole('menuitem'); |
| 550 | + expect(item).toHaveTextContent('Approve'); |
| 551 | + fireEvent.click(item); |
| 552 | + await waitFor(() => expect(run).toHaveBeenCalledTimes(1)); |
| 553 | + expect((run.mock.calls[0][0] as ActionDef).label).toBe('Approve'); |
| 554 | + expect(labelWarnings()).toHaveLength(0); |
| 555 | + }); |
| 556 | +}); |
0 commit comments