|
| 1 | +/** |
| 2 | + * objectstack#28242 — a `page:header` action that declares `pressed` (the |
| 3 | + * reader's own state, `ActionSchema.pressed`) is a toggle button: drawn with |
| 4 | + * `aria-pressed="true"` and the pressed face while the predicate holds for |
| 5 | + * this record, `aria-pressed="false"` while it does not. An action that |
| 6 | + * declares no predicate carries no `aria-pressed` at all. |
| 7 | + * |
| 8 | + * Display only: a pressed action stays enabled and still dispatches, and a |
| 9 | + * predicate that cannot be evaluated draws the plain button (never a claim |
| 10 | + * that the reader acted) and says so once. |
| 11 | + */ |
| 12 | + |
| 13 | +import * as React from 'react'; |
| 14 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 15 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 16 | +import userEvent from '@testing-library/user-event'; |
| 17 | +import '@testing-library/jest-dom'; |
| 18 | +import { ComponentRegistry } from '@object-ui/core'; |
| 19 | +import { ActionProvider, RecordContextProvider } from '@object-ui/react'; |
| 20 | +// Registers `page:header` at module scope (object-ui/no-dynamic-import-in-test-hook). |
| 21 | +import '../renderers'; |
| 22 | + |
| 23 | +const HELPFUL = { |
| 24 | + name: 'mark_article_helpful', |
| 25 | + label: 'Helpful', |
| 26 | + type: 'script', |
| 27 | + locations: ['record_header'], |
| 28 | + pressed: "has(record.viewer) && record.viewer.vote == 'helpful'", |
| 29 | +}; |
| 30 | +const NOT_HELPFUL = { |
| 31 | + name: 'mark_article_not_helpful', |
| 32 | + label: 'Not helpful', |
| 33 | + type: 'script', |
| 34 | + locations: ['record_header'], |
| 35 | + pressed: { dialect: 'cel', source: "has(record.viewer) && record.viewer.vote == 'not_helpful'" }, |
| 36 | +}; |
| 37 | +const SHARE = { name: 'share_article', label: 'Share', type: 'script', locations: ['record_header'] }; |
| 38 | + |
| 39 | +function PageHeader({ schema }: { schema: any }) { |
| 40 | + const Component = ComponentRegistry.get('page:header'); |
| 41 | + if (!Component) throw new Error('page:header not registered'); |
| 42 | + // eslint-disable-next-line react-hooks/static-components -- ComponentRegistry.get returns a registered component (stable), not one created during render |
| 43 | + return <Component schema={schema} />; |
| 44 | +} |
| 45 | + |
| 46 | +function mount(actions: Record<string, unknown>[], record: Record<string, unknown>, handler?: (a: any) => any) { |
| 47 | + return render( |
| 48 | + <ActionProvider handlers={handler ? { 'vote-run': handler } : undefined}> |
| 49 | + <RecordContextProvider |
| 50 | + objectName="kb_article" |
| 51 | + recordId="a-1" |
| 52 | + data={{ id: 'a-1', ...record }} |
| 53 | + objectSchema={{ name: 'kb_article', label: 'Article' }} |
| 54 | + > |
| 55 | + <PageHeader schema={{ type: 'page:header', title: 'Article', maxVisible: 5, actions }} /> |
| 56 | + </RecordContextProvider> |
| 57 | + </ActionProvider>, |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +afterEach(() => vi.restoreAllMocks()); |
| 62 | + |
| 63 | +describe('page:header — `pressed` draws the reader\'s own state (objectstack#28242)', () => { |
| 64 | + it('the predicate holds: aria-pressed="true" and the pressed face; the sibling vote is unpressed', () => { |
| 65 | + mount([HELPFUL, NOT_HELPFUL], { viewer: { vote: 'helpful' } }); |
| 66 | + const helpful = screen.getByRole('button', { name: 'Helpful' }); |
| 67 | + expect(helpful).toHaveAttribute('aria-pressed', 'true'); |
| 68 | + expect(helpful.className).toContain('aria-pressed:bg-accent'); |
| 69 | + expect(screen.getByRole('button', { name: 'Not helpful' })).toHaveAttribute('aria-pressed', 'false'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('the envelope arm reads the same: the other vote is the pressed one', () => { |
| 73 | + mount([HELPFUL, NOT_HELPFUL], { viewer: { vote: 'not_helpful' } }); |
| 74 | + expect(screen.getByRole('button', { name: 'Helpful' })).toHaveAttribute('aria-pressed', 'false'); |
| 75 | + expect(screen.getByRole('button', { name: 'Not helpful' })).toHaveAttribute('aria-pressed', 'true'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('a row with no served block (fetched by another route) draws both unpressed', () => { |
| 79 | + mount([HELPFUL, NOT_HELPFUL], {}); |
| 80 | + expect(screen.getByRole('button', { name: 'Helpful' })).toHaveAttribute('aria-pressed', 'false'); |
| 81 | + expect(screen.getByRole('button', { name: 'Not helpful' })).toHaveAttribute('aria-pressed', 'false'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('display only: a pressed action stays enabled and still dispatches', async () => { |
| 85 | + const user = userEvent.setup(); |
| 86 | + const handler = vi.fn(async () => ({ success: true })); |
| 87 | + mount([{ ...HELPFUL, type: 'vote-run' }], { viewer: { vote: 'helpful' } }, handler); |
| 88 | + const helpful = screen.getByRole('button', { name: 'Helpful' }); |
| 89 | + expect(helpful).not.toBeDisabled(); |
| 90 | + await user.click(helpful); |
| 91 | + await waitFor(() => expect(handler).toHaveBeenCalledTimes(1)); |
| 92 | + expect((handler.mock.calls[0] as unknown[])[0]).toMatchObject({ name: 'mark_article_helpful' }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('a predicate that cannot be evaluated draws the plain button, and says so', () => { |
| 96 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 97 | + mount([{ ...HELPFUL, pressed: "record.viewer.vote == 'helpful'" }], {}); |
| 98 | + expect(screen.getByRole('button', { name: 'Helpful' })).toHaveAttribute('aria-pressed', 'false'); |
| 99 | + expect(warn.mock.calls.some((c) => String(c[0]).includes('pressed'))).toBe(true); |
| 100 | + }); |
| 101 | + |
| 102 | + it('control — an action that declares no predicate is a plain button: no aria-pressed, no face', () => { |
| 103 | + mount([SHARE], { viewer: { vote: 'helpful' } }); |
| 104 | + const share = screen.getByRole('button', { name: 'Share' }); |
| 105 | + expect(share).not.toHaveAttribute('aria-pressed'); |
| 106 | + expect(share.className).not.toContain('aria-pressed:'); |
| 107 | + }); |
| 108 | +}); |
0 commit comments