|
| 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 | +}); |
0 commit comments