Skip to content

Commit e948aad

Browse files
committed
test(combobox-web): build mocks with the test-utils builders instead of casts
The characterisation specs set status, items, limit, sort order, validation and attribute ids by casting mocks to ad-hoc shapes. They now use the builders, the typed attrId helper and the generic actionValue. No expectation changed.
1 parent cf124a9 commit e948aad

7 files changed

Lines changed: 63 additions & 65 deletions

File tree

‎packages/pluggableWidgets/combobox-web/src/helpers/Association/__tests__/AssociationMultiSelector.spec.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { ListValue } from "mendix";
2-
import { list, ReferenceSetValueBuilder } from "@mendix/widget-plugin-test-utils";
3-
import { allItems, associationMultiProps, itemA, itemB, itemC } from "../../__tests__/fixtures";
2+
import { list, ListValueBuilder, ReferenceSetValueBuilder } from "@mendix/widget-plugin-test-utils";
3+
import { allItems, associationMultiProps, captionAttributeId, itemA, itemB, itemC } from "../../__tests__/fixtures";
44
import { AssociationMultiSelector } from "../AssociationMultiSelector";
55

66
function createSelector(): AssociationMultiSelector {
77
return new AssociationMultiSelector({ filterInputDebounceInterval: 200 });
88
}
99

1010
function sortedList(direction: "asc" | "desc"): ListValue {
11-
const ds = list(allItems);
12-
(ds as { sortOrder: unknown }).sortOrder = [["captionAttr", direction]];
13-
return ds;
11+
return new ListValueBuilder()
12+
.withItems(allItems)
13+
.withSortOrder([[captionAttributeId, direction]])
14+
.build();
1415
}
1516

