Skip to content

Commit

Permalink
feat(forms): add activeWhen prop to Wizard.Step
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Jun 13, 2024
1 parent 5a2d95f commit a1bced2
Show file tree
Hide file tree
Showing 9 changed files with 664 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { UseFieldProps } from '../../types'
import type { DataAttributes } from '../../hooks/useFieldProps'
import { FilterData } from '../../DataContext'

type VisibleWhen =
export type VisibleWhen =
| {
path: string
hasValue: unknown
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,352 @@
import React from 'react'
import { renderHook, screen } from '@testing-library/react'

Check failure on line 2 in packages/dnb-eufemia/src/extensions/forms/Form/Visibility/__tests__/useVisibility.test.tsx

View workflow job for this annotation

GitHub Actions / Run tests and checks

'screen' is defined but never used

Check failure on line 2 in packages/dnb-eufemia/src/extensions/forms/Form/Visibility/__tests__/useVisibility.test.tsx

View workflow job for this annotation

GitHub Actions / Run tests and checks

'screen' is defined but never used
import userEvent from '@testing-library/user-event'

Check failure on line 3 in packages/dnb-eufemia/src/extensions/forms/Form/Visibility/__tests__/useVisibility.test.tsx

View workflow job for this annotation

GitHub Actions / Run tests and checks

'userEvent' is defined but never used

Check failure on line 3 in packages/dnb-eufemia/src/extensions/forms/Form/Visibility/__tests__/useVisibility.test.tsx

View workflow job for this annotation

GitHub Actions / Run tests and checks

'userEvent' is defined but never used
import { FilterData, Provider } from '../../../DataContext'

Check failure on line 4 in packages/dnb-eufemia/src/extensions/forms/Form/Visibility/__tests__/useVisibility.test.tsx

View workflow job for this annotation

GitHub Actions / Run tests and checks

'FilterData' is defined but never used

Check failure on line 4 in packages/dnb-eufemia/src/extensions/forms/Form/Visibility/__tests__/useVisibility.test.tsx

View workflow job for this annotation

GitHub Actions / Run tests and checks

'FilterData' is defined but never used
import useVisibility from '../useVisibility'
import { Field, Form } from '../../..'

Check failure on line 6 in packages/dnb-eufemia/src/extensions/forms/Form/Visibility/__tests__/useVisibility.test.tsx

View workflow job for this annotation

GitHub Actions / Run tests and checks

'Field' is defined but never used

Check failure on line 6 in packages/dnb-eufemia/src/extensions/forms/Form/Visibility/__tests__/useVisibility.test.tsx

View workflow job for this annotation

GitHub Actions / Run tests and checks

'Form' is defined but never used

Check failure on line 6 in packages/dnb-eufemia/src/extensions/forms/Form/Visibility/__tests__/useVisibility.test.tsx

View workflow job for this annotation

GitHub Actions / Run tests and checks

'Field' is defined but never used

Check failure on line 6 in packages/dnb-eufemia/src/extensions/forms/Form/Visibility/__tests__/useVisibility.test.tsx

View workflow job for this annotation

GitHub Actions / Run tests and checks

'Form' is defined but never used
import { Flex } from '../../../../../components'

Check failure on line 7 in packages/dnb-eufemia/src/extensions/forms/Form/Visibility/__tests__/useVisibility.test.tsx

View workflow job for this annotation

GitHub Actions / Run tests and checks

'Flex' is defined but never used

Check failure on line 7 in packages/dnb-eufemia/src/extensions/forms/Form/Visibility/__tests__/useVisibility.test.tsx

View workflow job for this annotation

GitHub Actions / Run tests and checks

'Flex' is defined but never used
import { P } from '../../../../../elements'

Check failure on line 8 in packages/dnb-eufemia/src/extensions/forms/Form/Visibility/__tests__/useVisibility.test.tsx

View workflow job for this annotation

GitHub Actions / Run tests and checks

'P' is defined but never used

Check failure on line 8 in packages/dnb-eufemia/src/extensions/forms/Form/Visibility/__tests__/useVisibility.test.tsx

View workflow job for this annotation

GitHub Actions / Run tests and checks

'P' is defined but never used

describe('useVisibility', () => {
describe('visibility', () => {
it('renders children when visible is true', () => {
const { result } = renderHook(() =>
useVisibility({
visible: true,
})
)
expect(result.current.check()).toBe(true)
})

it('does not render children when visible is false', () => {
const { result } = renderHook(() =>
useVisibility({
visible: false,
})
)
expect(result.current.check()).toBe(false)
})
})

describe('pathDefined', () => {
it('renders children when target path is defined', () => {
const { result } = renderHook(
() =>
useVisibility({
pathDefined: '/isDefined',
}),
{
wrapper: ({ children }) => (
<Provider data={{ isDefined: 'foo' }}>{children}</Provider>
),
}
)
expect(result.current.check()).toBe(true)
})

it('does not render children when target path is not defined', () => {
const { result } = renderHook(
() =>
useVisibility({
pathDefined: '/notDefined',
}),
{
wrapper: ({ children }) => (
<Provider data={{ isDefined: 'foo' }}>{children}</Provider>
),
}
)
expect(result.current.check()).toBe(false)
})
})

describe('pathUndefined', () => {
it('renders children when target path is defined', () => {
const { result } = renderHook(
() =>
useVisibility({
pathUndefined: '/isDefined',
}),
{
wrapper: ({ children }) => (
<Provider data={{ isDefined: 'foo' }}>{children}</Provider>
),
}
)
expect(result.current.check()).toBe(false)
})

it('does not render children when target path is not defined', () => {
const { result } = renderHook(
() =>
useVisibility({
pathUndefined: '/notDefined',
}),
{
wrapper: ({ children }) => (
<Provider data={{ isDefined: 'foo' }}>{children}</Provider>
),
}
)
expect(result.current.check()).toBe(true)
})
})

describe('pathTruthy', () => {
it('renders children when target path is truthy', () => {
const { result } = renderHook(
() =>
useVisibility({
pathTruthy: '/isTruthy',
}),
{
wrapper: ({ children }) => (
<Provider data={{ isTruthy: 'value' }}>{children}</Provider>
),
}
)
expect(result.current.check()).toBe(true)
})

it('does not render children when target path is not truthy', () => {
const { result } = renderHook(
() =>
useVisibility({
pathTruthy: '/isFalsy',
}),
{
wrapper: ({ children }) => (
<Provider data={{ isFalsy: null }}>{children}</Provider>
),
}
)
expect(result.current.check()).toBe(false)
})

it('does not render children when target path is not defined', () => {
const { result } = renderHook(
() =>
useVisibility({
pathTruthy: '/isNotDefined',
}),
{
wrapper: ({ children }) => (
<Provider data={{ isFalse: false }}>{children}</Provider>
),
}
)
expect(result.current.check()).toBe(false)
})
})

describe('pathFalsy', () => {
it('renders children when target path is falsy', () => {
const { result } = renderHook(
() =>
useVisibility({
pathFalsy: '/isFalsy',
}),
{
wrapper: ({ children }) => (
<Provider data={{ isFalsy: null }}>{children}</Provider>
),
}
)
expect(result.current.check()).toBe(true)
})

it('renders children when target path is not defined', () => {
const { result } = renderHook(
() =>
useVisibility({
pathFalsy: '/isNotDefined',
}),
{
wrapper: ({ children }) => (
<Provider data={{ isFalse: false }}>{children}</Provider>
),
}
)
expect(result.current.check()).toBe(true)
})

it('does not render children when target path is not falsy', () => {
const { result } = renderHook(
() =>
useVisibility({
pathFalsy: '/isTruthy',
}),
{
wrapper: ({ children }) => (
<Provider data={{ isTruthy: 'value' }}>{children}</Provider>
),
}
)
expect(result.current.check()).toBe(false)
})
})

describe('pathValue', () => {
it('renders children when target path and value matches', () => {
const { result } = renderHook(
() =>
useVisibility({
pathValue: '/myPath',
whenValue: 'checked',
}),
{
wrapper: ({ children }) => (
<Provider data={{ myPath: 'checked' }}>{children}</Provider>
),
}
)
expect(result.current.check()).toBe(true)
})

it('does not render children when target path not not value matches', () => {
const { result } = renderHook(
() =>
useVisibility({
pathValue: '/myPath',
whenValue: 'not-checked',
}),
{
wrapper: ({ children }) => (
<Provider data={{ myPath: 'checked' }}>{children}</Provider>
),
}
)
expect(result.current.check()).toBe(false)
})
})

describe('visibleWhen', () => {
it('should render children when hasValue matches', () => {
const { result } = renderHook(
() =>
useVisibility({
visibleWhen: {
path: '/myPath',
hasValue: 'foo',
},
}),
{
wrapper: ({ children }) => (
<Provider data={{ myPath: 'foo' }}>{children}</Provider>
),
}
)
expect(result.current.check()).toBe(true)
})

it('should not render children when hasValue not matches', () => {
const { result } = renderHook(
() =>
useVisibility({
visibleWhen: {
path: '/myPath',
hasValue: 'bar',
},
}),
{
wrapper: ({ children }) => (
<Provider data={{ myPath: 'foo' }}>{children}</Provider>
),
}
)
expect(result.current.check()).toBe(false)
})

it('should not render children when path not matches', () => {
const { result } = renderHook(
() =>
useVisibility({
visibleWhen: {
path: '/nonExistingPath',
hasValue: 'foo',
},
}),
{
wrapper: ({ children }) => (
<Provider data={{ myPath: 'foo' }}>{children}</Provider>
),
}
)
expect(result.current.check()).toBe(false)
})

it('should render children when withValue matches', () => {
const { result } = renderHook(
() =>
useVisibility({
visibleWhen: {
path: '/myPath',
withValue: (value) => value === 'foo',
},
}),
{
wrapper: ({ children }) => (
<Provider data={{ myPath: 'foo' }}>{children}</Provider>
),
}
)
expect(result.current.check()).toBe(true)
})

it('should not render children when withValue not matches', () => {
const { result } = renderHook(
() =>
useVisibility({
visibleWhen: {
path: '/myPath',
withValue: (value) => value === 'bar',
},
}),
{
wrapper: ({ children }) => (
<Provider data={{ myPath: 'foo' }}>{children}</Provider>
),
}
)
expect(result.current.check()).toBe(false)
})
})

describe('visibleWhenNot', () => {
it('should render children when hasValue matches', () => {
const { result } = renderHook(
() =>
useVisibility({
visibleWhenNot: {
path: '/myPath',
hasValue: 'foo',
},
}),
{
wrapper: ({ children }) => (
<Provider data={{ myPath: 'foo' }}>{children}</Provider>
),
}
)
expect(result.current.check()).toBe(false)
})

it('should not render children when hasValue not matches', () => {
const { result } = renderHook(
() =>
useVisibility({
visibleWhenNot: {
path: '/myPath',
hasValue: 'bar',
},
}),
{
wrapper: ({ children }) => (
<Provider data={{ myPath: 'foo' }}>{children}</Provider>
),
}
)
expect(result.current.check()).toBe(true)
})
})
})
Loading

0 comments on commit a1bced2

Please sign in to comment.