|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +import React from 'react'; |
| 4 | +import { render } from '@testing-library/react'; |
| 5 | + |
| 6 | +import { warnOnce } from '@cloudscape-design/component-toolkit/internal'; |
| 7 | + |
| 8 | +import AttributeEditor from '../../../lib/components/attribute-editor'; |
| 9 | + |
| 10 | +jest.mock('@cloudscape-design/component-toolkit', () => ({ |
| 11 | + ...jest.requireActual('@cloudscape-design/component-toolkit'), |
| 12 | + useContainerQuery: jest.fn().mockImplementation(() => ['m', () => {}]), |
| 13 | +})); |
| 14 | + |
| 15 | +jest.mock('@cloudscape-design/component-toolkit/internal', () => ({ |
| 16 | + ...jest.requireActual('@cloudscape-design/component-toolkit/internal'), |
| 17 | + warnOnce: jest.fn(), |
| 18 | +})); |
| 19 | + |
| 20 | +afterEach(() => { |
| 21 | + (warnOnce as jest.Mock).mockReset(); |
| 22 | +}); |
| 23 | + |
| 24 | +describe('AttributeEditor component', () => { |
| 25 | + test('warns when a definition has no label', () => { |
| 26 | + render( |
| 27 | + <AttributeEditor |
| 28 | + addButtonText="Add" |
| 29 | + removeButtonText="Remove" |
| 30 | + definition={[{ label: 'Key label', control: () => 'key' }]} |
| 31 | + items={[{}]} |
| 32 | + gridLayout={[{ rows: [[1]] }]} |
| 33 | + /> |
| 34 | + ); |
| 35 | + expect(warnOnce).not.toHaveBeenCalled(); |
| 36 | + |
| 37 | + render( |
| 38 | + <AttributeEditor |
| 39 | + addButtonText="Add" |
| 40 | + removeButtonText="Remove" |
| 41 | + definition={[{ label: 'Key label', control: () => 'key' }, { control: () => 'value' }]} |
| 42 | + items={[{}]} |
| 43 | + gridLayout={[{ rows: [[1, 1]] }]} |
| 44 | + /> |
| 45 | + ); |
| 46 | + |
| 47 | + expect(warnOnce).toHaveBeenCalledTimes(1); |
| 48 | + expect(warnOnce).toHaveBeenCalledWith( |
| 49 | + 'AttributeEditor', |
| 50 | + 'A `label` should be provided for each field definition. It is used as `aria-label` for accessibility.' |
| 51 | + ); |
| 52 | + }); |
| 53 | +}); |
0 commit comments