-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
activeWhen
prop to Wizard.Step
- Loading branch information
There are no files selected for viewing
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 GitHub Actions / Run tests and checks
|
||
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 GitHub Actions / Run tests and checks
|
||
import { FilterData, Provider } from '../../../DataContext' | ||
Check failure on line 4 in packages/dnb-eufemia/src/extensions/forms/Form/Visibility/__tests__/useVisibility.test.tsx GitHub Actions / Run tests and checks
|
||
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 GitHub Actions / Run tests and checks
Check failure on line 6 in packages/dnb-eufemia/src/extensions/forms/Form/Visibility/__tests__/useVisibility.test.tsx GitHub Actions / Run tests and checks
Check failure on line 6 in packages/dnb-eufemia/src/extensions/forms/Form/Visibility/__tests__/useVisibility.test.tsx GitHub Actions / Run tests and checks
|
||
import { Flex } from '../../../../../components' | ||
Check failure on line 7 in packages/dnb-eufemia/src/extensions/forms/Form/Visibility/__tests__/useVisibility.test.tsx GitHub Actions / Run tests and checks
|
||
import { P } from '../../../../../elements' | ||
Check failure on line 8 in packages/dnb-eufemia/src/extensions/forms/Form/Visibility/__tests__/useVisibility.test.tsx GitHub Actions / Run tests and checks
|
||
|
||
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) | ||
}) | ||
}) | ||
}) |