-
Notifications
You must be signed in to change notification settings - Fork 156
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
chore: Add initialCheck
capability to alert/flash content API
#2879
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
344c46b
chore: Add `initialCheck` capability to alert/flash content API
gethinwebster 75e3857
Hide alerts during initial render
gethinwebster 7361561
Tweaks to API
gethinwebster be9ecb4
Add tests
gethinwebster 8b7a60b
Add some further tests
gethinwebster 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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,33 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import { BasePageObject } from '@cloudscape-design/browser-test-tools/page-objects'; | ||
import useBrowser from '@cloudscape-design/browser-test-tools/use-browser'; | ||
|
||
import createWrapper from '../../../lib/components/test-utils/selectors'; | ||
|
||
class RuntimeContentPage extends BasePageObject { | ||
async rerenderAlerts() { | ||
await this.click(createWrapper().findCheckbox('[data-testid="unmount-all"]').findNativeInput().toSelector()); | ||
await this.keys(['Space']); | ||
} | ||
} | ||
|
||
function setupTest(testFn: (page: RuntimeContentPage) => Promise<void>) { | ||
return useBrowser(async browser => { | ||
const page = new RuntimeContentPage(browser); | ||
await browser.url('#/light/alert/runtime-content/?autofocus=true'); | ||
await page.waitForVisible('.screenshot-area'); | ||
await testFn(page); | ||
}); | ||
} | ||
|
||
test( | ||
'should focus the alert', | ||
setupTest(async page => { | ||
await page.rerenderAlerts(); | ||
|
||
await expect(page.getFocusedElementText()).resolves.toEqual(expect.stringContaining('---REPLACEMENT---')); | ||
// (make sure entire page isn't focused) | ||
await expect(page.getFocusedElementText()).resolves.toEqual(expect.not.stringContaining('Header')); | ||
}) | ||
); |
This file contains 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,59 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import * as React from 'react'; | ||
import testRenderer, { ReactTestRendererJSON } from 'react-test-renderer'; | ||
|
||
import Alert from '../../../lib/components/alert'; | ||
import awsuiPlugins from '../../../lib/components/internal/plugins'; | ||
import { awsuiPluginsInternal } from '../../../lib/components/internal/plugins/api'; | ||
import { AlertFlashContentConfig } from '../../../lib/components/internal/plugins/controllers/alert-flash-content'; | ||
|
||
import stylesCss from '../../../lib/components/alert/styles.css.js'; | ||
|
||
afterEach(() => { | ||
awsuiPluginsInternal.alertContent.clearRegisteredReplacer(); | ||
jest.resetAllMocks(); | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
// In a separate file as mixing react-test-renderer and @testing-library/react in a single file can cause some issues. | ||
// We use react-test-renderer here as it works at a slightly lower level, so doesn't "hide" the first render cycle from tests. | ||
describe('initialCheck method', () => { | ||
let initialCheck: jest.Mock<boolean>; | ||
beforeEach(() => { | ||
initialCheck = jest.fn(() => true); | ||
const plugin: AlertFlashContentConfig = { | ||
id: 'plugin-1', | ||
runReplacer: () => { | ||
return { update: () => {}, unmount: () => {} }; | ||
}, | ||
initialCheck, | ||
}; | ||
awsuiPlugins.alertContent.registerContentReplacer(plugin); | ||
}); | ||
|
||
test('calls `initialCheck` method, and hides alert if true', () => { | ||
const basicRender = testRenderer.create(<Alert type="error">Content</Alert>); | ||
expect(initialCheck).toHaveBeenCalledTimes(1); | ||
expect((basicRender.toJSON() as ReactTestRendererJSON).props.className.split(' ')).toEqual( | ||
expect.arrayContaining([stylesCss['initial-hidden']]) | ||
); | ||
}); | ||
|
||
test('re-shows alert on next render', () => { | ||
const basicRender = testRenderer.create(<Alert type="error">Content</Alert>); | ||
basicRender.update(<Alert type="error">Content</Alert>); | ||
expect(initialCheck).toHaveBeenCalledTimes(1); | ||
expect((basicRender.toJSON() as ReactTestRendererJSON).props.className.split(' ')).toEqual( | ||
expect.not.arrayContaining([stylesCss['initial-hidden']]) | ||
); | ||
}); | ||
|
||
test('does not hide alert if `initialCheck` returns false', () => { | ||
initialCheck.mockReturnValue(false); | ||
const basicRender = testRenderer.create(<Alert type="error">Content</Alert>); | ||
expect((basicRender.toJSON() as ReactTestRendererJSON).props.className.split(' ')).toEqual( | ||
expect.not.arrayContaining([stylesCss['initial-hidden']]) | ||
); | ||
}); | ||
}); |
This file contains 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
This file contains 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.
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.
The types version does not match the actual version of the dependency