@olenbetong/synergi-react 5.0.0 → 5.1.0

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.
Files changed (141) hide show
  1. package/es/components/Checkbox/index.js +1 -9
  2. package/es/components/CodeEditor/CodeEditor.css +6 -6
  3. package/es/components/FilePond.js +3 -12
  4. package/es/components/ImageEditor/plugins/redactPlugin/useRedactTool.js +18 -14
  5. package/es/components/ImageEditor/useImageHistory.js +13 -1
  6. package/es/components/MergeProgressDialog/MergeProgressDialogDs.js +1 -1
  7. package/es/components/MergeProgressDialog/useMergeJob.js +5 -4
  8. package/es/components/Portal/index.js +20 -20
  9. package/es/components/SplitContainer/index.js +3 -1
  10. package/es/components/Toolbar/Toolbar.css +137 -137
  11. package/es/components/Toolbar/index.js +6 -1
  12. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.js +8 -0
  13. package/es/elements/ElementLookupDialog/HIDScanner/useHIDScanner.js +3 -1
  14. package/es/elements-ds/DescriptionInput.d.ts +16 -0
  15. package/es/elements-ds/DescriptionInput.js +41 -0
  16. package/es/elements-ds/DuplicateElementDamage.d.ts +40 -0
  17. package/es/elements-ds/DuplicateElementDamage.js +57 -0
  18. package/es/elements-ds/ElementLookup.d.ts +23 -0
  19. package/es/elements-ds/ElementLookup.js +35 -0
  20. package/es/elements-ds/ElementLookupDialog.d.ts +7 -0
  21. package/es/elements-ds/ElementLookupDialog.js +84 -0
  22. package/es/elements-ds/FormFields.d.ts +43 -0
  23. package/es/elements-ds/FormFields.js +67 -0
  24. package/es/elements-ds/HIDScanner.d.ts +7 -0
  25. package/es/elements-ds/HIDScanner.js +36 -0
  26. package/es/elements-ds/ManualForm.d.ts +7 -0
  27. package/es/elements-ds/ManualForm.js +70 -0
  28. package/es/elements-ds/ProjectSelect.d.ts +28 -0
  29. package/es/elements-ds/ProjectSelect.js +45 -0
  30. package/es/elements-ds/elements-ds.css +157 -0
  31. package/es/elements-ds/index.d.ts +12 -0
  32. package/es/elements-ds/index.js +14 -0
  33. package/es/useTranslation/index.js +5 -3
  34. package/package.json +15 -3
  35. package/src/chrome/AppShell/AppShell.test.tsx +48 -0
  36. package/src/chrome/AppShell/index.tsx +29 -29
  37. package/src/chrome/openReport.test.ts +50 -0
  38. package/src/chrome/openReport.ts +34 -34
  39. package/src/colorScheme.test.ts +188 -0
  40. package/src/colorScheme.ts +125 -125
  41. package/src/components/Checkbox/Checkbox.browser.test.tsx +116 -0
  42. package/src/components/Checkbox/index.tsx +1 -10
  43. package/src/components/CodeEditor/CodeEditor.css +6 -6
  44. package/src/components/CodeEditor/CodeEditor.tsx +173 -173
  45. package/src/components/CodeEditor/index.ts +1 -1
  46. package/src/components/ColorCard/ColorCard.test.tsx +34 -0
  47. package/src/components/DateNavigator/DateNavigator.test.tsx +175 -0
  48. package/src/components/DateNavigator/index.tsx +186 -186
  49. package/src/components/FilePond.browser.test.tsx +389 -0
  50. package/src/components/FilePond.tsx +303 -312
  51. package/src/components/ImageEditor/ImageEditor.browser.test.tsx +286 -0
  52. package/src/components/ImageEditor/ImageEditor.test.tsx +299 -0
  53. package/src/components/ImageEditor/ImageEditor.tsx +348 -348
  54. package/src/components/ImageEditor/index.ts +9 -9
  55. package/src/components/ImageEditor/plugins/cropPlugin/CropOverlay.tsx +165 -165
  56. package/src/components/ImageEditor/plugins/cropPlugin/CropToolbar.tsx +48 -48
  57. package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.test.ts +225 -0
  58. package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.ts +405 -405
  59. package/src/components/ImageEditor/plugins/plugins.browser.test.ts +252 -0
  60. package/src/components/ImageEditor/plugins/redactPlugin/RedactToolbar.tsx +47 -47
  61. package/src/components/ImageEditor/plugins/redactPlugin/useRedactTool.test.ts +89 -0
  62. package/src/components/ImageEditor/plugins/redactPlugin/useRedactTool.ts +16 -13
  63. package/src/components/ImageEditor/plugins/zoomPlugin/ZoomContext.tsx +23 -23
  64. package/src/components/ImageEditor/plugins/zoomPlugin/ZoomControls.tsx +40 -40
  65. package/src/components/ImageEditor/plugins/zoomPlugin/useZoomPan.test.ts +154 -0
  66. package/src/components/ImageEditor/useActiveTool.ts +21 -21
  67. package/src/components/ImageEditor/useEdgeDetection.test.ts +96 -0
  68. package/src/components/ImageEditor/useEdgeDetection.ts +319 -319
  69. package/src/components/ImageEditor/useImageHistory.test.ts +409 -0
  70. package/src/components/ImageEditor/useImageHistory.ts +206 -197
  71. package/src/components/ImageEditor/useOpenCV.test.ts +208 -0
  72. package/src/components/ImageEditor/useOpenCV.ts +184 -184
  73. package/src/components/LinkedCardList/index.scss +95 -95
  74. package/src/components/MergeProgressDialog/MergeProgressDialogDs.browser.test.tsx +156 -0
  75. package/src/components/MergeProgressDialog/MergeProgressDialogDs.tsx +1 -0
  76. package/src/components/MergeProgressDialog/useMergeJob.test.ts +326 -0
  77. package/src/components/MergeProgressDialog/useMergeJob.ts +4 -3
  78. package/src/components/PageBanner/index.tsx +80 -80
  79. package/src/components/PdfViewer/PdfViewer.browser.test.tsx +78 -0
  80. package/src/components/PdfViewer/PdfViewer.tsx +158 -158
  81. package/src/components/Portal/Portal.test.tsx +106 -0
  82. package/src/components/Portal/index.tsx +22 -25
  83. package/src/components/Sidebar/Sidebar.browser.test.tsx +57 -0
  84. package/src/components/Sidebar/Sidebar.test.tsx +123 -0
  85. package/src/components/SplitContainer/SplitContainer.browser.test.tsx +304 -0
  86. package/src/components/SplitContainer/index.tsx +335 -333
  87. package/src/components/Toolbar/Toolbar.css +137 -137
  88. package/src/components/Toolbar/Toolbar.test.tsx +216 -0
  89. package/src/components/Toolbar/ToolbarBreadcrumbs.browser.test.tsx +137 -0
  90. package/src/components/Toolbar/index.tsx +270 -265
  91. package/src/components/ValueToggleGroup/ValueToggleGroup.test.tsx +112 -0
  92. package/src/designsystemet-web-types.d.ts +24 -24
  93. package/src/elements/ElementDamage/DamageFormButtons.tsx +35 -35
  94. package/src/elements/ElementDamage/DescriptionInput.tsx +120 -120
  95. package/src/elements/ElementDamage/DuplicateDamage/DesktopView.tsx +82 -82
  96. package/src/elements/ElementDamage/DuplicateDamage/DublicateElementDamageDialog.tsx +67 -67
  97. package/src/elements/ElementDamage/DuplicateDamage/PhoneView.tsx +45 -45
  98. package/src/elements/ElementDamage/ElementNumberTextField.tsx +41 -41
  99. package/src/elements/ElementDamage/RegisterFix.tsx +100 -100
  100. package/src/elements/ElementDamage/SubmitButtons.tsx +40 -40
  101. package/src/elements/ElementDamage/TransportElementSelect.tsx +52 -52
  102. package/src/elements/ElementLookup.tsx +123 -123
  103. package/src/elements/ElementLookupDialog/BarcodeScanner/BarcodeScanner.browser.test.tsx +175 -0
  104. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.ts +8 -0
  105. package/src/elements/ElementLookupDialog/BarcodeScanner/engines.browser.test.ts +225 -0
  106. package/src/elements/ElementLookupDialog/HIDScanner/index.tsx +67 -67
  107. package/src/elements/ElementLookupDialog/HIDScanner/useHIDScanner.test.ts +124 -0
  108. package/src/elements/ElementLookupDialog/HIDScanner/useHIDScanner.ts +2 -1
  109. package/src/elements/ElementLookupDialog/Manual/ManualForm.tsx +141 -141
  110. package/src/elements/ElementLookupDialog/getElement.test.ts +133 -0
  111. package/src/elements/ElementLookupDialog/index.tsx +226 -226
  112. package/src/elements/ElementLookupDialog/utils/Giai.test.ts +53 -0
  113. package/src/elements/ElementLookupDialog/utils/Giai.ts +209 -209
  114. package/src/elements/ProjectSelect.tsx +130 -130
  115. package/src/elements/index.ts +15 -15
  116. package/src/elements-ds/DamageForm.browser.test.tsx +530 -0
  117. package/src/elements-ds/DescriptionInput.tsx +114 -0
  118. package/src/elements-ds/DuplicateElementDamage.tsx +185 -0
  119. package/src/elements-ds/ElementLookup.tsx +114 -0
  120. package/src/elements-ds/ElementLookupDialog.browser.test.tsx +207 -0
  121. package/src/elements-ds/ElementLookupDialog.tsx +196 -0
  122. package/src/elements-ds/FormFields.tsx +176 -0
  123. package/src/elements-ds/HIDScanner.tsx +53 -0
  124. package/src/elements-ds/ManualForm.tsx +129 -0
  125. package/src/elements-ds/ProjectSelect.tsx +120 -0
  126. package/src/elements-ds/elements-ds.css +157 -0
  127. package/src/elements-ds/index.ts +16 -0
  128. package/src/filter/index.ts +34 -34
  129. package/src/i18n.test.ts +31 -0
  130. package/src/i18n.ts +13 -13
  131. package/src/mui/MuiThemeProvider.tsx +135 -135
  132. package/src/test/fakeCamera.ts +98 -0
  133. package/src/test/fakeDataApi.ts +90 -0
  134. package/src/test/setup.browser.ts +21 -0
  135. package/src/test/setup.ts +23 -23
  136. package/src/useTranslation/index.ts +5 -3
  137. package/src/useTranslation/useTranslation.test.ts +128 -0
  138. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders.test.d.ts +0 -1
  139. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders.test.js +0 -103
  140. package/es/test/setup.d.ts +0 -6
  141. package/es/test/setup.js +0 -6
