Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/attribute-editor/__tests__/warnings.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import React from 'react';
import { render } from '@testing-library/react';

import AttributeEditor from '../../../lib/components/attribute-editor';

jest.mock('@cloudscape-design/component-toolkit', () => ({
...jest.requireActual('@cloudscape-design/component-toolkit'),
useContainerQuery: jest.fn().mockImplementation(() => ['m', () => {}]),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this line? If you mock '@cloudscape-design/component-toolkit/internal' we shouldn't need this since we are only interested in 'warnOnce'. I'd check how the other warnOnce usages are tested e.g.

jest.mock('@cloudscape-design/component-toolkit/internal', () => ({
...jest.requireActual('@cloudscape-design/component-toolkit/internal'),
warnOnce: jest.fn(),
}));
afterEach(() => {
(warnOnce as jest.Mock).mockReset();
});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah true, the useContainerQuery mock is unnecessary here. I've switched to mocking @cloudscape-design/component-toolkit/internal with just warnOnce: jest.fn(), matching the pattern in button-dropdown and table. This also lets us drop the consoleWarnSpy entirely since we can assert directly on the warnOnce mock and use mockReset() between tests instead of relying on a separate file for a clean warnOnce cache

}));

let consoleWarnSpy: jest.SpyInstance;
beforeEach(() => {
consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation();
});
afterEach(() => {
consoleWarnSpy?.mockRestore();
});

/**
* This test suite is in a separate file, because it needs a clean messageCache (inside `warnOnce()`).
* Otherwise, warnings would not appear at the expected time in the test, because they have been issued before.
*/
describe('AttributeEditor component', () => {
test('warns when a definition has no label', () => {
render(
<AttributeEditor
addButtonText="Add"
removeButtonText="Remove"
definition={[{ label: 'Key label', control: () => 'key' }]}
items={[{}]}
gridLayout={[{ rows: [[1]] }]}
/>
);
expect(console.warn).not.toHaveBeenCalled();

render(
<AttributeEditor
addButtonText="Add"
removeButtonText="Remove"
definition={[{ label: 'Key label', control: () => 'key' }, { control: () => 'value' }]}
items={[{}]}
gridLayout={[{ rows: [[1, 1]] }]}
/>
);

expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith(
'[AwsUi] [AttributeEditor] A `label` should be provided for each field definition. It is used as `aria-label` for accessibility.'
);
});
});
12 changes: 11 additions & 1 deletion src/attribute-editor/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import React, { useImperativeHandle, useRef, useState } from 'react';
import clsx from 'clsx';

import { useMergeRefs, useUniqueId } from '@cloudscape-design/component-toolkit/internal';
import { useMergeRefs, useUniqueId, warnOnce } from '@cloudscape-design/component-toolkit/internal';

import { ButtonProps } from '../button/interfaces';
import { InternalButton } from '../button/internal';
Expand Down Expand Up @@ -92,6 +92,16 @@ const InternalAttributeEditor = React.forwardRef(
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [items, i18nStrings?.itemRemovedAriaLive]);

for (const def of definition) {
if (def && !def.label) {
warnOnce(
'AttributeEditor',
'A `label` should be provided for each field definition. It is used as `aria-label` for accessibility.'
);
break;
}
}

if (!gridLayout) {
gridLayout = gridDefaults[definition.length];
if (!gridLayout) {
Expand Down
Loading