Skip to content

Commit 5e98e74

Browse files
author
Johannes Weber
committed
feat: Add findItemById test-util to FlashbarWrapper
1 parent a494b56 commit 5e98e74

4 files changed

Lines changed: 82 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
@@ -37439,6 +37439,25 @@ wrapper.selectOptionByValue('option_1');
3743937439
},
3744037440
{
3744137441
"methods": [
37442+
{
37443+
"description": "Returns a flash item by its id.
37444+
37445+
The id is matched against the \`id\` property of each item passed to the \`items\` property of the Flashbar component.",
37446+
"name": "findItemById",
37447+
"parameters": [
37448+
{
37449+
"flags": {
37450+
"isOptional": false,
37451+
},
37452+
"name": "id",
37453+
"typeName": "string",
37454+
},
37455+
],
37456+
"returnType": {
37457+
"isNullable": true,
37458+
"name": "FlashWrapper",
37459+
},
37460+
},
3744237461
{
3744337462
"description": "Returns the individual flashes of this flashbar.
3744437463

@@ -47270,6 +47289,25 @@ The mode selector is only rendered as a Select on narrow viewports. On wide view
4727047289
},
4727147290
{
4727247291
"methods": [
47292+
{
47293+
"description": "Returns a flash item by its id.
47294+
47295+
The id is matched against the \`id\` property of each item passed to the \`items\` property of the Flashbar component.",
47296+
"name": "findItemById",
47297+
"parameters": [
47298+
{
47299+
"flags": {
47300+
"isOptional": false,
47301+
},
47302+
"name": "id",
47303+
"typeName": "string",
47304+
},
47305+
],
47306+
"returnType": {
47307+
"isNullable": true,
47308+
"name": "FlashWrapper",
47309+
},
47310+
},
4727347311
{
4727447312
"description": "Returns the individual flashes of this flashbar.
4727547313

src/flashbar/__tests__/collapsible.test.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,25 @@ 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+
});
623642
});
624643

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

src/flashbar/__tests__/flashbar.test.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,21 @@ describe('Flashbar component', () => {
445445
testFlashDismissal({ stackItems: false });
446446
});
447447

448+
test('findItemById', () => {
449+
const wrapper = createFlashbarWrapper(
450+
<Flashbar
451+
items={[
452+
{ content: 'Flash 1', id: 'flash-1', type: 'success' },
453+
{ content: 'Flash 2', id: 'flash-2', type: 'error' },
454+
{ content: 'Flash 3', type: 'warning' },
455+
]}
456+
/>
457+
);
458+
expect(wrapper.findItemById('flash-1')!.findContent()!.getElement()).toHaveTextContent('Flash 1');
459+
expect(wrapper.findItemById('flash-2')!.findContent()!.getElement()).toHaveTextContent('Flash 2');
460+
expect(wrapper.findItemById('flash-3')).toBeNull();
461+
});
462+
448463
describe('rapid item replacement with animations', () => {
449464
beforeEach(() => {
450465
jest.useFakeTimers();

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)