-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add conditional render helper and border highlighting (#727)
Co-authored-by: Jonathan Norris <jonathan@taplytics.com>
- Loading branch information
1 parent
e5cd2f3
commit 70c90f2
Showing
9 changed files
with
301 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { RenderIfEnabled } from './RenderIfEnabled' | ||
import { render } from '@testing-library/react' | ||
import { useVariableValue } from './useVariableValue' | ||
import '@testing-library/jest-dom' | ||
|
||
const mockedUseVariable = jest.mocked(useVariableValue) | ||
|
||
jest.mock('./useVariableValue') | ||
|
||
describe('RenderIfEnabled', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
it('should render children when variable is set to target value', () => { | ||
const variableKey = 'test-key' | ||
const targetValue = true | ||
const defaultValue = false | ||
const children = <div>Test</div> | ||
mockedUseVariable.mockReturnValue(targetValue) | ||
const { getByText } = render( | ||
<RenderIfEnabled | ||
variableKey={variableKey} | ||
targetValue={targetValue} | ||
defaultValue={defaultValue} | ||
> | ||
{children} | ||
</RenderIfEnabled>, | ||
) | ||
expect(mockedUseVariable).toHaveBeenCalledWith( | ||
variableKey, | ||
defaultValue, | ||
) | ||
expect(getByText('Test')).toBeInTheDocument() | ||
}) | ||
|
||
it('should render children when variable is set to target value (string)', () => { | ||
const variableKey = 'test-key' | ||
const targetValue = 'test-value' | ||
const defaultValue = 'not-test-value' | ||
const children = <div>Test</div> | ||
mockedUseVariable.mockReturnValue(targetValue) | ||
const { getByText } = render( | ||
<RenderIfEnabled | ||
variableKey={variableKey} | ||
targetValue={targetValue} | ||
defaultValue={defaultValue} | ||
> | ||
{children} | ||
</RenderIfEnabled>, | ||
) | ||
expect(getByText('Test')).toBeInTheDocument() | ||
}) | ||
|
||
it('should not render children when variable is set to something else', () => { | ||
const variableKey = 'test-key' | ||
const targetValue = 'test-value' | ||
const defaultValue = 'not-test-value' | ||
const children = <div>Test</div> | ||
mockedUseVariable.mockReturnValue('something else') | ||
const { queryByText } = render( | ||
<RenderIfEnabled | ||
variableKey={variableKey} | ||
targetValue={targetValue} | ||
defaultValue={defaultValue} | ||
> | ||
{children} | ||
</RenderIfEnabled>, | ||
) | ||
expect(queryByText('Test')).toBeNull() | ||
}) | ||
|
||
it('should render children when variable is boolean enabled and target not specified', () => { | ||
const variableKey = 'test-key' | ||
const children = <div>Test</div> | ||
mockedUseVariable.mockReturnValue(true) | ||
const { getByText } = render( | ||
<RenderIfEnabled variableKey={variableKey}> | ||
{children} | ||
</RenderIfEnabled>, | ||
) | ||
expect(getByText('Test')).toBeInTheDocument() | ||
expect(mockedUseVariable).toHaveBeenCalledWith(variableKey, false) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import useVariableValue from './useVariableValue' | ||
import { DVCVariableValue } from '@devcycle/js-client-sdk' | ||
import { useContext } from 'react' | ||
import { debugContext } from './context' | ||
|
||
type CommonProps = { | ||
children: React.ReactNode | ||
variableKey: string | ||
} | ||
|
||
type RenderIfEnabledProps<T extends DVCVariableValue> = | ||
| CommonProps | ||
| (CommonProps & { | ||
targetValue: T | ||
defaultValue: T | ||
}) | ||
|
||
export const RenderIfEnabled = <T extends DVCVariableValue>( | ||
props: RenderIfEnabledProps<T>, | ||
): React.ReactNode => { | ||
let targetValue: DVCVariableValue | ||
let defaultValue: DVCVariableValue | ||
if ('targetValue' in props) { | ||
targetValue = props.targetValue | ||
defaultValue = props.defaultValue | ||
} else { | ||
targetValue = true | ||
defaultValue = false | ||
} | ||
|
||
const variableValue = useVariableValue(props.variableKey, defaultValue) | ||
const debugSettings = useContext(debugContext) | ||
|
||
if (variableValue === targetValue) { | ||
if (debugSettings.showConditionalBorders) { | ||
return ( | ||
<div | ||
style={{ | ||
border: `2px solid ${debugSettings.borderColor}`, | ||
position: 'relative', | ||
}} | ||
className={`devcycle-conditional-border devcycle-conditional-border-${props.variableKey}`} | ||
> | ||
<a | ||
style={{ | ||
position: 'absolute', | ||
cursor: 'pointer', | ||
right: '-2px', | ||
top: '-2.5rem', | ||
color: 'white', | ||
fontSize: '1.5rem', | ||
padding: '2px 5px', | ||
backgroundColor: `${debugSettings.borderColor}`, | ||
}} | ||
target={'_blank'} | ||
href={`https://app.devcycle.com/r/variables/${props.variableKey}`} | ||
rel="noreferrer" | ||
> | ||
{props.variableKey}: {JSON.stringify(variableValue)} | ||
</a> | ||
{props.children} | ||
</div> | ||
) | ||
} | ||
return <>{props.children}</> | ||
} | ||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useVariableValue } from './useVariableValue' | ||
import { SwapComponents } from './SwapComponents' | ||
import { render } from '@testing-library/react' | ||
import '@testing-library/jest-dom' | ||
|
||
const mockedUseVariable = jest.mocked(useVariableValue) | ||
|
||
jest.mock('./useVariableValue') | ||
const oldComponent = (props: { test: boolean }) => { | ||
return <div>Old Component</div> | ||
} | ||
|
||
const newComponent = (props: { test: boolean }) => { | ||
return <div>New Component</div> | ||
} | ||
|
||
const mismatchProps = (props: { something: boolean }) => { | ||
return <div>Mismatch Props</div> | ||
} | ||
|
||
const RenderSwapped = () => { | ||
const Swapped = SwapComponents(oldComponent, newComponent, 'test-key') | ||
return ( | ||
<span> | ||
<Swapped test={true} /> | ||
</span> | ||
) | ||
} | ||
|
||
const MismatchPropsError = () => { | ||
// @ts-expect-error should complain about non-matching props | ||
const Swapped = SwapComponents(oldComponent, mismatchProps, 'test-key') | ||
return <span></span> | ||
} | ||
|
||
const PropTypeError = () => { | ||
const Swapped = SwapComponents(oldComponent, newComponent, 'test-key') | ||
return ( | ||
<span> | ||
{/*@ts-expect-error should complain test prop is missing*/} | ||
<Swapped /> | ||
</span> | ||
) | ||
} | ||
|
||
describe('SwapComponents', () => { | ||
it('should render the old component if the variable is not enabled', () => { | ||
mockedUseVariable.mockReturnValue(false) | ||
const { getByText } = render(<RenderSwapped />) | ||
expect(getByText('Old Component')).toBeInTheDocument() | ||
}) | ||
it('should render the new component if the variable is enabled', () => { | ||
mockedUseVariable.mockReturnValue(true) | ||
const { getByText } = render(<RenderSwapped />) | ||
expect(getByText('New Component')).toBeInTheDocument() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { ComponentProps, ComponentType } from 'react' | ||
import useVariableValue from './useVariableValue' | ||
|
||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
export const SwapComponents = <T extends ComponentType<any>>( | ||
OldComponent: T, | ||
NewComponent: T, | ||
variableKey: string, | ||
) => { | ||
const DevCycleConditionalComponent = ( | ||
props: ComponentProps<T>, | ||
): React.ReactElement => { | ||
const variableValue = useVariableValue(variableKey, false) | ||
if (variableValue) { | ||
return <NewComponent {...props} /> | ||
} else { | ||
return <OldComponent {...props} /> | ||
} | ||
} | ||
|
||
return DevCycleConditionalComponent | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters