Skip to content

Commit e8cc8a9

Browse files
authored
Merge pull request #1484 from navikt/fix/migration-nested-properties
Fix migration dryrun on nested properties
2 parents cba6cc1 + 66a866d commit e8cc8a9

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

packages/bygger-backend/src/migration/filterUtils.test.ts

+19-7
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1+
import { Component } from '@navikt/skjemadigitalisering-shared-domain';
12
import { componentHasDependencyMatchingFilters, getPropertyFromTarget, targetMatchesFilters } from './filterUtils';
23
import {
34
componentWithAdvancedConditionalToRadio,
45
componentWithSimpleConditionalToRadio,
56
formWithSimpleConditionalToRadio,
7+
originalOtherDocumentationAttachmentComponent,
68
originalTextFieldComponent,
79
} from './testData';
810

911
describe('filterUtils', () => {
1012
describe('getPropertyFromComponent', () => {
1113
it('gets the value of a property in the object as a string', () => {
12-
const actual = getPropertyFromTarget({ value: 'the value' }, ['value']);
14+
const actual = getPropertyFromTarget({ value: 'the value' } as unknown as Component, ['value']);
1315
expect(actual).toBe('the value');
1416
});
1517

1618
it('gets properties from nested objects', () => {
17-
const actual = getPropertyFromTarget({ firstLevel: { secondLevel: { thirdLevel: { value: 'the value' } } } }, [
18-
'firstLevel',
19-
'secondLevel',
20-
'thirdLevel',
21-
'value',
22-
]);
19+
const actual = getPropertyFromTarget(
20+
{ firstLevel: { secondLevel: { thirdLevel: { value: 'the value' } } } } as unknown as Component,
21+
['firstLevel', 'secondLevel', 'thirdLevel', 'value'],
22+
);
2323
expect(actual).toBe('the value');
2424
});
2525
});
@@ -70,6 +70,18 @@ describe('filterUtils', () => {
7070
).toBe(false);
7171
});
7272

73+
it('handles nested properties with three levels', () => {
74+
expect(
75+
targetMatchesFilters(originalOtherDocumentationAttachmentComponent, [
76+
{
77+
key: 'attachmentValues.leggerVedNaa.enabled',
78+
value: null,
79+
operator: 'exists',
80+
},
81+
]),
82+
).toBe(true);
83+
});
84+
7385
describe('With operators', () => {
7486
const typeEqTextfield = { key: 'type', value: 'textfield' };
7587
const typeEqRadio = { key: 'type', value: 'radio' };

packages/bygger-backend/src/migration/filterUtils.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ function parseFiltersFromParam(filtersFromParam: object): Filter[] {
2020
});
2121
}
2222

23-
function getPropertyFromTarget(comp: any, properties: string[]): string | undefined {
24-
if (properties.length > 1) {
25-
return getPropertyFromTarget(comp[properties[0]], properties.slice(1));
23+
function getPropertyFromTarget(component: Component | NavFormType, properties: string[]): string | undefined {
24+
if (properties.length > 1 && component[properties[0]]) {
25+
return getPropertyFromTarget(component[properties[0]], properties.slice(1));
2626
}
27-
return comp && comp[properties[0]];
27+
return component && component[properties[0]];
2828
}
2929

3030
function targetMatchesFilters(target: Component | NavFormType, filters: Filter[]) {

packages/bygger-backend/src/migration/testData.ts

+25
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,30 @@ const originalFodselsnummerComponent = {
6161
tableView: true,
6262
};
6363

64+
const originalOtherDocumentationAttachmentComponent: Component = {
65+
key: 'annenDokumentasjon',
66+
type: 'attachment',
67+
input: true,
68+
label: 'Annen dokumentasjon',
69+
validate: {
70+
required: true,
71+
},
72+
properties: {
73+
vedleggskode: 'N6',
74+
vedleggstittel: 'Annet',
75+
},
76+
description: 'Har du noen annen dokumentasjon du ønsker å legge ved?',
77+
attachmentType: 'other',
78+
attachmentValues: {
79+
nei: {
80+
enabled: true,
81+
},
82+
leggerVedNaa: {
83+
enabled: true,
84+
},
85+
},
86+
};
87+
6488
const originalForm: NavFormType = {
6589
path: 'testform',
6690
components: [originalFodselsnummerComponent, originalTextFieldComponent],
@@ -189,6 +213,7 @@ export {
189213
formWithSimpleConditionalToRadio,
190214
originalFodselsnummerComponent,
191215
originalForm,
216+
originalOtherDocumentationAttachmentComponent,
192217
originalPanelComponent,
193218
originalSkjemaGruppeComponent,
194219
originalTextFieldComponent,

0 commit comments

Comments
 (0)