1617
describe("AssociationMultiSelector", () => {

‎packages/pluggableWidgets/combobox-web/src/helpers/Association/__tests__/AssociationSingleSelector.spec.tsx‎

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import { render } from "@testing-library/react";
22
import { ListValue } from "mendix";
3-
import { dynamic, list, listWidget, ReferenceValueBuilder } from "@mendix/widget-plugin-test-utils";
4-
import { allItems, associationSingleProps, captions, itemA, itemB, itemC, itemD } from "../../__tests__/fixtures";
3+
import { dynamic, list, ListValueBuilder, listWidget, ReferenceValueBuilder } from "@mendix/widget-plugin-test-utils";
4+
import {
5+
allItems,
6+
associationSingleProps,
7+
captionAttributeId,
8+
captions,
9+
itemA,
10+
itemB,
11+
itemC,
12+
itemD
13+
} from "../../__tests__/fixtures";
514
import { AssociationSingleSelector } from "../AssociationSingleSelector";
615

716
function createSelector(): AssociationSingleSelector {
817
return new AssociationSingleSelector({ filterInputDebounceInterval: 200 });
918
}
1019

1120
function unavailableList(): ListValue {
12-
const ds = list(allItems);
13-
(ds as { status: string }).status = "unavailable";
14-
(ds as { items?: unknown }).items = undefined;
15-
return ds;
21+
return new ListValueBuilder().withItems(allItems).isUnavailable().build();
1622
}
1723

1824
describe("AssociationSingleSelector", () => {
@@ -236,8 +242,10 @@ describe("AssociationSingleSelector", () => {
236242
});
237243

238244
it("reports the datasource sort order", () => {
239-
const ds = list(allItems);
240-
(ds as { sortOrder: unknown }).sortOrder = [["captionAttr", "desc"]];
245+
const ds = new ListValueBuilder()
246+
.withItems(allItems)
247+
.withSortOrder([[captionAttributeId, "desc"]])
248+
.build();
241249
const selector = createSelector();
242250
selector.updateProps(associationSingleProps({ ds }));
243251

@@ -286,11 +294,9 @@ describe("AssociationSingleSelector", () => {
286294
});
287295

288296
describe("lazy loading", () => {
289-
function lazyList(limit: number, hasMore: boolean): ListValue {
290-
const ds = list(allItems);
291-
(ds as { limit: number }).limit = limit;
292-
(ds as { hasMoreItems: boolean }).hasMoreItems = hasMore;
293-
return ds;
297+
function lazyList(limit: number, hasMore: boolean, loading = false): ListValue {
298+
const builder = new ListValueBuilder().withItems(allItems).withLimit(limit).withHasMore(hasMore);
299+
return (loading ? builder.isLoading() : builder).build();
294300
}
295301

296302
it("loads the next page of 100 and reports loading until the datasource settles", () => {
@@ -319,8 +325,7 @@ describe("AssociationSingleSelector", () => {
319325
});
320326

321327
it("reports loading while a lazy datasource is loading", () => {
322-
const ds = lazyList(100, true);
323-
(ds as { status: string }).status = "loading";
328+
const ds = lazyList(100, true, true);
324329
const selector = createSelector();
325330
selector.updateProps(associationSingleProps({ ds, lazyLoading: true }));
326331

‎packages/pluggableWidgets/combobox-web/src/helpers/Database/__tests__/DatabaseMultiSelectionSelector.spec.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { ListValue } from "mendix";
2-
import { dynamic, list, SelectionMultiValueBuilder } from "@mendix/widget-plugin-test-utils";
3-
import { allItems, databaseMultiProps, itemA, itemB, itemC, itemD } from "../../__tests__/fixtures";
2+
import { dynamic, list, ListValueBuilder, SelectionMultiValueBuilder } from "@mendix/widget-plugin-test-utils";
3+
import { allItems, captionAttributeId, databaseMultiProps, itemA, itemB, itemC, itemD } from "../../__tests__/fixtures";
44
import { DatabaseMultiSelectionSelector } from "../DatabaseMultiSelectionSelector";
55

66
function createSelector(): DatabaseMultiSelectionSelector {
77
return new DatabaseMultiSelectionSelector({ filterInputDebounceInterval: 200 });
88
}
99

1010
function sortedList(direction: "asc" | "desc"): ListValue {
11-
const ds = list(allItems);
12-
(ds as { sortOrder: unknown }).sortOrder = [["captionAttr", direction]];
13-
return ds;
11+
return new ListValueBuilder()
12+
.withItems(allItems)
13+
.withSortOrder([[captionAttributeId, direction]])
14+
.build();
1415
}
1516

1617
describe("DatabaseMultiSelectionSelector", () => {
@@ -155,8 +156,7 @@ describe("DatabaseMultiSelectionSelector", () => {
155156
});
156157

157158
it("is unavailable while the datasource or the empty-option text is unavailable", () => {
158-
const ds = list(allItems);
159-
(ds as { status: string }).status = "unavailable";
159+
const ds = new ListValueBuilder().withItems(allItems).isUnavailable().build();
160160
const noDatasource = createSelector();
161161
noDatasource.updateProps(databaseMultiProps({ ds }));
162162
expect(noDatasource.status).toBe("unavailable");

‎packages/pluggableWidgets/combobox-web/src/helpers/Database/__tests__/DatabaseSingleSelectionSelector.spec.ts‎

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { ListAttributeValue, ObjectItem } from "mendix";
1+
import { ListAttributeValue, ListValue, ObjectItem } from "mendix";
22
import {
3+
attrId,
34
dynamic,
45
EditableValueBuilder,
56
list,
@@ -14,27 +15,25 @@ type PropsOverrides = {
1415
items: ObjectItem[];
1516
values: Map<string, string>;
1617
targetValue?: string;
18+
validation?: string;
1719
selection?: ReturnType<SelectionSingleValueBuilder["build"]>;
20+
ds?: ListValue;
1821
};
1922

20-
function buildProps({ items, values, targetValue, selection }: PropsOverrides): ComboboxContainerProps {
21-
const valueAttr = listAttribute<string>(item => values.get(item.id) ?? "") as ListAttributeValue<string | Big>;
22-
(valueAttr as unknown as { id: string }).id = "valueAttrId" as any;
23+
function buildProps({ items, values, targetValue, validation, selection, ds }: PropsOverrides): ComboboxContainerProps {
24+
const valueAttr = listAttribute<string>(item => values.get(item.id) ?? "", attrId("value")) as ListAttributeValue<
25+
string | Big
26+
>;
27+
const captionAttr = listAttribute<string>(item => `caption_${item.id}`, attrId("caption"));
2328

24-
const captionAttr = listAttribute<string>(item => `caption_${item.id}`);
25-
(captionAttr as unknown as { id: string }).id = "captionAttrId" as any;
26-
27-
const targetAttr =
28-
targetValue === undefined
29-
? new EditableValueBuilder<string>().build()
30-
: new EditableValueBuilder<string>().withValue(targetValue).build();
29+
const targetAttr = new EditableValueBuilder<string>().withValue(targetValue).withValidation(validation).build();
3130

3231
return {
3332
name: "comboBox",
3433
id: "comboBox1",
3534
source: "database",
3635
optionsSourceType: "association",
37-
optionsSourceDatabaseDataSource: list(items),
36+
optionsSourceDatabaseDataSource: ds ?? list(items),
3837
optionsSourceDatabaseItemSelection: selection ?? new SelectionSingleValueBuilder().build(),
3938
optionsSourceDatabaseCaptionType: "attribute",
4039
optionsSourceDatabaseCaptionAttribute: captionAttr,
@@ -178,8 +177,7 @@ describe("DatabaseSingleSelectionSelector — writing and read-only", () => {
178177
});
179178

180179
it("ignores updates while a non-lazy datasource is loading", () => {
181-
const props = buildProps({ items, values, targetValue: "v1" });
182-
(props.optionsSourceDatabaseDataSource as { status: string }).status = "loading";
180+
const props = buildProps({ items, values, targetValue: "v1", ds: list.loading() });
183181
const selector = new DatabaseSingleSelectionSelector({ filterInputDebounceInterval: 200 });
184182

185183
selector.updateProps(props);
@@ -199,8 +197,7 @@ describe("DatabaseSingleSelectionSelector — writing and read-only", () => {
199197
});
200198

201199
it("resolves captions and passes validation and clearable through", () => {
202-
const props = buildProps({ items, values, targetValue: "v1" });
203-
(props.databaseAttributeString as { validation?: string }).validation = "Required";
200+
const props = buildProps({ items, values, targetValue: "v1", validation: "Required" });
204201
const selector = new DatabaseSingleSelectionSelector({ filterInputDebounceInterval: 200 });
205202

206203
selector.updateProps(props);

‎packages/pluggableWidgets/combobox-web/src/helpers/__tests__/LazyLoadProvider.spec.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { ValueStatus } from "mendix";
2-
import { list } from "@mendix/widget-plugin-test-utils";
2+
import { ListValueBuilder } from "@mendix/widget-plugin-test-utils";
33
import { LazyLoadProvider } from "../LazyLoadProvider";
44

55
function providerWithLimit(limit: number): LazyLoadProvider {
66
const provider = new LazyLoadProvider();
7-
const ds = list(2);
8-
(ds as { limit: number }).limit = limit;
9-
provider.updateProps(ds);
7+
provider.updateProps(new ListValueBuilder().withSize(2).withLimit(limit).build());
108
return provider;
119
}
1210

@@ -55,8 +53,7 @@ describe("LazyLoadProvider.getLimit", () => {
5553
describe("LazyLoadProvider.setLimit", () => {
5654
it("writes the limit only when it differs from the datasource limit", () => {
5755
const provider = new LazyLoadProvider();
58-
const ds = list(2);
59-
(ds as { limit: number }).limit = 100;
56+
const ds = new ListValueBuilder().withSize(2).withLimit(100).build();
6057
provider.updateProps(ds);
6158

6259
provider.setLimit(100);

‎packages/pluggableWidgets/combobox-web/src/helpers/__tests__/fixtures.ts‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
SelectionSingleValue
1111
} from "mendix";
1212
import {
13+
attrId,
1314
dynamic,
1415
EditableValueBuilder,
1516
list,
@@ -104,10 +105,10 @@ export const itemC = item("c");
104105
export const itemD = item("d");
105106
export const allItems = [itemA, itemB, itemC, itemD];
106107

108+
export const captionAttributeId = attrId("captionAttr");
109+
107110
export function captionAttribute(): ListAttributeValue<string> {
108-
const attr = listAttribute<string>(i => captions[i.id] ?? "");
109-
(attr as { id: string }).id = "captionAttr";
110-
return attr;
111+
return listAttribute<string>(i => captions[i.id] ?? "", captionAttributeId);
111112
}
112113

113114
export function captionExpression(): ListExpressionValue<string> {
@@ -201,9 +202,7 @@ export function staticProps(
201202

202203
/** Value attribute: obj_a → "v_a", obj_b → "v_b", ... */
203204
export function valueAttribute(): ListAttributeValue<string | Big> {
204-
const attr = listAttribute<string>(i => `v_${i.id.slice(-1)}`) as ListAttributeValue<string | Big>;
205-
(attr as { id: string }).id = "valueAttr";
206-
return attr;
205+
return listAttribute<string>(i => `v_${i.id.slice(-1)}`, attrId("valueAttr")) as ListAttributeValue<string | Big>;
207206
}
208207

209208
interface DatabaseOverrides extends Partial<ComboboxContainerProps> {

‎packages/pluggableWidgets/combobox-web/src/hooks/__tests__/useGetSelector.spec.tsx‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import { act, renderHook } from "@testing-library/react";
2-
import { ActionValue } from "mendix";
32
import { actionValue } from "@mendix/widget-plugin-test-utils";
43
import { ComboboxContainerProps } from "../../../typings/ComboboxProps";
54
import { enumerationProps } from "../../helpers/__tests__/fixtures";
65
import { Selector } from "../../helpers/types";
76
import { useGetSelector } from "../useGetSelector";
87

9-
type FilterAction = ActionValue<{ filterInput: string | undefined }>;
8+
type FilterAction = NonNullable<ComboboxContainerProps["onChangeFilterInputEvent"]>;
109

11-
function propsWithFilterAction(action: ActionValue, interval = 200): ComboboxContainerProps {
10+
function propsWithFilterAction(action: FilterAction, interval = 200): ComboboxContainerProps {
1211
return enumerationProps(undefined, {
13-
onChangeFilterInputEvent: action as unknown as FilterAction,
12+
onChangeFilterInputEvent: action,
1413
filterInputDebounceInterval: interval
1514
});
1615
}
@@ -75,7 +74,7 @@ describe("useGetSelector", () => {
7574
beforeEach(() => jest.useFakeTimers());
7675
afterEach(() => jest.useRealTimers());
7776

78-
function renderAttached(action: ActionValue, interval?: number): Selector {
77+
function renderAttached(action: FilterAction, interval?: number): Selector {
7978
const { result, rerender } = renderHook((props: ComboboxContainerProps) => useGetSelector(props), {
8079
initialProps: propsWithFilterAction(action, interval)
8180
});
@@ -84,7 +83,7 @@ describe("useGetSelector", () => {
8483
}
8584

8685
it("debounces the action and sends the last term", () => {
87-
const action = actionValue();
86+
const action = actionValue<FilterAction>();
8887
const selector = renderAttached(action);
8988

9089
selector.onFilterInputChange!("a");
@@ -98,7 +97,7 @@ describe("useGetSelector", () => {
9897
});
9998

10099
it("respects the configured interval", () => {
101-
const action = actionValue();
100+
const action = actionValue<FilterAction>();
102101
const selector = renderAttached(action, 500);
103102

104103
selector.onFilterInputChange!("a");
@@ -110,7 +109,7 @@ describe("useGetSelector", () => {
110109
});
111110

112111
it("sends undefined for an empty term", () => {
113-
const action = actionValue();
112+
const action = actionValue<FilterAction>();
114113
const selector = renderAttached(action);
115114

116115
selector.onFilterInputChange!("");
@@ -120,12 +119,12 @@ describe("useGetSelector", () => {
120119
});
121120

122121
it("skips the action when it cannot execute or is already executing", () => {
123-
const cannot = actionValue(false);
122+
const cannot = actionValue<FilterAction>(false);
124123
renderAttached(cannot).onFilterInputChange!("a");
125124
jest.advanceTimersByTime(200);
126125
expect(cannot.execute).not.toHaveBeenCalled();
127126

128-
const executing = actionValue(true, true);
127+
const executing = actionValue<FilterAction>(true, true);
129128
renderAttached(executing).onFilterInputChange!("a");
130129
jest.advanceTimersByTime(200);
131130
expect(executing.execute).not.toHaveBeenCalled();

0 commit comments

Comments
 (0)