-
Notifications
You must be signed in to change notification settings - Fork 72
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
base: main
Are you sure you want to change the base?
Conversation
>> >> Two issues remain: >> 1. We get a backend error when changing one of the values in industri.json, even though the request looks fine >> 2. convertValue function needs more work if the value is a boolean
…eat/preserve-value-type-in-codelists
…n-codelists-pr-split
@@ -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."); |
There was a problem hiding this comment.
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.
Codecov ReportAll modified and coverable lines are covered by tests ✅
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. |
There was a problem hiding this 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.
There was a problem hiding this comment.
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.?
There was a problem hiding this comment.
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.
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); | ||
} | ||
}); | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
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], | ||
]); | ||
}); |
There was a problem hiding this comment.
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) }, |
There was a problem hiding this comment.
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.
{ ...codeListWithNumbers[0], value: Number(newValue) }, | |
{ ...codeListWithNumbers[0], value: 3.1416 }, |
There was a problem hiding this comment.
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.
Description
Previously, when a code list's values were of type
number
, they would be changed to strings when edited inStudioCodeListEditor
.This PR adds functionality that checks the code list's values. If at least one value is
number
orboolean
, 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