@@ -0,0 +1,90 @@
1
+ /*
2
+ * A stand-in for the Appframe data API in tests: every data object and `DataProviderHandlerAPI`
3
+ * reads from in-memory tables keyed by view name instead of the server. It understands the filters
4
+ * the components send - `[Col] = 'value'`, `Col LIKE '%text%'`, `AND`, `1=0`, `1=1` - and throws on
5
+ * anything else, so a test notices a query it was not written for.
6
+ */
7
+ import { DataProviderHandlerAPI } from "@olenbetong/appframe-data";
8
+ import { vi } from "vitest";
9
+
10
+ type Row = Record<string, unknown>;
11
+ type Retrieve = { resource: string; whereClause: string; filterString: string };
12
+
13
+ function parseCondition(condition: string): (row: Row) => boolean {
14
+ let text = condition.trim();
15
+ while (text.startsWith("(") && text.endsWith(")")) text = text.slice(1, -1).trim();
16
+ if (text === "1=1" || text === "1 = 1") return () => true;
17
+ if (text === "1=0" || text === "1 = 0" || text === "1=2") return () => false;
18
+
19
+ let equals = /^\[?(\w+)\]?\s*=\s*(?:'([^']*)'|(-?\d+(?:\.\d+)?))$/.exec(text);
20
+ if (equals) {
21
+ let [, column, quoted, number] = equals;
22
+ return (row) => String(row[column] ?? "") === (quoted ?? number);
23
+ }
24
+
25
+ let isNull = /^\[?(\w+)\]?\s+IS\s+(NOT\s+)?NULL$/i.exec(text);
26
+ if (isNull) {
27
+ let [, column, not] = isNull;
28
+ return (row) => (row[column] == null) !== Boolean(not);
29
+ }
30
+
31
+ let like = /^\[?(\w+)\]?\s+LIKE\s+'%([^%']*)%'$/i.exec(text);
32
+ if (like) {
33
+ let [, column, value] = like;
34
+ return (row) =>
35
+ String(row[column] ?? "")
36
+ .toLowerCase()
37
+ .includes(value.toLowerCase());
38
+ }
39
+
40
+ throw new Error(`fakeDataApi does not understand the condition "${text}"`);
41
+ }
42
+
43
+ function matches(row: Row, filter: string | undefined) {
44
+ if (!filter?.trim()) return true;
45
+ return filter.split(/\s+AND\s+/i).every((condition) => parseCondition(condition)(row));
46
+ }
47
+
48
+ export type FakeDataApi = {
49
+ /** Sets the rows a view returns. */
50
+ set(resource: string, rows: Row[]): void;
51
+ /** Every retrieve so far, with its view and filters. */
52
+ retrieves: Retrieve[];
53
+ restore(): void;
54
+ };
55
+
56
+ export function installFakeDataApi(): FakeDataApi {
57
+ let tables = new Map<string, Row[]>();
58
+ let retrieves: Retrieve[] = [];
59
+
60
+ let spy = vi.spyOn(DataProviderHandlerAPI.prototype, "retrieve").mockImplementation(async function (
61
+ this: DataProviderHandlerAPI<Row>,
62
+ parameters: { whereClause?: string; filterString?: string; skip?: number; maxRecords?: number },
63
+ ) {
64
+ let resource = (this as unknown as { _options: { dataSourceId: string } })._options.dataSourceId;
65
+ let whereClause = parameters?.whereClause ?? "";
66
+ let filterString = parameters?.filterString ?? "";
67
+ retrieves.push({ resource, whereClause, filterString });
68
+
69
+ let rows = (tables.get(resource) ?? []).filter((row) => matches(row, whereClause) && matches(row, filterString));
70
+
71
+ // Dynamic-loading (paged) data objects send `skip` and expect a page with the total back.
72
+ if (typeof parameters?.skip === "number") {
73
+ let { skip, maxRecords = -1 } = parameters;
74
+ let data = maxRecords > 0 ? rows.slice(skip, skip + maxRecords) : rows.slice(skip);
75
+ return { data, skip, total: rows.length };
76
+ }
77
+
78
+ return rows;
79
+ } as any);
80
+
81
+ return {
82
+ set(resource, rows) {
83
+ tables.set(resource, rows);
84
+ },
85
+ retrieves,
86
+ restore() {
87
+ spy.mockRestore();
88
+ },
89
+ };
90
+ }
@@ -0,0 +1,21 @@
1
+ // What a 2026 page loads before the app: Designsystemet's theme, styles and web components.
2
+ import "@digdir/designsystemet-css/theme";
3
+ import "@digdir/designsystemet-css";
4
+ import "@digdir/designsystemet-web";
5
+ import "@testing-library/jest-dom/vitest";
6
+
7
+ import { cleanup } from "@testing-library/react";
8
+ import { afterEach } from "vitest";
9
+
10
+ let globals = globalThis as any;
11
+
12
+ // The Appframe globals the components read on a page.
13
+ globals.af = globals.af ?? {
14
+ article: { id: "test", features: {} },
15
+ common: { logError: () => undefined },
16
+ userSession: { culture: "en-GB", domain: "OB", login: "tester" },
17
+ };
18
+
19
+ afterEach(() => {
20
+ cleanup();
21
+ });
package/src/test/setup.ts CHANGED
@@ -1,23 +1,23 @@
1
- import "@testing-library/jest-dom/vitest";
2
-
3
- import type { TestingLibraryMatchers } from "@testing-library/jest-dom/matchers";
4
-
5
- import { cleanup } from "@testing-library/react";
6
- import { afterEach } from "vitest";
7
-
8
- afterEach(() => {
9
- cleanup();
10
- });
11
-
12
- /*
13
- * jest-dom 7.0.1 types its matchers onto the one-parameter `Assertion<T>` of Vitest 4, which
14
- * Vitest 5 replaced with `Assertion<R, T>`; custom matchers now go on `Matchers<R, T>`. Until
15
- * jest-dom ships Vitest 5 types, register them there. Remove this when its `vitest` entry
16
- * types `Matchers` itself.
17
- */
18
- declare module "vitest" {
19
- interface Matchers<R extends void | Promise<void> = void | Promise<void>, T = unknown> extends TestingLibraryMatchers<
20
- T,
21
- R
22
- > {}
23
- }
1
+ import "@testing-library/jest-dom/vitest";
2
+
3
+ import type { TestingLibraryMatchers } from "@testing-library/jest-dom/matchers";
4
+
5
+ import { cleanup } from "@testing-library/react";
6
+ import { afterEach } from "vitest";
7
+
8
+ afterEach(() => {
9
+ cleanup();
10
+ });
11
+
12
+ /*
13
+ * jest-dom 7.0.1 types its matchers onto the one-parameter `Assertion<T>` of Vitest 4, which
14
+ * Vitest 5 replaced with `Assertion<R, T>`; custom matchers now go on `Matchers<R, T>`. Until
15
+ * jest-dom ships Vitest 5 types, register them there. Remove this when its `vitest` entry
16
+ * types `Matchers` itself.
17
+ */
18
+ declare module "vitest" {
19
+ interface Matchers<R extends void | Promise<void> = void | Promise<void>, T = unknown> extends TestingLibraryMatchers<
20
+ T,
21
+ R
22
+ > {}
23
+ }
@@ -16,7 +16,7 @@ async function translateText(text: string, target: string, apiKey: string) {
16
16
  let json = await result.json();
17
17
  let { data } = json;
18
18
 
19
- if (data?.translations) {
19
+ if (data?.translations?.length) {
20
20
  let [translation] = data.translations;
21
21
 
22
22
  return {
@@ -25,7 +25,9 @@ async function translateText(text: string, target: string, apiKey: string) {
25
25
  };
26
26
  }
27
27
 
28
- return false;
28
+ // An error answer (invalid key, quota) carries no translations; report it rather than leave
29
+ // the caller translating forever.
30
+ throw new Error(json?.error?.message ?? "The translation service returned no translation.");
29
31
  }
30
32
 
31
33
  export function useTranslation(text: string, targetLanguage: string, apiKey: string) {
@@ -51,7 +53,7 @@ export function useTranslation(text: string, targetLanguage: string, apiKey: str
51
53
 
52
54
  try {
53
55
  let result = await translateText(text, targetLanguage, apiKey);
54
- if (result && textRef.current === text) {
56
+ if (textRef.current === text) {
55
57
  let { from, translation } = result;
56
58
  setSourceLang(from);
57
59
  setTranslation(translation);
@@ -0,0 +1,128 @@
1
+ import { act, renderHook } from "@testing-library/react";
2
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
3
+
4
+ import { useTranslation } from "./index";
5
+
6
+ let fetchMock: ReturnType<typeof vi.fn>;
7
+
8
+ function respond(body: unknown) {
9
+ fetchMock.mockResolvedValueOnce({ json: async () => body });
10
+ }
11
+
12
+ function translated(translatedText: string, detectedSourceLanguage = "ru") {
13
+ return { data: { translations: [{ translatedText, detectedSourceLanguage }] } };
14
+ }
15
+
16
+ /** A fetch that answers only when the test says so. */
17
+ function deferResponse() {
18
+ let answer!: (body: unknown) => void;
19
+ fetchMock.mockReturnValueOnce(
20
+ new Promise((resolve) => {
21
+ answer = (body) => resolve({ json: async () => body });
22
+ }),
23
+ );
24
+ return (body: unknown) => answer(body);
25
+ }
26
+
27
+ beforeEach(() => {
28
+ fetchMock = vi.fn();
29
+ vi.stubGlobal("fetch", fetchMock);
30
+ });
31
+
32
+ afterEach(() => {
33
+ vi.unstubAllGlobals();
34
+ });
35
+
36
+ describe("useTranslation", () => {
37
+ it("does nothing until asked", () => {
38
+ let { result } = renderHook(() => useTranslation("Привет", "nb", "key"));
39
+
40
+ expect(fetchMock).not.toHaveBeenCalled();
41
+ expect(result.current).toMatchObject({ translation: "", from: null, error: null, isTranslating: false });
42
+ });
43
+
44
+ it("translates the text into the target language", async () => {
45
+ respond(translated("Hei"));
46
+ let { result } = renderHook(() => useTranslation("Привет", "nb", "secret"));
47
+
48
+ await act(() => result.current.translate());
49
+
50
+ expect(result.current).toMatchObject({ translation: "Hei", from: "ru", error: null, isTranslating: false });
51
+ let [url, options] = fetchMock.mock.calls[0];
52
+ expect(url).toBe("https://translation.googleapis.com/language/translate/v2?key=secret");
53
+ expect(options.method).toBe("POST");
54
+ expect(JSON.parse(options.body)).toEqual({ q: "Привет", target: "nb" });
55
+ });
56
+
57
+ it("is translating while the request is out", async () => {
58
+ let answer = deferResponse();
59
+ let { result } = renderHook(() => useTranslation("Привет", "nb", "key"));
60
+
61
+ let pending!: Promise<void>;
62
+ act(() => {
63
+ pending = result.current.translate();
64
+ });
65
+ expect(result.current.isTranslating).toBe(true);
66
+
67
+ await act(async () => {
68
+ answer(translated("Hei"));
69
+ await pending;
70
+ });
71
+ expect(result.current.isTranslating).toBe(false);
72
+ });
73
+
74
+ it("reports a network failure", async () => {
75
+ fetchMock.mockRejectedValueOnce(new Error("Failed to fetch"));
76
+ let { result } = renderHook(() => useTranslation("Привет", "nb", "key"));
77
+
78
+ await act(() => result.current.translate());
79
+
80
+ expect(result.current).toMatchObject({ error: "Failed to fetch", isTranslating: false, translation: "" });
81
+ });
82
+
83
+ it("reports an error the translation service answers with, instead of spinning forever", async () => {
84
+ respond({ error: { code: 400, message: "API key not valid. Please pass a valid API key." } });
85
+ let { result } = renderHook(() => useTranslation("Привет", "nb", "wrong"));
86
+
87
+ await act(() => result.current.translate());
88
+
89
+ expect(result.current).toMatchObject({
90
+ error: "API key not valid. Please pass a valid API key.",
91
+ isTranslating: false,
92
+ translation: "",
93
+ });
94
+ });
95
+
96
+ it("clears the translation when the text changes", async () => {
97
+ respond(translated("Hei"));
98
+ let { result, rerender } = renderHook(({ text }) => useTranslation(text, "nb", "key"), {
99
+ initialProps: { text: "Привет" },
100
+ });
101
+ await act(() => result.current.translate());
102
+ expect(result.current.translation).toBe("Hei");
103
+
104
+ rerender({ text: "Пока" });
105
+
106
+ expect(result.current).toMatchObject({ translation: "", from: null, error: null, isTranslating: false });
107
+ });
108
+
109
+ it("drops a translation that arrives after the text has changed", async () => {
110
+ let answer = deferResponse();
111
+ let { result, rerender } = renderHook(({ text }) => useTranslation(text, "nb", "key"), {
112
+ initialProps: { text: "Привет" },
113
+ });
114
+
115
+ let pending!: Promise<void>;
116
+ act(() => {
117
+ pending = result.current.translate();
118
+ });
119
+ rerender({ text: "Пока" });
120
+
121
+ await act(async () => {
122
+ answer(translated("Hei"));
123
+ await pending;
124
+ });
125
+
126
+ expect(result.current).toMatchObject({ translation: "", from: null, isTranslating: false });
127
+ });
128
+ });
@@ -1,103 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { mapDbrResult } from "./decoders/dbr";
3
- import { mapNativeResult } from "./decoders/native";
4
- import { mapZxingResult } from "./decoders/zxing";
5
- import { createDuplicateFilter } from "./duplicateFilter";
6
- const GS = "";
7
- const GIAI = `8004${"7043210"}${GS}${"12345"}`;
8
- describe("result mapping", () => {
9
- it("maps a zxing result to corners in reading order", () => {
10
- let mapped = mapZxingResult({
11
- text: GIAI,
12
- format: "DataMatrix",
13
- isValid: true,
14
- position: {
15
- topLeft: { x: 10, y: 20 },
16
- topRight: { x: 110, y: 20 },
17
- bottomRight: { x: 110, y: 120 },
18
- bottomLeft: { x: 10, y: 120 },
19
- },
20
- });
21
- expect(mapped.text).toBe(GIAI);
22
- expect(mapped.format).toBe("DataMatrix");
23
- expect(mapped.corners).toEqual([
24
- { x: 10, y: 20 },
25
- { x: 110, y: 20 },
26
- { x: 110, y: 120 },
27
- { x: 10, y: 120 },
28
- ]);
29
- });
30
- it("maps a native result from its corner points", () => {
31
- let result = {
32
- rawValue: GIAI,
33
- format: "data_matrix",
34
- cornerPoints: [
35
- { x: 1, y: 2 },
36
- { x: 3, y: 2 },
37
- { x: 3, y: 4 },
38
- { x: 1, y: 4 },
39
- ],
40
- boundingBox: { left: 0, top: 0, right: 0, bottom: 0 },
41
- };
42
- expect(mapNativeResult(result)).toEqual({
43
- text: GIAI,
44
- format: "data_matrix",
45
- corners: [
46
- { x: 1, y: 2 },
47
- { x: 3, y: 2 },
48
- { x: 3, y: 4 },
49
- { x: 1, y: 4 },
50
- ],
51
- });
52
- });
53
- it("falls back to the bounding box when a native result has no corner points", () => {
54
- let result = {
55
- rawValue: GIAI,
56
- format: "data_matrix",
57
- cornerPoints: [],
58
- boundingBox: { left: 5, top: 6, right: 15, bottom: 16 },
59
- };
60
- expect(mapNativeResult(result).corners).toEqual([
61
- { x: 5, y: 6 },
62
- { x: 15, y: 6 },
63
- { x: 15, y: 16 },
64
- { x: 5, y: 16 },
65
- ]);
66
- });
67
- it("maps a Dynamsoft result from its localization result", () => {
68
- let mapped = mapDbrResult({
69
- BarcodeText: GIAI,
70
- BarcodeFormat: 134217728,
71
- LocalizationResult: { X1: 1, Y1: 2, X2: 3, Y2: 2, X3: 3, Y3: 4, X4: 1, Y4: 4 },
72
- });
73
- expect(mapped.text).toBe(GIAI);
74
- expect(mapped.corners).toEqual([
75
- { x: 1, y: 2 },
76
- { x: 3, y: 2 },
77
- { x: 3, y: 4 },
78
- { x: 1, y: 4 },
79
- ]);
80
- });
81
- });
82
- describe("createDuplicateFilter", () => {
83
- it("reports a code once while it stays in view", () => {
84
- let isNew = createDuplicateFilter(1000);
85
- expect(isNew("a", 0)).toBe(true);
86
- expect(isNew("a", 100)).toBe(false);
87
- expect(isNew("a", 900)).toBe(false);
88
- // Still in view: every sighting refreshes the timer.
89
- expect(isNew("a", 1500)).toBe(false);
90
- });
91
- it("reports a code again once it has been out of view for the forget time", () => {
92
- let isNew = createDuplicateFilter(1000);
93
- expect(isNew("a", 0)).toBe(true);
94
- expect(isNew("a", 1001)).toBe(true);
95
- });
96
- it("tracks codes independently", () => {
97
- let isNew = createDuplicateFilter(1000);
98
- expect(isNew("a", 0)).toBe(true);
99
- expect(isNew("b", 0)).toBe(true);
100
- expect(isNew("a", 10)).toBe(false);
101
- expect(isNew("b", 10)).toBe(false);
102
- });
103
- });
@@ -1,6 +0,0 @@
1
- import "@testing-library/jest-dom/vitest";
2
- import type { TestingLibraryMatchers } from "@testing-library/jest-dom/matchers";
3
- declare module "vitest" {
4
- interface Matchers<R extends void | Promise<void> = void | Promise<void>, T = unknown> extends TestingLibraryMatchers<T, R> {
5
- }
6
- }
package/es/test/setup.js DELETED
@@ -1,6 +0,0 @@
1
- import "@testing-library/jest-dom/vitest";
2
- import { cleanup } from "@testing-library/react";
3
- import { afterEach } from "vitest";
4
- afterEach(() => {
5
- cleanup();
6
- });