@scheels-softdev/kendoreact-generics 2.6.1 → 2.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,19 +1,32 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { ComboBox } from "@progress/kendo-react-dropdowns";
3
- import { filterBy } from "@progress/kendo-data-query";
4
- import { useRef, useState } from "react";
3
+ import { filterBy, process } from "@progress/kendo-data-query";
4
+ import { useEffect, useRef, useState } from "react";
5
5
  import { Skeleton } from "@progress/kendo-react-indicators";
6
+ import { Guard } from "@scheels-softdev/frontend-utility-functions";
6
7
  export function GenericDropdown({ data, selectedId, selectedData, onChange, textField, disabled, idField, title, hideClearButton, style, isLoading, }) {
8
+ //local state
9
+ const pageSize = 8;
7
10
  if (selectedId !== undefined && selectedData !== undefined) {
8
11
  throw new Error("You cannot provide both selectedData and selectedId to GenericDropdown.");
9
12
  }
10
- //local state
11
- const pageSize = 8;
13
+ //useState
14
+ const [selectedObject, setSelectedObject] = useState(null);
12
15
  const [state, setState] = useState({
13
16
  skip: 0,
14
17
  total: data.length,
15
18
  subsetData: data.slice(0, pageSize),
16
19
  });
20
+ //trigger functions
21
+ useEffect(() => {
22
+ var _a, _b;
23
+ setSelectedObject(selectedId
24
+ ? (_a = data.find((item) => selectedId === item[idField || "id"])) !== null && _a !== void 0 ? _a : null
25
+ : (_b = data.find((x) => Guard.Ensure.EqualObjects(Object.assign(Object.assign({}, x), selectedData), Object.assign({}, x)))) !== null && _b !== void 0 ? _b : null);
26
+ }, [selectedId, selectedData, data]);
27
+ useEffect(() => {
28
+ setState((current) => (Object.assign(Object.assign({}, current), { subsetData: process(data, current).data })));
29
+ }, [data]);
17
30
  //external function variables
18
31
  let filteredData = useRef(data.slice());
19
32
  //function creation
@@ -39,14 +52,6 @@ export function GenericDropdown({ data, selectedId, selectedData, onChange, text
39
52
  const newSubsetData = filteredData.current.length ? filteredData.current.slice(skip, skip + take) : data.slice(skip, skip + take);
40
53
  setState(Object.assign(Object.assign({}, state), { subsetData: newSubsetData, skip: skip }));
41
54
  };
42
- const findByIndex = (id, idKey) => {
43
- const item = data.find((item) => id === item[idKey || "id"]);
44
- return item;
45
- };
46
- const findByValue = (item) => {
47
- const returnVal = data.find((x) => JSON.stringify(Object.assign(Object.assign({}, x), selectedData)) === JSON.stringify(Object.assign({}, x)));
48
- return returnVal;
49
- };
50
55
  return (_jsx("div", Object.assign({ "data-testid": "dropdown" }, { children: data.length && !isLoading ? (_jsx(ComboBox, { style: style, label: title, disabled: disabled, data: state.subsetData, textField: textField.toString(), virtual: {
51
56
  // enable virtualization for large datasets
52
57
  total: state.total,
@@ -54,5 +59,5 @@ export function GenericDropdown({ data, selectedId, selectedData, onChange, text
54
59
  skip: state.skip,
55
60
  }, suggest: true, onPageChange: pageChange, filterable: true, onFilterChange: onFilterChange, popupSettings: {
56
61
  height: "210px",
57
- }, onChange: (e) => onChange(e), onBlur: (e) => e.nativeEvent.preventDefault(), clearButton: !hideClearButton, value: !selectedId && !selectedData ? null : findByIndex(selectedId, idField) || findByValue(selectedData) || null })) : (_jsx(Skeleton, { shape: "rectangle", style: style ? style : { height: "40px", width: "100%" } })) })));
62
+ }, onChange: (e) => onChange(e), onBlur: (e) => e.nativeEvent.preventDefault(), clearButton: !hideClearButton, value: selectedObject })) : (_jsx(Skeleton, { shape: "rectangle", style: style ? style : { height: "40px", width: "100%" } })) })));
58
63
  }
