Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syncing expression tests with app-lib-dotnet #741

Merged
merged 4 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@
"version": "detect"
}
},
"ignorePatterns": ["node_modules", "coverage", "dist", "**/*.snap"],
"ignorePatterns": [
"node_modules",
"coverage",
"dist",
"**/*.snap",
"src/features/expressions/shared-tests/**/*.json"
],
"rules": {
"no-console": [
"warn",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@
".husky/pre-commit-check-for-skipped-tests",
"eslint --cache --fix"
],
"*.{css,md,json}": "prettier --write"
"*.{json}": "eslint --cache --fix",
"*.{css,md}": "prettier --write"
},
"jest": {
"transform": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
{
"name": "Lookup for hidden component inside repeating group",
"expression": ["component", "single-top-component"],
"expects": "singleTopValue",
"layouts": {
"Page1": {
"$schema": "https://altinncdn.no/schemas/json/layout/layout.schema.v1.json",
"data": {
"layout": [
{
"id": "single-top-component",
"type": "Input",
"dataModelBindings": {
"simpleBinding": "Single.Top"
}
},
{
"id": "bedrifter",
"type": "Group",
"maxCount": 99,
"dataModelBindings": {
"group": "Bedrifter"
},
"children": ["bedriftsNavn", "ansatte"]
},
{
"id": "bedriftsNavn",
"type": "Input",
"dataModelBindings": {
"simpleBinding": "Bedrifter.Navn"
}
},
{
"id": "ansatte",
"type": "Group",
"maxCount": 99,
"dataModelBindings": {
"group": "Bedrifter.Ansatte"
},
"children": ["navn", "alder", "myndig"]
},
{
"id": "navn",
"type": "Input",
"dataModelBindings": {
"simpleBinding": "Bedrifter.Ansatte.Navn"
}
},
{
"id": "alder",
"type": "Input",
"dataModelBindings": {
"simpleBinding": "Bedrifter.Ansatte.Alder"
},
"hidden": [
"if",
["dataModel", "Bedrifter.Ansatte.AlderSkjult"],
true
]
},
{
"id": "myndig",
"type": "Paragraph",
"textResourceBindings": {
"title": "Hurra, den ansatte er myndig!"
}
}
]
}
}
},
"dataModel": {
"Single": {
"Top": "singleTopValue"
},
"Bedrifter": [
{
"Navn": "Hell og lykke AS",
"Ansatte": [
{
"Navn": "Kaare",
"Alder": 24
},
{
"Navn": "Per",
"Alder": 24
}
]
},
{
"Navn": "Nedtur og motgang AS",
"Ansatte": [
{
"Navn": "Arne",
"Alder": 24,
"AlderSkjult": true
},
{
"Navn": "Vidar",
"Alder": 14
}
]
}
]
},
"context": {
"component": "ansatte",
"currentLayout": "Page1",
"rowIndices": [1,0]
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"name": "Simple lookup equals other lookup",
"expression": ["equals", ["component", "my-component"], ["component", "my-other-component"]],
"expression": [
"equals",
["component", "my-component"],
["component", "my-other-component"]
],
"expects": true,
"layouts": {
"Page1": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
"name": "Inside a lookup argument",
"expression": [
"dataModel",
["concat", "FirstPart", ".", ["dataModel", "Middle"], ".Last", ["frontendSettings", "suffix"]]
[
"concat",
"FirstPart",
".",
["dataModel", "Middle"],
".Last",
["frontendSettings", "suffix"]
]
],
"expects": "foo bar",
"dataModel": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"name": "Simple lookup equals other lookup",
"expression": ["equals", ["frontendSettings", "mySetting1"], ["frontendSettings", "mySetting2"]],
"expression": [
"equals",
["frontendSettings", "mySetting1"],
["frontendSettings", "mySetting2"]
],
"expects": true,
"frontendSettings": {
"mySetting1": "foo",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"name": "Simple lookup equals other lookup",
"expression": ["equals", ["instanceContext", "instanceOwnerPartyId"], ["instanceContext", "appId"]],
"expression": [
"equals",
["instanceContext", "instanceOwnerPartyId"],
["instanceContext", "appId"]
],
"expects": false,
"instanceContext": {
"instanceId": "d00ce51c-800b-416a-a906-ccab55f597e9",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@
"dataModelBindings": {
"simpleBinding": "Bedrifter.Ansatte.Navn"
},
"hidden": ["and", ["greaterThan", ["component", "alder"], 99], ["notEquals", ["component", "alder"], 101]],
"hidden": [
"and",
["greaterThan", ["component", "alder"], 99],
["notEquals", ["component", "alder"], 101]
],
"required": ["dataModel", "Bedrifter.isRequired"]
},
{
Expand Down Expand Up @@ -132,7 +136,11 @@
"dataModelBindings": {
"simpleBinding": "Bedrifter.Ansatte.Navn"
},
"hidden": ["and", ["greaterThan", ["component", "alder"], 99], ["notEquals", ["component", "alder"], 101]],
"hidden": [
"and",
["greaterThan", ["component", "alder"], 99],
["notEquals", ["component", "alder"], 101]
],
"required": ["dataModel", "Bedrifter.isRequired"]
},
{
Expand Down
29 changes: 24 additions & 5 deletions src/features/expressions/shared.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,30 @@ import type { FunctionTest, SharedTestContext, SharedTestContextList } from 'src
import type { Expression } from 'src/features/expressions/types';
import type { IRepeatingGroups } from 'src/types';
import type { IApplicationSettings, IInstanceContext } from 'src/types/shared';
import type { LayoutNode } from 'src/utils/layout/hierarchy';
import type { LayoutNode, LayoutRootNodeCollection } from 'src/utils/layout/hierarchy';

function findComponent(context: FunctionTest['context'], collection: LayoutRootNodeCollection<any>) {
const { component, rowIndices } = context || { component: 'no-component' };
const componentId = (component || 'no-component') + (rowIndices ? `-${rowIndices.join('-')}` : '');
const found = collection.findById(componentId);
if (found) {
return found;
}

if (component && rowIndices && rowIndices.length) {
const componentId2 = `${component}-${rowIndices.slice(0, rowIndices.length - 1).join('-')}`.replace(/-+$/, '');
const foundMaybeGroup = collection.findById(componentId2);
if (foundMaybeGroup && foundMaybeGroup.item.type === 'Group') {
// Special case for using a group component with a row index, looking up within the
// group context, but actually pointing to a row inside the group. This is supported
// in useExpressions() itself, but evalExpr() requires the context of an actual component
// inside the group.
const rowIndex = [...rowIndices].pop();
return foundMaybeGroup.children(undefined, rowIndex)[0];
}
}

function toComponentId({ component, rowIndices }: Exclude<FunctionTest['context'], undefined>) {
return (component || 'no-component') + (rowIndices ? `-${rowIndices.join('-')}` : '');
return new NodeNotFoundWithoutContext(componentId);
}

describe('Expressions shared function tests', () => {
Expand Down Expand Up @@ -44,8 +64,7 @@ describe('Expressions shared function tests', () => {
const rootCollection = expectsFailure
? nodesInLayouts(_layouts, currentLayout, repeatingGroups)
: resolvedNodesInLayouts(_layouts, currentLayout, repeatingGroups, dataSources);
const componentId = context ? toComponentId(context) : 'no-component';
const component = rootCollection.findById(componentId) || new NodeNotFoundWithoutContext(componentId);
const component = findComponent(context, rootCollection);

if (expectsFailure) {
expect(() => {
Expand Down