@inseefr/lunatic 3.6.6 → 3.6.8-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/README.md +25 -0
  2. package/components/Loop/Loop.d.ts +1 -1
  3. package/components/Loop/Loop.js +5 -27
  4. package/components/Loop/Loop.js.map +1 -1
  5. package/components/Loop/utils.d.ts +8 -0
  6. package/components/Loop/utils.js +66 -0
  7. package/components/Loop/utils.js.map +1 -0
  8. package/components/RosterForLoop/RosterForLoop.js +4 -27
  9. package/components/RosterForLoop/RosterForLoop.js.map +1 -1
  10. package/esm/components/Loop/Loop.d.ts +1 -1
  11. package/esm/components/Loop/Loop.js +5 -28
  12. package/esm/components/Loop/Loop.js.map +1 -1
  13. package/esm/components/Loop/utils.d.ts +8 -0
  14. package/esm/components/Loop/utils.js +63 -0
  15. package/esm/components/Loop/utils.js.map +1 -0
  16. package/esm/components/RosterForLoop/RosterForLoop.js +5 -29
  17. package/esm/components/RosterForLoop/RosterForLoop.js.map +1 -1
  18. package/esm/use-lunatic/commons/compile-controls.js +8 -0
  19. package/esm/use-lunatic/commons/compile-controls.js.map +1 -1
  20. package/esm/use-lunatic/props/propIterations.js +12 -1
  21. package/esm/use-lunatic/props/propIterations.js.map +1 -1
  22. package/esm/use-lunatic/reducer/controls/check-base-control.js.map +1 -1
  23. package/package.json +15 -3
  24. package/src/components/Loop/Loop.tsx +6 -35
  25. package/src/components/Loop/utils.test.ts +61 -0
  26. package/src/components/Loop/utils.ts +74 -0
  27. package/src/components/RosterForLoop/RosterForLoop.tsx +3 -33
  28. package/src/stories/behaviour/controls/controls.stories.tsx +12 -3
  29. package/src/stories/behaviour/controls/data-standalone-loop.json +17 -0
  30. package/src/stories/behaviour/controls/source-standalone-loop.json +5503 -0
  31. package/src/stories/loop/source-paginated.json +1 -1
  32. package/src/stories/roundabout/sourceWithControl.json +14 -0
  33. package/src/use-lunatic/commons/compile-controls.ts +13 -0
  34. package/src/use-lunatic/props/propIterations.ts +30 -15
  35. package/src/use-lunatic/reducer/controls/check-base-control.ts +0 -1
  36. package/src/use-lunatic/use-lunatic-bug.test.ts +42 -0
  37. package/tsconfig.build.tsbuildinfo +1 -1
  38. package/use-lunatic/commons/compile-controls.js +7 -0
  39. package/use-lunatic/commons/compile-controls.js.map +1 -1
  40. package/use-lunatic/props/propIterations.js +12 -1
  41. package/use-lunatic/props/propIterations.js.map +1 -1
  42. package/use-lunatic/reducer/controls/check-base-control.js.map +1 -1
  43. /package/src/stories/behaviour/controls/{source-loop.json → source-roster-for-loop.json} +0 -0
@@ -93,7 +93,7 @@
93
93
  "variableType": "COLLECTED",
94
94
  "name": "PRENOM",
95
95
  "values": {
96
- "COLLECTED": ["John", "Jane"]
96
+ "COLLECTED": []
97
97
  }
98
98
  },
99
99
  {
@@ -347,6 +347,20 @@
347
347
  },
348
348
  "locked": false,
