|
| 1 | +/** |
| 2 | + * objectstack#28242 — a `DeclaredActionsBar` action that declares `pressed` |
| 3 | + * (the reader's own state, `ActionSchema.pressed`) is a toggle button: drawn |
| 4 | + * with `aria-pressed="true"` and the pressed face while the predicate holds |
| 5 | + * for 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 | + * Doubles as the sibling suites (`DeclaredActionsBar.disabledReason-26619.test.tsx`) |
| 13 | + * do: only the action dispatch and the console runtime shell. |
| 14 | + */ |
| 15 | + |
| 16 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 17 | +import { render, screen } from '@testing-library/react'; |
| 18 | +import userEvent from '@testing-library/user-event'; |
| 19 | +import '@testing-library/jest-dom'; |
| 20 | +import React from 'react'; |
| 21 | + |
| 22 | +const executeSpy = vi.fn().mockResolvedValue({ success: true }); |
| 23 | + |
| 24 | +vi.mock('@object-ui/react', async (importOriginal) => { |
| 25 | + const actual = await importOriginal<typeof import('@object-ui/react')>(); |
| 26 | + return { |
| 27 | + ...actual, |
| 28 | + ActionProvider: ({ children }: { children: React.ReactNode }) => <>{children}</>, |
| 29 | + useAction: () => ({ execute: executeSpy }), |
| 30 | + }; |
| 31 | +}); |
| 32 | +vi.mock('../../hooks/useConsoleActionRuntime', () => ({ |
| 33 | + useConsoleActionRuntime: () => ({ actionProviderProps: {}, dialogs: null }), |
| 34 | +})); |
| 35 | +vi.mock('../../providers/AdapterProvider', () => ({ useAdapter: () => ({}) })); |
| 36 | +vi.mock('../../utils/getIcon', () => ({ getIcon: () => () => null })); |
| 37 | + |
| 38 | +import { DeclaredActionsBar } from '../DeclaredActionsBar'; |
| 39 | + |
| 40 | +const HELPFUL = { |
| 41 | + name: 'mark_article_helpful', |
| 42 | + type: 'script', |
| 43 | + label: 'Helpful', |
| 44 | + locations: ['record_section'], |
| 45 | + pressed: "has(record.viewer) && record.viewer.vote == 'helpful'", |
| 46 | +}; |
| 47 | +const NOT_HELPFUL = { |
| 48 | + name: 'mark_article_not_helpful', |
| 49 | + type: 'script', |
| 50 | + label: 'Not helpful', |
| 51 | + locations: ['record_section'], |
| 52 | + pressed: { dialect: 'cel', source: "has(record.viewer) && record.viewer.vote == 'not_helpful'" }, |
| 53 | +}; |
| 54 | +const SHARE = { name: 'share_article', type: 'script', label: 'Share', locations: ['record_section'] }; |
| 55 | + |
| 56 | +function mount(record: Record<string, unknown>, actions: Record<string, unknown>[] = [HELPFUL, NOT_HELPFUL]) { |
| 57 | + return render( |
| 58 | + <DeclaredActionsBar |
| 59 | + objectName="kb_article" |
| 60 | + record={{ id: 'a-1', ...record }} |
| 61 | + location="record_section" |
| 62 | + actions={actions as any} |
| 63 | + />, |
| 64 | + ); |
| 65 | +} |
| 66 | + |
| 67 | +const lever = (name: string) => screen.getByTestId(`declared-action-${name}`); |
| 68 | + |
| 69 | +beforeEach(() => executeSpy.mockReset().mockResolvedValue({ success: true })); |
| 70 | +afterEach(() => vi.restoreAllMocks()); |
| 71 | + |
| 72 | +describe('DeclaredActionsBar — `pressed` draws the reader\'s own state (objectstack#28242)', () => { |
| 73 | + it('the predicate holds: aria-pressed="true" and the pressed face; the sibling vote is unpressed', () => { |
| 74 | + mount({ viewer: { vote: 'helpful' } }); |
| 75 | + expect(lever('mark_article_helpful')).toHaveAttribute('aria-pressed', 'true'); |
| 76 | + expect(lever('mark_article_helpful').className).toContain('aria-pressed:bg-accent'); |
| 77 | + expect(lever('mark_article_not_helpful')).toHaveAttribute('aria-pressed', 'false'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('the envelope arm reads the same: the other vote is the pressed one', () => { |
| 81 | + mount({ viewer: { vote: 'not_helpful' } }); |
| 82 | + expect(lever('mark_article_helpful')).toHaveAttribute('aria-pressed', 'false'); |
| 83 | + expect(lever('mark_article_not_helpful')).toHaveAttribute('aria-pressed', 'true'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('a row with no served block draws both unpressed', () => { |
| 87 | + mount({}); |
| 88 | + expect(lever('mark_article_helpful')).toHaveAttribute('aria-pressed', 'false'); |
| 89 | + expect(lever('mark_article_not_helpful')).toHaveAttribute('aria-pressed', 'false'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('display only: a pressed action stays enabled and still dispatches', async () => { |
| 93 | + const user = userEvent.setup(); |
| 94 | + mount({ viewer: { vote: 'helpful' } }, [HELPFUL]); |
| 95 | + expect(lever('mark_article_helpful')).not.toBeDisabled(); |
| 96 | + await user.click(lever('mark_article_helpful')); |
| 97 | + expect(executeSpy).toHaveBeenCalledTimes(1); |
| 98 | + expect(executeSpy.mock.calls[0][0]).toMatchObject({ name: 'mark_article_helpful' }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('a predicate that cannot be evaluated draws the plain button, and says so', () => { |
| 102 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 103 | + mount({}, [{ ...HELPFUL, pressed: "record.viewer.vote == 'helpful'" }]); |
| 104 | + expect(lever('mark_article_helpful')).toHaveAttribute('aria-pressed', 'false'); |
| 105 | + expect(warn.mock.calls.some((c) => String(c[0]).includes('(pressed)'))).toBe(true); |
| 106 | + }); |
| 107 | + |
| 108 | + it('control — an action that declares no predicate is a plain button: no aria-pressed, no face', () => { |
| 109 | + mount({ viewer: { vote: 'helpful' } }, [SHARE]); |
| 110 | + expect(lever('share_article')).not.toHaveAttribute('aria-pressed'); |
| 111 | + expect(lever('share_article').className).not.toContain('aria-pressed:'); |
| 112 | + }); |
| 113 | +}); |
0 commit comments