|
| 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 1 — a bulk action's `confirmText` is a `template` |
| 11 | + * slot, and its `{{ count }}` hole is filled with the number of records the run |
| 12 | + * covers, through the shared template engine (whitelisted formatters, no |
| 13 | + * logic). A sentence with no hole is shown as written, and the default |
| 14 | + * sentence is untouched. |
| 15 | + * |
| 16 | + * Two halves: |
| 17 | + * - the helper `bulkConfirmText`: the count root, a formatter, the two slot |
| 18 | + * shapes, a sentence without holes, blank and absent values, a source the |
| 19 | + * engine refuses; |
| 20 | + * - the real `BulkActionDialog`: the confirm sentence shows the count of the |
| 21 | + * rows it was handed, for a string and an envelope alike. |
| 22 | + */ |
| 23 | +import { describe, it, expect, vi, beforeAll } from 'vitest'; |
| 24 | +import React from 'react'; |
| 25 | +import { render, screen, cleanup } from '@testing-library/react'; |
| 26 | +import '@testing-library/jest-dom'; |
| 27 | +import type { BulkActionDef } from '@object-ui/types'; |
| 28 | +import { BULK_CONFIRM_COUNT_ROOT } from '@objectstack/spec/ui'; |
| 29 | + |
| 30 | +import { BulkActionDialog } from '../components/BulkActionDialog'; |
| 31 | +import { bulkConfirmText } from '../components/bulkConfirmText'; |
| 32 | + |
| 33 | +beforeAll(() => { |
| 34 | + if (!Element.prototype.scrollIntoView) { |
| 35 | + Element.prototype.scrollIntoView = vi.fn() as any; |
| 36 | + } |
| 37 | +}); |
| 38 | + |
| 39 | +describe('bulkConfirmText fills {{ count }} (objectstack#25942)', () => { |
| 40 | + it('binds the root the protocol names for the count', () => { |
| 41 | + expect(BULK_CONFIRM_COUNT_ROOT).toBe('count'); |
| 42 | + expect(bulkConfirmText('Archive {{count}} account(s)?', { count: 3 })).toBe('Archive 3 account(s)?'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('fills the hole in a bare string and in an envelope alike', () => { |
| 46 | + expect(bulkConfirmText('Close {{ count }} deal(s)?', { count: 7 })).toBe('Close 7 deal(s)?'); |
| 47 | + expect( |
| 48 | + bulkConfirmText({ dialect: 'template', source: 'Close {{ count }} deal(s)?' }, { count: 7 }), |
| 49 | + ).toBe('Close 7 deal(s)?'); |
| 50 | + }); |
| 51 | + |
| 52 | + it("applies the engine's whitelisted formatters", () => { |
| 53 | + expect(bulkConfirmText('Update {{ count | number }} records?', { count: 1234 })).toBe('Update 1,234 records?'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('shows a sentence without holes as written, a `${}` token included', () => { |
| 57 | + expect(bulkConfirmText('Archive these accounts?', { count: 2 })).toBe('Archive these accounts?'); |
| 58 | + expect(bulkConfirmText('Pay ${count} now?', { count: 2 })).toBe('Pay ${count} now?'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('returns undefined for a blank or absent slot, so the default sentence shows', () => { |
| 62 | + expect(bulkConfirmText(' ', { count: 2 })).toBeUndefined(); |
| 63 | + expect(bulkConfirmText({ dialect: 'template' }, { count: 2 })).toBeUndefined(); |
| 64 | + expect(bulkConfirmText(undefined, { count: 2 })).toBeUndefined(); |
| 65 | + expect(bulkConfirmText(null, { count: 2 })).toBeUndefined(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('returns a source the engine refuses as written', () => { |
| 69 | + expect(bulkConfirmText('Archive {{ count }} of {{ count', { count: 2 })).toBe('Archive {{ count }} of {{ count'); |
| 70 | + expect(bulkConfirmText('Archive {{ count + 1 }}?', { count: 2 })).toBe('Archive {{ count + 1 }}?'); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +const ROWS = [ |
| 75 | + { id: 'r1', name: 'Acme' }, |
| 76 | + { id: 'r2', name: 'Globex' }, |
| 77 | + { id: 'r3', name: 'Initech' }, |
| 78 | +]; |
| 79 | + |
| 80 | +function openConfirm(confirmText: BulkActionDef['confirmText'], rows = ROWS) { |
| 81 | + cleanup(); |
| 82 | + const def: BulkActionDef = { |
| 83 | + name: 'archive', |
| 84 | + label: 'Archive', |
| 85 | + operation: 'update', |
| 86 | + patch: { archived: true }, |
| 87 | + ...(confirmText !== undefined && { confirmText }), |
| 88 | + }; |
| 89 | + render( |
| 90 | + <BulkActionDialog |
| 91 | + def={def} |
| 92 | + rows={rows} |
| 93 | + resource="account" |
| 94 | + dataSource={{ update: vi.fn(async () => ({})), delete: vi.fn(async () => ({})) } as any} |
| 95 | + open |
| 96 | + onClose={() => {}} |
| 97 | + />, |
| 98 | + ); |
| 99 | +} |
| 100 | + |
| 101 | +describe('BulkActionDialog fills {{ count }} in its confirm sentence (objectstack#25942)', () => { |
| 102 | + it('shows the count of the rows the run covers', async () => { |
| 103 | + openConfirm('Archive {{ count }} account(s)?'); |
| 104 | + expect(await screen.findByText('Archive 3 account(s)?')).toBeInTheDocument(); |
| 105 | + expect(screen.queryByText(/\{\{/)).toBeNull(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('counts the rows it was handed, not a fixed number', async () => { |
| 109 | + openConfirm({ dialect: 'template', source: 'Archive {{ count }} account(s)?' }, ROWS.slice(0, 1)); |
| 110 | + expect(await screen.findByText('Archive 1 account(s)?')).toBeInTheDocument(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('keeps the default sentence when no confirmText is authored', async () => { |
| 114 | + openConfirm(undefined); |
| 115 | + expect(await screen.findByText(/This will apply to 3 record/)).toBeInTheDocument(); |
| 116 | + }); |
| 117 | +}); |
0 commit comments