Skip to content

Commit 691772a

Browse files
author
Johannes Weber
committed
feat: Add findItemById test-util to FlashbarWrapper
1 parent 6082b18 commit 691772a

5 files changed

Lines changed: 120 additions & 0 deletions

File tree

src/__tests__/snapshot-tests/__snapshots__/documenter.test.ts.snap

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37453,6 +37453,25 @@ wrapper.selectOptionByValue('option_1');
3745337453
},
3745437454
{
3745537455
"methods": [
37456+
{
37457+
"description": "Returns a flash item by its id.
37458+
37459+
The id is matched against the \`id\` property of each item passed to the \`items\` property of the Flashbar component.",
37460+
"name": "findItemById",
37461+
"parameters": [
37462+
{
37463+
"flags": {
37464+
"isOptional": false,
37465+
},
37466+
"name": "id",
37467+
"typeName": "string",
37468+
},
37469+
],
37470+
"returnType": {
37471+
"isNullable": true,
37472+
"name": "FlashWrapper",
37473+
},
37474+
},
3745637475
{
3745737476
"description": "Returns the individual flashes of this flashbar.
3745837477

@@ -47284,6 +47303,25 @@ The mode selector is only rendered as a Select on narrow viewports. On wide view
4728447303
},
4728547304
{
4728647305
"methods": [
47306+
{
47307+
"description": "Returns a flash item by its id.
47308+
47309+
The id is matched against the \`id\` property of each item passed to the \`items\` property of the Flashbar component.",
47310+
"name": "findItemById",
47311+
"parameters": [
47312+
{
47313+
"flags": {
47314+
"isOptional": false,
47315+
},
47316+
"name": "id",
47317+
"typeName": "string",
47318+
},
47319+
],
47320+
"returnType": {
47321+
"isNullable": true,
47322+
"name": "FlashWrapper",
47323+
},
47324+
},
4728747325
{
4728847326
"description": "Returns the individual flashes of this flashbar.
4728947327

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
import { BasePageObject } from '@cloudscape-design/browser-test-tools/page-objects';
4+
import useBrowser from '@cloudscape-design/browser-test-tools/use-browser';
5+
6+
import createWrapper from '../../../lib/components/test-utils/selectors';
7+
8+
const flashbar = createWrapper().findFlashbar();
9+
10+
const setupTest = (testFn: (page: BasePageObject) => Promise<void>) =>
11+
useBrowser(async browser => {
12+
const page = new BasePageObject(browser);
13+
await browser.url('#/light/flashbar/interactive');
14+
await page.waitForVisible(flashbar.toSelector());
15+
await testFn(page);
16+
});
17+
18+
describe('findItemById', () => {
19+
test(
20+
'finds a flash item by its id',
21+
setupTest(async page => {
22+
const item = flashbar.findItemById('0');
23+
await expect(page.isExisting(item!.toSelector())).resolves.toBe(true);
24+
})
25+
);
26+
27+
test(
28+
'returns null for non-existent id',
29+
setupTest(async page => {
30+
const item = flashbar.findItemById('nonexistent');
31+
await expect(page.isExisting(item!.toSelector())).resolves.toBe(false);
32+
})
33+
);
34+
});

src/flashbar/__tests__/collapsible.test.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,26 @@ describe('Collapsible Flashbar', () => {
620620
disableMotion(true);
621621
testFlashDismissal({ stackItems: true });
622622
});
623+
624+
test('findItemById', () => {
625+
const wrapper = createFlashbarWrapper(
626+
<Flashbar
627+
stackItems={true}
628+
items={[
629+
{ content: 'Flash 1', id: 'flash-1', type: 'success' },
630+
{ content: 'Flash 2', id: 'flash-2', type: 'error' },
631+
]}
632+
/>
633+
);
634+
expect(wrapper.findItemById('flash-1')).not.toBeNull();
635+
expect(wrapper.findItemById('flash-2')).toBeNull();
636+
637+
findNotificationBar(wrapper)!.click();
638+
639+
expect(wrapper.findItemById('flash-1')).not.toBeNull();
640+
expect(wrapper.findItemById('flash-2')).not.toBeNull();
641+
expect(wrapper.findItemById('nonexistent')).toBeNull();
642+
});
623643
});
624644

625645
// Entire interactive element including the counter and the actual <button/> element

src/flashbar/__tests__/flashbar.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,4 +443,22 @@ describe('Flashbar component', () => {
443443
setAnimations(false);
444444
testFlashDismissal({ stackItems: false });
445445
});
446+
447+
test('findItemById', () => {
448+
const wrapper = createFlashbarWrapper(
449+
<Flashbar
450+
items={[
451+
{ content: 'Flash 1', id: 'flash-1', type: 'success' },
452+
{ content: 'Flash 2', id: 'flash-2', type: 'error' },
453+
{ content: 'Flash 3', type: 'warning' },
454+
]}
455+
/>
456+
);
457+
expect(wrapper.findItemById('flash-1')).not.toBeNull();
458+
expect(wrapper.findItemById('flash-1')!.findContent()!.getElement()).toHaveTextContent('Flash 1');
459+
expect(wrapper.findItemById('flash-2')).not.toBeNull();
460+
expect(wrapper.findItemById('flash-2')!.findContent()!.getElement()).toHaveTextContent('Flash 2');
461+
expect(wrapper.findItemById('nonexistent')).toBeNull();
462+
expect(wrapper.findItemById('flash-3')).toBeNull();
463+
});
446464
});

src/test-utils/dom/flashbar/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ export default class FlashbarWrapper extends ComponentWrapper {
1818
return this.findAllByClassName(styles['flash-list-item']).map(item => new FlashWrapper(item.getElement()));
1919
}
2020

21+
/**
22+
* Returns a flash item by its id.
23+
*
24+
* The id is matched against the `id` property of each item passed to the `items` property of the Flashbar component.
25+
*/
26+
findItemById(id: string): FlashWrapper | null {
27+
const element = this.find(`[data-itemid="${CSS.escape(id)}"]`);
28+
return element ? new FlashWrapper(element.getElement()) : null;
29+
}
30+
2131
/**
2232
* Returns the individual flashes of this flashbar given the item type.
2333
*

0 commit comments

Comments
 (0)