Skip to content

Commit

Permalink
Add some further tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gethinwebster committed Oct 18, 2024
1 parent be9ecb4 commit 8b7a60b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pages/alert/runtime-content.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ export default function () {
<Checkbox onChange={e => setUrlParams({ loading: e.detail.checked })} checked={loading}>
Content loading
</Checkbox>
<Checkbox onChange={e => setUrlParams({ hidden: e.detail.checked })} checked={hidden}>
<Checkbox
onChange={e => setUrlParams({ hidden: e.detail.checked })}
checked={hidden}
data-testid="unmount-all"
>
Unmount all
</Checkbox>
<Checkbox onChange={e => setUnrelatedState(e.detail.checked)} checked={unrelatedState}>
Expand Down Expand Up @@ -147,6 +151,7 @@ export default function () {
header="Header"
action={<Button>Action</Button>}
ref={alertRef}
data-testid="error-alert"
>
{!contentSwapped ? content2 : content1}
</Alert>
Expand Down
33 changes: 33 additions & 0 deletions src/alert/__integ__/runtime-content.test.ts
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'));
})
);
2 changes: 2 additions & 0 deletions src/alert/__tests__/runtime-content-initial.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ afterEach(() => {
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(() => {
Expand Down
2 changes: 2 additions & 0 deletions src/flashbar/__tests__/runtime-content-initial.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ afterEach(() => {
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>;
const getFirstFlash = (basicRender: ReactTestRenderer) =>
Expand Down

0 comments on commit 8b7a60b

Please sign in to comment.