-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: handle question component in usePageHasResponse (#1199)
- Loading branch information
1 parent
d389eae
commit 33ac488
Showing
3 changed files
with
205 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import { describe, expect, it, vi } from 'vitest'; | ||
import { usePageHasResponse } from './use-page-has-response'; | ||
import { type LunaticComponentProps } from '../../components/type'; | ||
import { renderHook } from '@testing-library/react'; | ||
|
||
const defaultComponentValues = { | ||
id: 'a', | ||
value: null, | ||
handleChanges: vi.fn(), | ||
}; | ||
|
||
describe('usePageHasResponse', () => { | ||
it('should be true when there are no components', () => { | ||
const components: LunaticComponentProps[] = []; | ||
const { result } = renderHook(() => | ||
usePageHasResponse(components, vi.fn()) | ||
); | ||
expect(result.current()).toBeTruthy(); | ||
}); | ||
|
||
it('should be false when there is no value', () => { | ||
const components: LunaticComponentProps[] = [ | ||
{ ...defaultComponentValues, componentType: 'Text' }, | ||
]; | ||
const { result } = renderHook(() => | ||
usePageHasResponse(components, vi.fn()) | ||
); | ||
expect(result.current()).toBeFalsy(); | ||
}); | ||
|
||
it('should be true when there is a value', () => { | ||
const components: LunaticComponentProps[] = [ | ||
{ | ||
...defaultComponentValues, | ||
componentType: 'Text', | ||
value: 'my awesome value', | ||
}, | ||
]; | ||
const { result } = renderHook(() => | ||
usePageHasResponse(components, vi.fn()) | ||
); | ||
expect(result.current()).toBeTruthy(); | ||
}); | ||
|
||
it('should be true for a Question component', () => { | ||
const components: LunaticComponentProps[] = [ | ||
{ | ||
...defaultComponentValues, | ||
componentType: 'Question', | ||
components: [ | ||
{ | ||
...defaultComponentValues, | ||
componentType: 'Text', | ||
}, | ||
], | ||
value: { t1: 'my awesome value' }, | ||
}, | ||
]; | ||
const { result } = renderHook(() => | ||
usePageHasResponse(components, vi.fn()) | ||
); | ||
expect(result.current()).toBeTruthy(); | ||
}); | ||
|
||
it('should be true when there is a missing response', () => { | ||
const components: LunaticComponentProps[] = [ | ||
{ | ||
...defaultComponentValues, | ||
componentType: 'Text', | ||
missingResponse: { name: 'a_MISSING', value: 'my missing value' }, | ||
}, | ||
]; | ||
const { result } = renderHook(() => | ||
usePageHasResponse(components, vi.fn()) | ||
); | ||
expect(result.current()).toBeTruthy(); | ||
}); | ||
|
||
it('should be true for a QCM table', () => { | ||
const mockExecuteExpression = vi.fn(); | ||
mockExecuteExpression.mockReturnValueOnce('my value'); | ||
const components: LunaticComponentProps[] = [ | ||
{ | ||
...defaultComponentValues, | ||
componentType: 'Table', | ||
body: [ | ||
[ | ||
{ | ||
...defaultComponentValues, | ||
id: 't1', | ||
componentType: 'CheckboxBoolean', | ||
response: { name: 't1' }, | ||
}, | ||
], | ||
], | ||
header: [{ label: 'my label' }], | ||
executeExpression: vi.fn(), | ||
iteration: 0, | ||
value: {}, | ||
}, | ||
]; | ||
const { result } = renderHook(() => | ||
usePageHasResponse(components, mockExecuteExpression) | ||
); | ||
expect(result.current()).toBeTruthy(); | ||
}); | ||
|
||
it('should be true for a QCM table in a Question', () => { | ||
const mockExecuteExpression = vi.fn(); | ||
mockExecuteExpression.mockReturnValueOnce('my value'); | ||
const components: LunaticComponentProps[] = [ | ||
{ | ||
...defaultComponentValues, | ||
componentType: 'Question', | ||
components: [ | ||
{ | ||
...defaultComponentValues, | ||
componentType: 'Table', | ||
body: [ | ||
[ | ||
{ | ||
...defaultComponentValues, | ||
id: 't1', | ||
componentType: 'CheckboxBoolean', | ||
response: { name: 't1' }, | ||
}, | ||
], | ||
], | ||
header: [{ label: 'my label' }], | ||
executeExpression: vi.fn(), | ||
iteration: 0, | ||
value: { t1: true }, | ||
}, | ||
], | ||
value: {}, | ||
}, | ||
]; | ||
const { result } = renderHook(() => | ||
usePageHasResponse(components, mockExecuteExpression) | ||
); | ||
expect(result.current()).toBeTruthy(); | ||
}); | ||
}); |
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