349
349
  "controls": [
350
+ {
351
+ "id": "lypxwut2-CI-3",
352
+ "type": "ROW",
353
+ "control": {
354
+ "type": "VTL",
355
+ "value": "not(AGE = \"null\")"
356
+ },
357
+ "criticality": "WARN",
358
+ "errorMessage": {
359
+ "type": "VTL|MD",
360
+ "value": "\"Vous n'avez pas renseigné l'âge de \" || PRENOM || \".\" "
361
+ },
362
+ "typeOfControl": "CONSISTENCY"
363
+ },
350
364
  {
351
365
  "id": "lypxwut2-CI-1",
352
366
  "type": "SIMPLE",
@@ -217,6 +217,19 @@ function checkComponentInLoop(
217
217
  if (!('controls' in component) || !component.controls) {
218
218
  continue;
219
219
  }
220
+
221
+ // For Roundabout, we don't check controls for iterations that are disabled
222
+ if (component.componentType === 'Roundabout' && component.item.disabled) {
223
+ const isIterationDisabled = state.executeExpression(
224
+ { value: component.item.disabled?.value, type: 'VTL' },
225
+ iterationPager
226
+ );
227
+
228
+ if (isIterationDisabled) {
229
+ continue;
230
+ }
231
+ }
232
+
220
233
  // The component is filtered on this iteration, skip it
221
234
  if (
222
235
  // conditionFilter can be the interpreted expression, or the object representing the expression
@@ -32,20 +32,35 @@ export function getIterationsProp(
32
32
 
33
33
  // Iterations expression is not present on the component definition
34
34
  // infer it from the value of child components
35
- return (definition.components as LunaticComponentDefinition[]).reduce(
36
- (acc, component) => {
37
- if (!hasResponse(component)) {
38
- return acc;
39
- }
40
- const value = state.variables.get(
41
- component.response.name,
42
- isNumber(state.pager.iteration) ? [state.pager.iteration] : undefined
43
- );
44
- if (Array.isArray(value) && value.length > acc) {
45
- return value.length;
46
- }
35
+
36
+ return getFlatComponentsInLoop(definition).reduce((acc, component) => {
37
+ if (!hasResponse(component)) {
47
38
  return acc;
48
- },
49
- 0
50
- );
39
+ }
40
+ const value = state.variables.get(
41
+ component.response.name,
42
+ isNumber(state.pager.iteration) ? [state.pager.iteration] : undefined
43
+ );
44
+ if (Array.isArray(value) && value.length > acc) {
45
+ return value.length;
46
+ }
47
+ return acc;
48
+ }, 0);
49
+ }
50
+
51
+ function getFlatComponentsInLoop(
52
+ definition: LunaticComponentDefinition
53
+ ): LunaticComponentDefinition[] {
54
+ if (
55
+ definition.componentType !== 'RosterForLoop' &&
56
+ definition.componentType !== 'Loop'
57
+ ) {
58
+ return [definition];
59
+ }
60
+
61
+ return definition.components.reduce((acc, component) => {
62
+ if (component.componentType === 'Question')
63
+ return [...acc, ...component.components];
64
+ return [...acc, component];
65
+ }, [] as LunaticComponentDefinition[]);
51
66
  }
@@ -21,7 +21,6 @@ export function checkBaseControl(
21
21
  const result = executeExpression(control, {
22
22
  iteration: linksIterations ?? iteration,
23
23
  });
24
-
25
24
  try {
26
25
  /**
27
26
  * Currently, the controls are lifted when the condition is false.
@@ -0,0 +1,42 @@
1
+ import { act, renderHook } from '@testing-library/react';
2
+ import { describe, expect, it } from 'vitest';
3
+
4
+ import loopControlSource from '../stories/behaviour/controls/source-standalone-loop.json';
5
+ import surveyUnit from '../stories/behaviour/controls/data-standalone-loop.json';
6
+ import { useLunatic } from './use-lunatic';
7
+
8
+ describe('use-lunatic()', () => {
9
+ /**
10
+ * This test should be succeded after make some improvment
11
+ * See: comment above useEffect of src/components/Loop/utils.ts > useLoopUtils
12
+ */
13
+ it.skip('should compile errors correctly', () => {
14
+ const { result } = renderHook(() =>
15
+ useLunatic(loopControlSource, surveyUnit.data)
16
+ );
17
+ act(() => {
18
+ result.current.goToPage({ page: '3' });
19
+ });
20
+ const { currentErrors } = result.current.compileControls();
21
+ expect(currentErrors).not.toBeUndefined();
22
+ expect(Object.keys(currentErrors!).length).toEqual(6);
23
+ });
24
+ it('should compile errors correctly 2', () => {
25
+ const { result } = renderHook(() =>
26
+ useLunatic(loopControlSource, {
27
+ COLLECTED: {
28
+ PRES_SAL: { COLLECTED: ['1', null, '2', null, null, null] },
29
+ },
30
+ ...surveyUnit.data,
31
+ })
32
+ );
33
+ act(() => {
34
+ result.current.goToPage({ page: '3' });
35
+ });
36
+ act(() => {
37
+ const { currentErrors } = result.current.compileControls();
38
+ expect(currentErrors).not.toBeUndefined();
39
+ expect(Object.keys(currentErrors!).length).toEqual(4);
40
+ });
41
+ });
42
+ });