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

feat: Infer and coerce code list value types in StudioCodeListEditor #14289

Open
wants to merge 23 commits into
base: main
Choose a base branch
from

Conversation

ErlingHauan
Copy link
Contributor

@ErlingHauan ErlingHauan commented Dec 16, 2024

Description

Previously, when a code list's values were of type number, they would be changed to strings when edited in StudioCodeListEditor.

This PR adds functionality that checks the code list's values. If at least one value is number or boolean, it will save the other values in the same type.

In addition, test data for StudioCodeListEditor has been moved into a separate file.

Not in scope

Selecting type when creating a new code list.

Related Issue(s)

Verification

  • Your code builds clean without any errors or warnings
  • Manual testing done (required)
  • Relevant automated test added (if you find this hard, leave it and we'll help out)

@github-actions github-actions bot added area/ui-editor Area: Related to the designer tool for assembling app UI in Altinn Studio. backend labels Dec 16, 2024
@@ -33,7 +33,7 @@ public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOp
writer.WriteBooleanValue(b);
break;
default:
throw new InvalidOptionsFormatException($"{value} is an unsupported type for Value field. Accepted types are string, double and bool.");
throw new InvalidOptionsFormatException($"{value} with type {value.GetType()} is unsupported for Value field. Accepted types are string, double and bool.");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I discovered a backend bug when enabling editing of numbers - see issue #14290.
The added information in this exception makes it easier to see what is wrong.

Copy link

codecov bot commented Dec 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.57%. Comparing base (bf7e3c3) to head (d3ed8e3).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #14289   +/-   ##
=======================================
  Coverage   95.56%   95.57%           
=======================================
  Files        1837     1838    +1     
  Lines       23856    23891   +35     
  Branches     2754     2757    +3     
=======================================
+ Hits        22798    22833   +35     
  Misses        801      801           
  Partials      257      257           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ErlingHauan ErlingHauan marked this pull request as ready for review December 16, 2024 12:56
@TomasEng TomasEng assigned TomasEng and ErlingHauan and unassigned TomasEng Dec 17, 2024
@ErlingHauan ErlingHauan removed their assignment Dec 19, 2024
@TomasEng TomasEng self-assigned this Dec 19, 2024
Copy link
Contributor

@TomasEng TomasEng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dette ser absolutt gjennomtenkt ut, men jeg ville nok heller utledet typen ved å sjekke mot selve codeList-propen. Da unngår vi feil som for eksempel oppstår når man bare har ett element, som jeg nevnte i en kommentar her.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jeg er usikker på om vi burde slette denne. I nyeste main er det også en kodeliste som heter codeListWithTextResources. Kanksje vi kan kalle de nye testlistene for stringCodeListWithoutTextResources osv.?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Godt forslag! Tar det inn.

Comment on lines +31 to +46
function coerceValues(codeList: CodeList, type: CodeListValueType): void {
codeList.forEach((codeListItem) => {
switch (type) {
case 'number':
codeListItem.value = tryCoerceNumber(codeListItem.value);
break;
case 'boolean':
codeListItem.value = coerceBoolean(codeListItem.value);
break;
case 'string':
case 'undefined':
default:
codeListItem.value = String(codeListItem.value);
}
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funksjoner som brukes av React-komponenter bør være rene. Det vil si at de ikke manipulerer inndataobjektet, men returnerer et nytt objekt med modifiserte verdier. Dette gjør koden mer oversiktlig og feilsøking enklere. Når funksjonen returnerer void er det vanskeligere å se hva den gjør. Du kan lese mer om dette her: https://react.dev/learn/keeping-components-pure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Takk for innspill!
Jeg var usikker på om jeg skulle returnere void eller modifisere en kopi, men her er det tydeligvis en klar vinner.

Comment on lines +246 to +260
it('Should save all values as numbers, when a code list that contains numbers is modified', async () => {
const user = userEvent.setup();
renderCodeListEditor({ codeList: codeListWithNumbers });

const valueInput = screen.getByRole('textbox', { name: texts.itemValue(1) });
const newValue = '3.1416';
await user.type(valueInput, newValue);

expect(onChange).toHaveBeenCalledTimes(newValue.length);
expect(onChange).toHaveBeenLastCalledWith([
{ ...codeListWithNumbers[0], value: Number(newValue) },
codeListWithNumbers[1],
codeListWithNumbers[2],
]);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jeg prøvde å modifisere denne testen til å teste en kodeliste med kun ett element. Da feiler den fordi verdien blir konvertert til en tekststreng.

Hvis jeg endrer verdien til '3,1416' (med komma), feiler testen også. Vi bør nok ta høyde for komma siden det er slik vi skriver desimaltall på norsk.


expect(onChange).toHaveBeenCalledTimes(newValue.length);
expect(onChange).toHaveBeenLastCalledWith([
{ ...codeListWithNumbers[0], value: Number(newValue) },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Når vi tester konvertering av typer, er det en fordel å skrive verdien rett ut. Her brukes Number() både i testen og i funksjonen som testes, så hvis noe er feil i forbindelse med nettopp Number-funksjonen, vil ikke testen oppfatte feilen.

Suggested change
{ ...codeListWithNumbers[0], value: Number(newValue) },
{ ...codeListWithNumbers[0], value: 3.1416 },

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Samle gjerne alt i en felles describe-blokk for hele filen.

@ErlingHauan ErlingHauan assigned ErlingHauan and unassigned TomasEng Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/ui-editor Area: Related to the designer tool for assembling app UI in Altinn Studio. backend frontend solution/studio/designer Issues related to the Altinn Studio Designer solution. team/studio-domain1
Projects
Status: 👷 In Progress
Development

Successfully merging this pull request may close these issues.

2 participants