|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * objectstack#25942 step 2 — a bulk `confirmText` hole naming one of the def's |
| 11 | + * params is filled with that param's DISPLAY value, by the rule the protocol |
| 12 | + * states on the slot: the chosen option's label (a multi-value param's labels |
| 13 | + * joined with ', '), the picked record's display name for a lookup / user |
| 14 | + * param, and the entered value otherwise, so the engine's formatters apply. |
| 15 | + * `count` wins over a param of that name. |
| 16 | + * |
| 17 | + * Two halves: |
| 18 | + * - the helper `bulkConfirmText` over a full scope; |
| 19 | + * - the real `BulkActionDialog`: an option label, and a lookup name that |
| 20 | + * arrives after the confirm step has opened. |
| 21 | + */ |
| 22 | +import { describe, it, expect, vi, beforeAll } from 'vitest'; |
| 23 | +import React from 'react'; |
| 24 | +import { render, screen, cleanup, fireEvent } from '@testing-library/react'; |
| 25 | +import '@testing-library/jest-dom'; |
| 26 | +import type { BulkActionDef, BulkActionParam } from '@object-ui/types'; |
| 27 | + |
| 28 | +import { BulkActionDialog } from '../components/BulkActionDialog'; |
| 29 | +import { bulkConfirmText } from '../components/bulkConfirmText'; |
| 30 | + |
| 31 | +beforeAll(() => { |
| 32 | + if (!Element.prototype.scrollIntoView) { |
| 33 | + Element.prototype.scrollIntoView = vi.fn() as any; |
| 34 | + } |
| 35 | +}); |
| 36 | + |
| 37 | +const TIER: BulkActionParam = { |
| 38 | + name: 'tier', |
| 39 | + label: 'Tier', |
| 40 | + type: 'select', |
| 41 | + options: [ |
| 42 | + { label: 'Gold', value: 'gold' }, |
| 43 | + { label: 'Silver', value: 'silver' }, |
| 44 | + ], |
| 45 | +}; |
| 46 | +const TAGS: BulkActionParam = { |
| 47 | + name: 'tags', |
| 48 | + label: 'Tags', |
| 49 | + type: 'multiselect', |
| 50 | + options: [ |
| 51 | + { label: 'Alpha', value: 'a' }, |
| 52 | + { label: 'Beta', value: 'b' }, |
| 53 | + ], |
| 54 | +}; |
| 55 | +const OWNER: BulkActionParam = { name: 'owner', label: 'Owner', type: 'lookup', object: 'sys_user' }; |
| 56 | +const AMOUNT: BulkActionParam = { name: 'amount', label: 'Amount', type: 'number' }; |
| 57 | +const DUE: BulkActionParam = { name: 'due', label: 'Due', type: 'date' }; |
| 58 | +const NOTE: BulkActionParam = { name: 'note', label: 'Note', type: 'text' }; |
| 59 | + |
| 60 | +function fill(source: string, values: Record<string, unknown>, extra: { count?: number; params?: BulkActionParam[]; lookupLabels?: Record<string, Record<string, string>> } = {}) { |
| 61 | + return bulkConfirmText(source, { |
| 62 | + count: extra.count ?? 2, |
| 63 | + params: extra.params ?? [TIER, TAGS, OWNER, AMOUNT, DUE, NOTE], |
| 64 | + values, |
| 65 | + lookupLabels: extra.lookupLabels, |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +describe('bulkConfirmText fills param holes with display values (objectstack#25942)', () => { |
| 70 | + it("shows the chosen option's label, not its value", () => { |
| 71 | + expect(fill('Move {{ count }} account(s) to {{ tier }}?', { tier: 'gold' })).toBe('Move 2 account(s) to Gold?'); |
| 72 | + }); |
| 73 | + |
| 74 | + it("joins a multi-value param's labels with ', '", () => { |
| 75 | + expect(fill('Tag them {{ tags }}?', { tags: ['a', 'b'] })).toBe('Tag them Alpha, Beta?'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('shows a value no option declares as entered', () => { |
| 79 | + expect(fill('Move to {{ tier }}?', { tier: 'bronze' })).toBe('Move to bronze?'); |
| 80 | + }); |
| 81 | + |
| 82 | + it("shows a lookup's resolved record name, and the id until it is resolved", () => { |
| 83 | + expect(fill('Assign to {{ owner }}?', { owner: 'u1' }, { lookupLabels: { owner: { u1: 'Ada Lovelace' } } })) |
| 84 | + .toBe('Assign to Ada Lovelace?'); |
| 85 | + expect(fill('Assign to {{ owner }}?', { owner: 'u1' })).toBe('Assign to u1?'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('hands the entered value to the formatters', () => { |
| 89 | + expect(fill('Charge {{ amount | currency:EUR }} each?', { amount: 1234.5 })).toBe('Charge €1,234.50 each?'); |
| 90 | + expect(fill('Charge {{ amount | number:2 }} each?', { amount: 3 })).toBe('Charge 3.00 each?'); |
| 91 | + expect(fill('Due {{ due | date:iso }}?', { due: '2026-10-26' })).toBe('Due 2026-10-26?'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('prints an empty param empty, so the default formatter can name it', () => { |
| 95 | + expect(fill('Note: {{ note }}.', {})).toBe('Note: .'); |
| 96 | + expect(fill("Note: {{ note | default:'none' }}.", { note: '' })).toBe('Note: none.'); |
| 97 | + expect(fill("Tags: {{ tags | default:'none' }}.", { tags: [] })).toBe('Tags: none.'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('lets count win over a param named count', () => { |
| 101 | + const countParam: BulkActionParam = { name: 'count', label: 'Count', type: 'number' }; |
| 102 | + expect(fill('Update {{ count }} record(s)?', { count: 99 }, { count: 3, params: [countParam] })) |
| 103 | + .toBe('Update 3 record(s)?'); |
| 104 | + }); |
| 105 | + |
| 106 | + it('prints a hole outside the closed scope empty', () => { |
| 107 | + expect(fill('Update {{ record.name }} to {{ tier }}?', { tier: 'silver' })).toBe('Update to Silver?'); |
| 108 | + }); |
| 109 | +}); |
| 110 | + |
| 111 | +const ROWS = [{ id: 'r1', name: 'Acme' }, { id: 'r2', name: 'Globex' }]; |
| 112 | + |
| 113 | +function openDialog(def: BulkActionDef, dataSource: Record<string, unknown> = {}) { |
| 114 | + cleanup(); |
| 115 | + render( |
| 116 | + <BulkActionDialog |
| 117 | + def={def} |
| 118 | + rows={ROWS} |
| 119 | + resource="account" |
| 120 | + dataSource={{ update: vi.fn(async () => ({})), delete: vi.fn(async () => ({})), ...dataSource } as any} |
| 121 | + open |
| 122 | + onClose={() => {}} |
| 123 | + />, |
| 124 | + ); |
| 125 | +} |
| 126 | + |
| 127 | +describe('BulkActionDialog fills param holes in its confirm sentence (objectstack#25942)', () => { |
| 128 | + it('shows the option label the params step chose', async () => { |
| 129 | + openDialog({ |
| 130 | + name: 'set_tier', |
| 131 | + label: 'Set tier', |
| 132 | + operation: 'update', |
| 133 | + params: [{ ...TIER, default: 'silver' }], |
| 134 | + confirmText: { dialect: 'template', source: 'Move {{ count }} account(s) to {{ tier }}?' }, |
| 135 | + }); |
| 136 | + fireEvent.click(await screen.findByRole('button', { name: 'Next' })); |
| 137 | + expect(await screen.findByText('Move 2 account(s) to Silver?')).toBeInTheDocument(); |
| 138 | + }); |
| 139 | + |
| 140 | + it('shows the lookup record name once it resolves after the step opens', async () => { |
| 141 | + let resolveName: (rec: Record<string, unknown>) => void = () => {}; |
| 142 | + const findOne = vi.fn(() => new Promise<Record<string, unknown>>((r) => { resolveName = r; })); |
| 143 | + openDialog( |
| 144 | + { |
| 145 | + name: 'assign', |
| 146 | + label: 'Assign', |
| 147 | + operation: 'update', |
| 148 | + params: [{ ...OWNER, default: 'u1' }], |
| 149 | + confirmText: 'Assign {{ count }} account(s) to {{ owner }}?', |
| 150 | + }, |
| 151 | + { findOne }, |
| 152 | + ); |
| 153 | + fireEvent.click(await screen.findByRole('button', { name: 'Next' })); |
| 154 | + expect(await screen.findByText('Assign 2 account(s) to u1?')).toBeInTheDocument(); |
| 155 | + expect(findOne).toHaveBeenCalledWith('sys_user', 'u1'); |
| 156 | + resolveName({ id: 'u1', name: 'Ada Lovelace' }); |
| 157 | + expect(await screen.findByText('Assign 2 account(s) to Ada Lovelace?')).toBeInTheDocument(); |
| 158 | + }); |
| 159 | +}); |
0 commit comments