Skip to content

Commit d8a7e08

Browse files
authored
Update shared tests and require maxCount for a Group component to be repeating (#150)
Co-authored-by: Ivar <ivar.nesje@finanstilsynet.no>
1 parent 3a2447e commit d8a7e08

7 files changed

Lines changed: 123 additions & 49 deletions

File tree

src/Altinn.App.Core/Internal/Expressions/LayoutEvaluatorState.cs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,31 +49,28 @@ public IEnumerable<ComponentContext> GetComponentContexts()
4949

5050
private static ComponentContext GetComponentContextsRecurs(BaseComponent component, IDataModelAccessor dataModel, ReadOnlySpan<int> indexes)
5151
{
52-
if (!(component is GroupComponent groupComponent && groupComponent.Children.Any()))
53-
{
54-
// Non group components are simple
55-
return new ComponentContext(component, ToArrayOrNullForEmpty(indexes));
56-
}
57-
5852
var children = new List<ComponentContext>();
5953

60-
if (groupComponent.DataModelBindings.TryGetValue("group", out var groupBinding))
54+
if(component is RepeatingGroupComponent repeatingGroupComponent)
6155
{
62-
var rowLength = dataModel.GetModelDataCount(groupBinding, indexes.ToArray()) ?? 0;
63-
foreach (var index in Enumerable.Range(0, rowLength))
56+
if (repeatingGroupComponent.DataModelBindings.TryGetValue("group", out var groupBinding))
6457
{
65-
foreach (var child in groupComponent.Children)
58+
var rowLength = dataModel.GetModelDataCount(groupBinding, indexes.ToArray()) ?? 0;
59+
foreach (var index in Enumerable.Range(0, rowLength))
6660
{
67-
// concatenate [...indexes, index]
68-
var subIndexes = new int[indexes.Length + 1];
69-
indexes.CopyTo(subIndexes.AsSpan());
70-
subIndexes[^1] = index;
71-
72-
children.Add(GetComponentContextsRecurs(child, dataModel, subIndexes));
61+
foreach (var child in repeatingGroupComponent.Children)
62+
{
63+
// concatenate [...indexes, index]
64+
var subIndexes = new int[indexes.Length + 1];
65+
indexes.CopyTo(subIndexes.AsSpan());
66+
subIndexes[^1] = index;
67+
68+
children.Add(GetComponentContextsRecurs(child, dataModel, subIndexes));
69+
}
7370
}
7471
}
7572
}
76-
else
73+
else if (component is GroupComponent groupComponent )
7774
{
7875
foreach (var child in groupComponent.Children)
7976
{

src/Altinn.App.Core/Models/Layout/LayoutModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public class LayoutModel
1515
/// </summary>
1616
public Dictionary<string, PageComponent> Pages { get; init; } = new Dictionary<string, PageComponent>();
1717

18+
/// <summary>
19+
/// Get a page from the <see cref="Pages" /> dictionary
20+
/// </summary>
1821
public PageComponent GetPage(string pageName)
1922
{
2023
if (Pages.TryGetValue(pageName, out var page))

test/Altinn.App.Core.Tests/Helpers/JsonHelperTests.cs

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,6 @@ public async Task SimpleNoChangeTest()
7272
diff.Should().BeNull();
7373
}
7474

75-
[Fact(Skip = "Curently empty diffs are sendt to frontend, so the result here is an empty dictionary")]
76-
public async Task SimpleNoChangeTestTrueReturn()
77-
{
78-
var data = new TestModel();
79-
var diff = await DoTest(data, (model) => true);
80-
diff.Should().BeNull();
81-
}
82-
8375
[Fact]
8476
public async Task InitializingPropertiesLeadsToNoDiff()
8577
{
@@ -109,9 +101,9 @@ public async Task InitializingNonNullablePropertiesCreatesDiff()
109101
// Not sure if RecursiveTest should be null here, but apparently it does not hurt
110102
diff.Should().Equal(new Dictionary<string, object?>()
111103
{
112-
{"RecursiveTest.IntegerTest", 0},
113-
{"RecursiveTest.DecimalTest", 0M},
114-
{"RecursiveTest", null},
104+
{ "RecursiveTest.IntegerTest", 0 },
105+
{ "RecursiveTest.DecimalTest", 0M },
106+
{ "RecursiveTest", null },
115107
});
116108
}
117109

@@ -164,26 +156,7 @@ public async Task TestSystemTextJsonAnnotation()
164156

165157
diff.Should().Equal(new Dictionary<string, object?>
166158
{
167-
{"jsonPropertyName", "New Value"},
168-
});
169-
}
170-
171-
[Fact(Skip = "System.Text.Json annotation is required")]
172-
public async Task NewtonsoftJsonAnnotation()
173-
{
174-
var data = new TestModel()
175-
{
176-
NewtonsoftAttributeWorks = "Original Value",
177-
};
178-
var diff = await DoTest(data, (model) =>
179-
{
180-
model.NewtonsoftAttributeWorks = "New Value";
181-
return true;
182-
});
183-
184-
diff.Should().Equal(new Dictionary<string, object?>
185-
{
186-
{"newtonsoftString", "New Value"},
159+
{ "jsonPropertyName", "New Value" },
187160
});
188161
}
189162

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"name": "Lookup for component inside hidden non-repeating group",
3+
"expression": ["component", "dagligLederNavn"],
4+
"expects": null,
5+
"layouts": {
6+
"Page1": {
7+
"$schema": "https://altinncdn.no/schemas/json/layout/layout.schema.v1.json",
8+
"data": {
9+
"layout": [
10+
{
11+
"id": "bedrift",
12+
"type": "Group",
13+
"dataModelBindings": {
14+
"group": "Bedrift"
15+
},
16+
"children": ["bedriftsNavn", "dagligLederNavn"],
17+
"hidden": ["greaterThan", ["dataModel", "Bedrift.DagligLeder.Alder"], 18]
18+
},
19+
{
20+
"id": "bedriftsNavn",
21+
"type": "Input",
22+
"dataModelBindings": {
23+
"simpleBinding": "Bedrift.Navn"
24+
}
25+
},
26+
{
27+
"id": "dagligLederNavn",
28+
"type": "Input",
29+
"dataModelBindings": {
30+
"simpleBinding": "Bedrift.DagligLeder.Navn"
31+
}
32+
}
33+
]
34+
}
35+
}
36+
},
37+
"dataModel": {
38+
"Bedrift": {
39+
"Navn": "Hell og lykke AS",
40+
"DagligLeder": {
41+
"Navn": "Kaare",
42+
"Alder": 24
43+
}
44+
}
45+
},
46+
"context": {
47+
"component": "bedriftsNavn",
48+
"currentLayout": "Page1"
49+
}
50+
}

test/Altinn.App.Core.Tests/LayoutExpressions/CommonTests/shared-tests/functions/component/in-group-with-hidden.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,4 @@
6666
}
6767
]
6868
}
69-
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"name": "Lookup for component inside visible non-repeating group",
3+
"expression": ["component", "dagligLederNavn"],
4+
"expects": "Kaare",
5+
"layouts": {
6+
"Page1": {
7+
"$schema": "https://altinncdn.no/schemas/json/layout/layout.schema.v1.json",
8+
"data": {
9+
"layout": [
10+
{
11+
"id": "bedrift",
12+
"type": "Group",
13+
"dataModelBindings": {
14+
"group": "Bedrift"
15+
},
16+
"children": ["bedriftsNavn", "dagligLederNavn"],
17+
"hidden": ["greaterThan", ["dataModel", "Bedrift.DagligLeder.Alder"], 35]
18+
},
19+
{
20+
"id": "bedriftsNavn",
21+
"type": "Input",
22+
"dataModelBindings": {
23+
"simpleBinding": "Bedrift.Navn"
24+
}
25+
},
26+
{
27+
"id": "dagligLederNavn",
28+
"type": "Input",
29+
"dataModelBindings": {
30+
"simpleBinding": "Bedrift.DagligLeder.Navn"
31+
}
32+
}
33+
]
34+
}
35+
}
36+
},
37+
"dataModel": {
38+
"Bedrift": {
39+
"Navn": "Hell og lykke AS",
40+
"DagligLeder": {
41+
"Navn": "Kaare",
42+
"Alder": 24
43+
}
44+
}
45+
},
46+
"context": {
47+
"component": "bedriftsNavn",
48+
"currentLayout": "Page1"
49+
}
50+
}

test/Altinn.App.Core.Tests/LayoutExpressions/FullTests/Test2/FirstPage.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"dataModelBindings": {
2020
"group": "some.data"
2121
},
22+
"maxCount": 5,
2223
"hidden": ["notEquals", ["dataModel", "some.notRepeating"], "showGroup"]
2324
},
2425
{

0 commit comments

Comments
 (0)