Skip to content

Commit

Permalink
add backwards compatibility for selected property
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthiee committed Feb 28, 2024
1 parent c90a56d commit 6e2a47b
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/sdk/src/controllers/SubscriberController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export class SubscriberController {
items: ((variable as ListVariable).items as unknown as ListVariableItem[]).map(
(item) => item.value,
),
selected: ((variable as ListVariable).selected as unknown as ListVariableItem | undefined)?.value,
}
: variable,
);
Expand Down
8 changes: 3 additions & 5 deletions packages/sdk/src/controllers/VariableController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,12 @@ export class VariableController {
const updated = listVariable;

const items = listVariable.items as unknown as ListVariableItem[];
const selected = listVariable.selected as unknown as ListVariableItem | undefined;

const newItems: string[] = [];

items.forEach((item) => {
newItems.push(item.value);
});
const newItems = items.map((item) => item.value);

updated.items = newItems;
updated.selected = selected?.value;

return updated;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ describe('SubscriberController', () => {
});
it('Should be possible to subscribe to onVariableListChanged', async () => {
await mockedSubscriberController.onVariableListChanged(
'[{"id":"1","type":"group"},{"id":"varList","type":"list","selected":"Orange","items":[{"value":"Apple"},{"value":"Pear"},{"value":"Orange"}]}]',
'[{"id":"1","type":"group"},{"id":"varList","type":"list","selected": {"value": "Orange"},"items":[{"value":"Apple"},{"value":"Pear"},{"value":"Orange"}]}]',
);
expect(mockEditorApi.onVariableListChanged).toHaveBeenCalled();
expect(mockEditorApi.onVariableListChanged).toHaveBeenCalledWith([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('VariableController', () => {
},
};

const listVar: Variable & { items: ListVariableItem[] } = {
const listVar: Variable & { items: ListVariableItem[]; selected?: ListVariableItem } = {
id: variableId,
type: VariableType.list,
name: '',
Expand All @@ -41,6 +41,7 @@ describe('VariableController', () => {
isReadonly: false,
isRequired: false,
occurrences: 0,
selected: { value: 'abc', displayValue: 'A-B-C' },
items: [{ value: 'abc', displayValue: 'A-B-C' }],
};

Expand Down Expand Up @@ -107,13 +108,15 @@ describe('VariableController', () => {
expect(mockEditorApi.getVariableByName).toHaveBeenCalledWith('name');
// Backwards compatibility layer maps the new `VariableListItem` into a string.
expect((result.parsedData as ListVariable).items).toStrictEqual(['abc']);
expect((result.parsedData as ListVariable).selected).toStrictEqual('abc');
});

it('get variable list', async () => {
const result = await mockedVariableController.getAll();
expect(mockEditorApi.getVariables).toHaveBeenCalledTimes(1);
// Backwards compatibility layer maps the new `VariableListItem` into a string.
expect((result.parsedData as ListVariable[])[0].items).toStrictEqual(['abc']);
expect((result.parsedData as ListVariable[])[0].selected).toStrictEqual('abc');
});

it('create a new variable', async () => {
Expand Down

0 comments on commit 6e2a47b

Please sign in to comment.