-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(forms): add
activeWhen
prop to Wizard.Step
Co-authored-by: Joakim Bjerknes <joakbjerk@gmail.com>
- Loading branch information
1 parent
70b1f8b
commit 3cde4f8
Showing
9 changed files
with
660 additions
and
98 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
348 changes: 348 additions & 0 deletions
348
packages/dnb-eufemia/src/extensions/forms/Form/Visibility/__tests__/useVisibility.test.tsx
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,348 @@ | ||
import React from 'react' | ||
import { renderHook } from '@testing-library/react' | ||
import { Provider } from '../../../DataContext' | ||
import useVisibility from '../useVisibility' | ||
|
||
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 does not match value', () => { | ||
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 does not match', () => { | ||
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 does not match', () => { | ||
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 does not match', () => { | ||
const { result } = renderHook( | ||
() => | ||
useVisibility({ | ||
visibleWhenNot: { | ||
path: '/myPath', | ||
hasValue: 'bar', | ||
}, | ||
}), | ||
{ | ||
wrapper: ({ children }) => ( | ||
<Provider data={{ myPath: 'foo' }}>{children}</Provider> | ||
), | ||
} | ||
) | ||
expect(result.current.check()).toBe(true) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.