|
| 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 | + * A related list's header count badge speaks the session locale. |
| 11 | + * |
| 12 | + * The badge draws a bare number, so its `aria-label` is what a screen reader |
| 13 | + * announces for it. It reads `detail.relatedRecordOne` at one record and |
| 14 | + * `detail.relatedRecords` otherwise — the same pair the tab list's summary |
| 15 | + * row lead reads — so a zh-CN console announces 「3 条记录」 and an English |
| 16 | + * one never says "1 records". |
| 17 | + */ |
| 18 | +import { describe, it, expect, vi, beforeAll, afterEach } from 'vitest'; |
| 19 | +import { cleanup, render, waitFor } from '@testing-library/react'; |
| 20 | +import '@testing-library/jest-dom'; |
| 21 | +import React from 'react'; |
| 22 | +import { I18nProvider } from '@object-ui/i18n'; |
| 23 | +import { RelatedList } from './RelatedList'; |
| 24 | + |
| 25 | +beforeAll(() => { |
| 26 | + Object.defineProperty(window, 'innerWidth', { configurable: true, value: 1280 }); |
| 27 | +}); |
| 28 | + |
| 29 | +afterEach(() => cleanup()); |
| 30 | + |
| 31 | +const rows = (n: number) => Array.from({ length: n }, (_, i) => ({ id: `c${i + 1}`, name: `Contact ${i + 1}` })); |
| 32 | + |
| 33 | +function renderListIn(language: string, n: number) { |
| 34 | + const find = vi.fn(async () => ({ data: rows(n), total: n })); |
| 35 | + render( |
| 36 | + <I18nProvider config={{ defaultLanguage: language, detectBrowserLanguage: false }}> |
| 37 | + <RelatedList |
| 38 | + title="Contacts" |
| 39 | + type="table" |
| 40 | + api="contact" |
| 41 | + objectName="contact" |
| 42 | + referenceField="account" |
| 43 | + parentId="ACC-1" |
| 44 | + columns={[{ accessorKey: 'name', header: 'Name' }]} |
| 45 | + dataSource={{ find } as any} |
| 46 | + /> |
| 47 | + </I18nProvider>, |
| 48 | + ); |
| 49 | +} |
| 50 | + |
| 51 | +/** The accessible name of the list header's count badge, once it draws `n`. */ |
| 52 | +async function headBadgeName(n: number): Promise<string | null> { |
| 53 | + let name: string | null = null; |
| 54 | + await waitFor(() => { |
| 55 | + const badges = Array.from(document.querySelectorAll('[data-related-list-head] [aria-label]')) |
| 56 | + .filter((el) => el.textContent === String(n)); |
| 57 | + expect(badges).toHaveLength(1); |
| 58 | + name = badges[0].getAttribute('aria-label'); |
| 59 | + }); |
| 60 | + return name; |
| 61 | +} |
| 62 | + |
| 63 | +describe('related list count badge — accessible name is translated', () => { |
| 64 | + it('announces the zh bundle value under a zh-CN session', async () => { |
| 65 | + renderListIn('zh-CN', 3); |
| 66 | + expect(await headBadgeName(3)).toBe('3 条记录'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('announces the singular under an en session at one record', async () => { |
| 70 | + renderListIn('en', 1); |
| 71 | + expect(await headBadgeName(1)).toBe('1 record'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('announces the plural under an en session', async () => { |
| 75 | + renderListIn('en', 3); |
| 76 | + expect(await headBadgeName(3)).toBe('3 records'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('announces the singular under a de session at one record', async () => { |
| 80 | + renderListIn('de', 1); |
| 81 | + expect(await headBadgeName(1)).toBe('1 Datensatz'); |
| 82 | + }); |
| 83 | +}); |
0 commit comments