-
Notifications
You must be signed in to change notification settings - Fork 221
feat: add dev warning for missing field definition labels in attribute-editor #4337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ee9905d
add warning in attribute-editor
Who-is-PS 93e44b6
added tesst
Who-is-PS f9617ac
update text
Who-is-PS 44074e4
update test
Who-is-PS 3ca91c5
fix
Who-is-PS d121224
Merge branch 'main' into dev-v3-philosr-AWSUI-61432
Who-is-PS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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', () => {}]), | ||
| })); | ||
|
|
||
| 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.' | ||
| ); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
components/src/button-dropdown/__tests__/button-dropdown.test.tsx
Lines 16 to 23 in 8093e3f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah true, the
useContainerQuerymock is unnecessary here. I've switched to mocking@cloudscape-design/component-toolkit/internalwith justwarnOnce: jest.fn(), matching the pattern inbutton-dropdownandtable. This also lets us drop theconsoleWarnSpyentirely since we can assert directly on thewarnOncemock and usemockReset()between tests instead of relying on a separate file for a cleanwarnOncecache