Skip to content

Commit

Permalink
fix: Account for invalid text resource ids in code list editor (#14797)
Browse files Browse the repository at this point in the history
Co-authored-by: William Thorenfeldt <[email protected]>
  • Loading branch information
TomasEng and wrt95 authored Feb 27, 2025
1 parent d5e44f1 commit 21f546a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ describe('StudioTextResourcePicker', () => {
expect(getCombobox()).toHaveValue('');
});

it('Renders with no text resource option as selected when the given id does not exist', () => {
const nonExistentId = 'non-existent-id';
renderTextResourcePicker({ value: nonExistentId });
expect(getCombobox()).toHaveValue('');
});

it('Calls the onValueChange callback with null when the user selects the unset option', async () => {
const user = userEvent.setup();
const value = textResources[arbitraryTextResourceIndex].id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { ReactElement } from 'react';
import React, { forwardRef, useCallback } from 'react';
import React, { useMemo, forwardRef, useCallback } from 'react';
import type { TextResource } from '../../types/TextResource';
import type { StudioComboboxProps } from '../StudioCombobox';
import { StudioCombobox } from '../StudioCombobox';
import type { Override } from '../../types/Override';
import classes from './StudioTextResourcePicker.module.css';
import { retrieveSelectedValues } from './utils';

export type StudioTextResourcePickerProps = Override<
{
Expand Down Expand Up @@ -37,11 +38,16 @@ export const StudioTextResourcePicker = forwardRef<HTMLInputElement, StudioTextR
[onValueChange],
);

const selectedValues: string[] = useMemo(
() => retrieveSelectedValues(textResources, value),
[textResources, value],
);

return (
<StudioCombobox
hideLabel
onValueChange={handleValueChange}
value={value ? [value] : []}
value={selectedValues}
{...rest}
ref={ref}
>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { textResourcesMock } from '../../test-data/textResourcesMock';
import { retrieveSelectedValues } from './utils';

describe('utils', () => {
describe('retrieveSelectedValues', () => {
it('Returns an array with the value when the text resource exists', () => {
const textResources = textResourcesMock;
const arbitraryTextResourceIndex = 129;
const textResource = textResources[arbitraryTextResourceIndex];
expect(retrieveSelectedValues(textResources, textResource.id)).toEqual([textResource.id]);
});

it('Returns an empty array when the text resource does not exist', () => {
const textResources = textResourcesMock;
const idThatDoesNotExist = 'does-not-exist';
expect(retrieveSelectedValues(textResources, idThatDoesNotExist)).toEqual([]);
});

it.each([null, undefined])('Returns an empty array when the value is %s', (value) => {
expect(retrieveSelectedValues(textResourcesMock, value)).toEqual([]);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { TextResource } from '../../types/TextResource';

export function retrieveSelectedValues(textResources: TextResource[], id?: string): string[] {
return doesTextResourceExist(textResources, id) ? [id] : [];
}

function doesTextResourceExist(textResources: TextResource[], id?: string): boolean {
return textResources.some((textResource) => textResource.id === id);
}

0 comments on commit 21f546a

Please sign in to comment.