Skip to content

Commit 1bc5f19

Browse files
authored
chore(atomic): migrate tab guard (#6197)
1 parent a417c3f commit 1bc5f19

File tree

4 files changed

+115
-1
lines changed

4 files changed

+115
-1
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import {html, nothing, render, type TemplateResult} from 'lit';
2+
import {describe, expect, it} from 'vitest';
3+
import {renderTabWrapper} from './tab-wrapper';
4+
5+
describe('renderTabWrapper', () => {
6+
const renderTabWrapperWithChildren = (
7+
props: {
8+
tabsIncluded?: string | string[];
9+
tabsExcluded?: string | string[];
10+
activeTab: string;
11+
},
12+
children: TemplateResult | typeof nothing = nothing
13+
) => {
14+
const container = document.createElement('div');
15+
document.body.appendChild(container);
16+
17+
render(
18+
renderTabWrapper({
19+
props: {
20+
tabsIncluded: props.tabsIncluded ?? [],
21+
tabsExcluded: props.tabsExcluded ?? [],
22+
activeTab: props.activeTab,
23+
},
24+
})(children),
25+
container
26+
);
27+
28+
return container;
29+
};
30+
31+
const testChild = html`<div class="child">Test Child</div>`;
32+
33+
it.each([
34+
{
35+
description: 'no tabs are specified',
36+
props: {activeTab: 'products'},
37+
},
38+
{
39+
description: 'activeTab is included in tabsIncluded',
40+
props: {tabsIncluded: ['products', 'services'], activeTab: 'products'},
41+
},
42+
{
43+
description: 'tabsIncluded is empty',
44+
props: {tabsIncluded: [], activeTab: 'products'},
45+
},
46+
{
47+
description: 'activeTab is empty and tabsIncluded is specified',
48+
props: {tabsIncluded: ['products'], activeTab: ''},
49+
},
50+
])('should render children when $description', ({props}) => {
51+
const container = renderTabWrapperWithChildren(props, testChild);
52+
const child = container.querySelector('.child');
53+
54+
expect(child).toBeInTheDocument();
55+
expect(child?.textContent).toBe('Test Child');
56+
57+
container.remove();
58+
});
59+
60+
it.each([
61+
{
62+
description: 'activeTab is excluded in tabsExcluded',
63+
props: {tabsExcluded: ['products', 'services'], activeTab: 'products'},
64+
},
65+
{
66+
description: 'activeTab is not included in tabsIncluded',
67+
props: {tabsIncluded: ['services', 'support'], activeTab: 'products'},
68+
},
69+
{
70+
description: 'activeTab is excluded even if also included',
71+
props: {
72+
tabsIncluded: ['products'],
73+
tabsExcluded: ['products'],
74+
activeTab: 'products',
75+
},
76+
},
77+
])('should not render children when $description', ({props}) => {
78+
const container = renderTabWrapperWithChildren(props, testChild);
79+
const child = container.querySelector('.child');
80+
81+
expect(child).not.toBeInTheDocument();
82+
83+
container.remove();
84+
});
85+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {nothing} from 'lit';
2+
import type {FunctionalComponentWithChildren} from '@/src/utils/functional-component-utils';
3+
import {shouldDisplayOnCurrentTab} from '@/src/utils/tab-utils';
4+
5+
interface TabWrapperProps {
6+
tabsIncluded: string | string[];
7+
tabsExcluded: string | string[];
8+
activeTab: string;
9+
}
10+
11+
export const renderTabWrapper: FunctionalComponentWithChildren<
12+
TabWrapperProps
13+
> =
14+
({props}) =>
15+
(children) => {
16+
const {tabsIncluded, tabsExcluded, activeTab} = props;
17+
18+
if (
19+
!shouldDisplayOnCurrentTab(
20+
[...tabsIncluded],
21+
[...tabsExcluded],
22+
activeTab
23+
)
24+
) {
25+
return nothing;
26+
}
27+
28+
return children;
29+
};

packages/atomic/src/components/search/result-lists/atomic-result-list/atomic-result-list.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import {
4242
ItemDisplayLayout,
4343
getItemListDisplayClasses,
4444
} from '../../../common/layout/display-options';
45-
import {TabGuard} from '../../../common/tabs/tab-guard';
45+
import {TabGuard} from '../../../common/tabs/stencil-tab-guard';
4646
import {Bindings} from '../../atomic-search-interface/atomic-search-interface';
4747

4848
/**

0 commit comments

Comments
 (0)