|
| 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 | + * objectui#13183 step 1 — a bulk action's `confirmText` is read as a string or |
| 11 | + * as the `{ dialect: 'template', source }` envelope the protocol serves once |
| 12 | + * objectstack#24947 makes the slot a `template` slot. |
| 13 | + * |
| 14 | + * Two halves: |
| 15 | + * - the reader `bulkConfirmTextSource`: a string, an envelope (with and |
| 16 | + * without `dialect`), blank and absent values; |
| 17 | + * - the real `BulkActionDialog`: an envelope's `source` is the confirm |
| 18 | + * sentence (never `[object Object]`), a plain string renders unchanged, and |
| 19 | + * a blank or absent value keeps the default sentence. |
| 20 | + * Holes are not filled at this step: `{{ count }}` stays as written. |
| 21 | + */ |
| 22 | +import { describe, it, expect, vi, beforeAll } from 'vitest'; |
| 23 | +import React from 'react'; |
| 24 | +import { render, screen, cleanup } from '@testing-library/react'; |
| 25 | +import '@testing-library/jest-dom'; |
| 26 | +import type { BulkActionDef } from '@object-ui/types'; |
| 27 | + |
| 28 | +import { BulkActionDialog } from '../components/BulkActionDialog'; |
| 29 | +import { bulkConfirmTextSource } from '../components/bulkConfirmText'; |
| 30 | + |
| 31 | +beforeAll(() => { |
| 32 | + if (!Element.prototype.scrollIntoView) { |
| 33 | + Element.prototype.scrollIntoView = vi.fn() as any; |
| 34 | + } |
| 35 | +}); |
| 36 | + |
| 37 | +describe('bulkConfirmTextSource (objectui#13183)', () => { |
| 38 | + it('returns a plain string as written', () => { |
| 39 | + expect(bulkConfirmTextSource('Archive these accounts?')).toBe('Archive these accounts?'); |
| 40 | + }); |
| 41 | + |
| 42 | + it("returns an envelope's source verbatim, holes unfilled", () => { |
| 43 | + expect( |
| 44 | + bulkConfirmTextSource({ dialect: 'template', source: 'Update {{ count }} record(s)?' }), |
| 45 | + ).toBe('Update {{ count }} record(s)?'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('reads an envelope that omits `dialect`', () => { |
| 49 | + expect(bulkConfirmTextSource({ source: 'Close these deals?' })).toBe('Close these deals?'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('returns undefined for a blank string or a blank source', () => { |
| 53 | + expect(bulkConfirmTextSource('')).toBeUndefined(); |
| 54 | + expect(bulkConfirmTextSource(' ')).toBeUndefined(); |
| 55 | + expect(bulkConfirmTextSource({ dialect: 'template', source: ' ' })).toBeUndefined(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('returns undefined when absent', () => { |
| 59 | + expect(bulkConfirmTextSource(undefined)).toBeUndefined(); |
| 60 | + expect(bulkConfirmTextSource(null)).toBeUndefined(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('returns undefined for an envelope with no string source', () => { |
| 64 | + expect(bulkConfirmTextSource({ dialect: 'template' } as never)).toBeUndefined(); |
| 65 | + }); |
| 66 | +}); |
| 67 | + |
| 68 | +const ROWS = [{ id: 'r1', name: 'Acme' }, { id: 'r2', name: 'Globex' }]; |
| 69 | + |
| 70 | +function openConfirm(confirmText: BulkActionDef['confirmText']) { |
| 71 | + cleanup(); |
| 72 | + const def: BulkActionDef = { |
| 73 | + name: 'archive', |
| 74 | + label: 'Archive', |
| 75 | + operation: 'update', |
| 76 | + patch: { archived: true }, |
| 77 | + ...(confirmText !== undefined && { confirmText }), |
| 78 | + }; |
| 79 | + render( |
| 80 | + <BulkActionDialog |
| 81 | + def={def} |
| 82 | + rows={ROWS} |
| 83 | + resource="account" |
| 84 | + dataSource={{ update: vi.fn(async () => ({})), delete: vi.fn(async () => ({})) } as any} |
| 85 | + open |
| 86 | + onClose={() => {}} |
| 87 | + />, |
| 88 | + ); |
| 89 | +} |
| 90 | + |
| 91 | +describe('BulkActionDialog renders confirmText through the reader (objectui#13183)', () => { |
| 92 | + it("renders an envelope's source, never [object Object]", async () => { |
| 93 | + openConfirm({ dialect: 'template', source: 'Archive {{ count }} account(s)?' }); |
| 94 | + expect(await screen.findByText('Archive {{ count }} account(s)?')).toBeInTheDocument(); |
| 95 | + expect(screen.queryByText(/\[object Object\]/)).toBeNull(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('renders a plain string unchanged', async () => { |
| 99 | + openConfirm('Archive these accounts?'); |
| 100 | + expect(await screen.findByText('Archive these accounts?')).toBeInTheDocument(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('keeps the default sentence when the slot is absent', async () => { |
| 104 | + openConfirm(undefined); |
| 105 | + expect(await screen.findByText(/This will apply to 2 record/)).toBeInTheDocument(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('keeps the default sentence when the slot is blank', async () => { |
| 109 | + openConfirm({ dialect: 'template', source: ' ' }); |
| 110 | + expect(await screen.findByText(/This will apply to 2 record/)).toBeInTheDocument(); |
| 111 | + }); |
| 112 | +}); |
0 commit comments