@@ -0,0 +1 @@
1
+ export {};
package/_index.test.js ADDED
@@ -0,0 +1,101 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { GenericDropdown } from "./GenericDropdown";
3
+ import { fireEvent, render } from "@testing-library/react";
4
+ describe("GenericDropdown", () => {
5
+ const data = [];
6
+ for (let i = 1; i < 20; i++) {
7
+ data.push({ id: i, name: `Option ${i}` });
8
+ }
9
+ test("renders virtualized ordering by index when passed selectedData", () => {
10
+ // Set initial selectedData to the second item in the data array
11
+ let selectedData = data[1];
12
+ // Render the GenericDropdown component with the data, onChange function, textField prop, and selectedData prop
13
+ let result = render(_jsx(GenericDropdown, { data: data, onChange: () => { }, textField: "name", selectedData: selectedData }));
14
+ // Get the input element by its role of "combobox"
15
+ const input = result.getByRole("combobox");
16
+ expect(input.getAttribute("value")).toBe("Option 2");
17
+ // Simulate a change event on the input element by setting its target value to "O"
18
+ fireEvent.change(input, { target: { value: "O" } });
19
+ // Simulate a key down event on the input element with the key value of "ArrowDown"
20
+ fireEvent.keyDown(input, { key: "ArrowDown" });
21
+ // Query the rendered result for elements with the text "Option 1", "Option 2", and "Option 3"
22
+ let item1 = result.queryByText("Option 1");
23
+ let item2 = result.queryByText("Option 2");
24
+ let item3 = result.queryByText("Option 3");
25
+ // Ensure that item1, item2, and item3 are not undefined (i.e., they are rendered)
26
+ expect(item1).not.toBeUndefined();
27
+ expect(item2).not.toBeUndefined();
28
+ expect(item3).not.toBeUndefined();
29
+ // Ensure that there are no elements with the text "Option 9" (i.e., it is not rendered)
30
+ expect(result.queryAllByText("Option 9")).toHaveLength(0);
31
+ });
32
+ test("renders virtualized ordering by index when passed selectedId", () => {
33
+ // Set initial selectedData to the second item in the data array
34
+ let selectedId = 2;
35
+ // Render the GenericDropdown component with the data, onChange function, textField prop, and selectedData prop
36
+ let result = render(_jsx(GenericDropdown, { data: data, onChange: () => { }, textField: "name", selectedId: selectedId }));
37
+ // Get the input element by its role of "combobox"
38
+ const input = result.getByRole("combobox");
39
+ expect(input.getAttribute("value")).toBe("Option 2");
40
+ // Simulate a change event on the input element by setting its target value to "O"
41
+ fireEvent.change(input, { target: { value: "O" } });
42
+ // Simulate a key down event on the input element with the key value of "ArrowDown"
43
+ fireEvent.keyDown(input, { key: "ArrowDown" });
44
+ // Query the rendered result for elements with the text "Option 1", "Option 2", and "Option 3"
45
+ let item1 = result.queryByText("Option 1");
46
+ let item2 = result.queryByText("Option 2");
47
+ let item3 = result.queryByText("Option 3");
48
+ // Ensure that item1, item2, and item3 are not undefined (i.e., they are rendered)
49
+ expect(item1).not.toBeUndefined();
50
+ expect(item2).not.toBeUndefined();
51
+ expect(item3).not.toBeUndefined();
52
+ // Ensure that there are no elements with the text "Option 9" (i.e., it is not rendered)
53
+ expect(result.queryAllByText("Option 9")).toHaveLength(0);
54
+ });
55
+ test("handles change event when selecting by object", () => {
56
+ // Set initial selectedData to the second item in the data array
57
+ let selectedData = data[1];
58
+ // Render the GenericDropdown component with the data, onChange function, textField prop, and selectedData prop
59
+ let result = render(_jsx(GenericDropdown, { data: data, onChange: (e) => (selectedData = data[e.value]), textField: "name", selectedData: selectedData }));
60
+ // Get the input element by its role of "combobox"
61
+ // Simulate a change event on the input element by setting its target value to "O"
62
+ let input = result.getByRole("combobox");
63
+ fireEvent.change(input, { target: { value: "O" } });
64
+ // Get the item element with the text "Option 2"
65
+ let item2 = result.getByText("Option 2");
66
+ // Simulate a click event on the item element
67
+ fireEvent.click(item2);
68
+ // Ensure that the input element's value attribute is set to "Option 2"
69
+ expect(selectedData.id).toBe(2);
70
+ });
71
+ test("handles change event when selecting by id", () => {
72
+ // Set initial selectedData to the second item in the data array
73
+ let selectedId = 1;
74
+ // Render the GenericDropdown component with the data, onChange function, textField prop, and selectedData prop
75
+ let result = render(_jsx(GenericDropdown, { data: data, onChange: (e) => (selectedId = e.value.id), textField: "name", selectedId: selectedId, idField: "id" }));
76
+ // Get the input element by its role of "combobox"
77
+ // Simulate a change event on the input element by setting its target value to "O"
78
+ let input = result.getByRole("combobox");
79
+ fireEvent.change(input, { target: { value: "O" } });
80
+ // Get the item element with the text "Option 2"
81
+ let item2 = result.getByText("Option 2");
82
+ // Simulate a click event on the item element
83
+ fireEvent.click(item2);
84
+ // Ensure that the input element's value attribute is set to "Option 2"
85
+ expect(selectedId).toBe(2);
86
+ });
87
+ test("only displays items that match the search", () => {
88
+ // Set initial selectedData to the second item in the data array
89
+ let selectedData = data[1];
90
+ // Render the GenericDropdown component with the data, onChange function, textField prop, and selectedData prop
91
+ let result = render(_jsx(GenericDropdown, { data: data, onChange: (e) => (selectedData = data[e.value]), textField: "name", selectedData: selectedData }));
92
+ let input = result.getByRole("combobox"); // Get the input element by its role of "combobox"
93
+ fireEvent.change(input, { target: { value: "9" } }); //simulate a change event on the input element
94
+ // Query the rendered result for elements with the text "Option 1" and "Option 9", and store the results in variables
95
+ let item1 = result.queryAllByText("Option 1");
96
+ let item9 = result.queryAllByText("Option 9");
97
+ // Ensure that there are no elements with the text "Option 1" and at least one element with the text "Option 9"
98
+ expect(item1).toHaveLength(0);
99
+ expect(item9.length).toBeGreaterThan(0);
100
+ });
101
+ });
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@scheels-softdev/kendoreact-generics",
3
- "version": "2.6.1",
3
+ "version": "2.6.3",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
- "test": "echo \"Error: no test specified\" && exit 1",
6
+ "test": "jest",
7
7
  "clean": "rmdir /S /Q dist && mkdir dist",
8
8
  "build": "npm run clean && tsc && copy package.json .\\dist && copy README.md .\\dist "
9
9
  },
@@ -19,8 +19,13 @@
19
19
  "@progress/kendo-react-grid": "latest",
20
20
  "@progress/kendo-react-indicators": "^5.14.1",
21
21
  "@progress/kendo-react-inputs": "latest",
22
+ "@scheels-softdev/frontend-utility-functions": "^1.3.7",
23
+ "@testing-library/react": "^14.0.0",
24
+ "@types/jest": "^29.5.3",
22
25
  "@types/moment": "latest",
23
26
  "@types/react": "latest",
27
+ "jest": "^29.6.2",
28
+ "jest-environment-jsdom": "^29.6.2",
24
29
  "moment": "latest",
25
30
  "npmrc": "latest",
26
31
  "react": "latest",
@@ -37,6 +42,7 @@
37
42
  "moment": "^2.29.4",
38
43
  "react": "^18.2.0",
39
44
  "react-dom": "^18.2.0",
45
+ "ts-jest": "^29.1.1",
40
46
  "typescript": "^4.9.5"
41
47
  },
42
48
  "description": ""