|
| 1 | +import React from 'react' |
| 2 | +import {render} from '@testing-library/react' |
| 3 | +import {FeatureFlags} from '../../../FeatureFlags' |
| 4 | +import {toggleStyledComponent} from '../toggleStyledComponent' |
| 5 | +import styled from 'styled-components' |
| 6 | + |
| 7 | +describe('toggleStyledComponent', () => { |
| 8 | + test('renders the `as` prop when flag is enabled', () => { |
| 9 | + const TestComponent = toggleStyledComponent('testFeatureFlag', () => <div />) |
| 10 | + const {container} = render( |
| 11 | + <FeatureFlags flags={{testFeatureFlag: true}}> |
| 12 | + <TestComponent as="button" /> |
| 13 | + </FeatureFlags>, |
| 14 | + ) |
| 15 | + expect(container.firstChild).toBeInstanceOf(HTMLButtonElement) |
| 16 | + }) |
| 17 | + |
| 18 | + test('renders a div as fallback when flag is enabled and no `as` prop is provided', () => { |
| 19 | + const TestComponent = toggleStyledComponent('testFeatureFlag', () => <div />) |
| 20 | + const {container} = render( |
| 21 | + <FeatureFlags flags={{testFeatureFlag: true}}> |
| 22 | + <TestComponent /> |
| 23 | + </FeatureFlags>, |
| 24 | + ) |
| 25 | + expect(container.firstChild).toBeInstanceOf(HTMLDivElement) |
| 26 | + }) |
| 27 | + |
| 28 | + test('renders Box with `as` if `sx` is provided and flag is enabled', () => { |
| 29 | + const TestComponent = toggleStyledComponent('testFeatureFlag', () => styled.div``) |
| 30 | + const {container} = render( |
| 31 | + <FeatureFlags flags={{testFeatureFlag: true}}> |
| 32 | + <TestComponent as="button" sx={{color: 'red'}} /> |
| 33 | + </FeatureFlags>, |
| 34 | + ) |
| 35 | + |
| 36 | + expect(container.firstChild).toBeInstanceOf(HTMLButtonElement) |
| 37 | + expect(container.firstChild).toHaveStyle('color: red') |
| 38 | + }) |
| 39 | + |
| 40 | + test('renders styled component when flag is disabled', () => { |
| 41 | + const StyledComponent = toggleStyledComponent('testFeatureFlag', styled.div.attrs({['data-styled']: true})``) |
| 42 | + const {container} = render( |
| 43 | + <FeatureFlags flags={{testFeatureFlag: false}}> |
| 44 | + <StyledComponent /> |
| 45 | + </FeatureFlags>, |
| 46 | + ) |
| 47 | + expect(container.firstChild).toHaveAttribute('data-styled') |
| 48 | + }) |
| 49 | +}) |
0 commit comments