@olenbetong/synergi-react 5.0.0 → 5.2.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 (168) hide show
  1. package/dist/styles/styles.css +1 -1
  2. package/dist/styles/styles.css.map +1 -1
  3. package/es/components/Checkbox/index.css +1 -1
  4. package/es/components/Checkbox/index.js +1 -9
  5. package/es/components/CodeEditor/CodeEditor.css +6 -6
  6. package/es/components/ColorCard/index.css +17 -28
  7. package/es/components/DateNavigator/index.css +5 -7
  8. package/es/components/FilePond.js +3 -12
  9. package/es/components/ImageEditor/ImageEditor.css +10 -10
  10. package/es/components/ImageEditor/ImageEditor.js +4 -4
  11. package/es/components/ImageEditor/ResponsiveButton.js +2 -2
  12. package/es/components/ImageEditor/plugins/cropPlugin/CropToolbar.js +1 -1
  13. package/es/components/ImageEditor/plugins/redactPlugin/RedactToolbar.js +1 -1
  14. package/es/components/ImageEditor/plugins/redactPlugin/useRedactTool.js +18 -14
  15. package/es/components/ImageEditor/useImageHistory.js +13 -1
  16. package/es/components/MergeProgressDialog/MergeProgressDialogDs.js +3 -3
  17. package/es/components/MergeProgressDialog/useMergeJob.js +5 -4
  18. package/es/components/PageBanner/index.css +1 -1
  19. package/es/components/PdfViewer/PdfViewer.js +1 -1
  20. package/es/components/Portal/index.js +20 -20
  21. package/es/components/Spinner/index.css +1 -1
  22. package/es/components/SplitContainer/index.css +1 -1
  23. package/es/components/SplitContainer/index.js +3 -1
  24. package/es/components/Toolbar/Toolbar.css +137 -137
  25. package/es/components/Toolbar/index.js +6 -1
  26. package/es/components/ValueToggleGroup/index.css +2 -2
  27. package/es/elements/ElementLookupDialog/BarcodeScanner/BarcodeScanner.css +7 -7
  28. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.js +8 -0
  29. package/es/elements/ElementLookupDialog/HIDScanner/useHIDScanner.js +3 -1
  30. package/es/elements-ds/DescriptionInput.d.ts +16 -0
  31. package/es/elements-ds/DescriptionInput.js +41 -0
  32. package/es/elements-ds/DuplicateElementDamage.d.ts +40 -0
  33. package/es/elements-ds/DuplicateElementDamage.js +57 -0
  34. package/es/elements-ds/ElementLookup.d.ts +23 -0
  35. package/es/elements-ds/ElementLookup.js +35 -0
  36. package/es/elements-ds/ElementLookupDialog.d.ts +7 -0
  37. package/es/elements-ds/ElementLookupDialog.js +84 -0
  38. package/es/elements-ds/FormFields.d.ts +43 -0
  39. package/es/elements-ds/FormFields.js +67 -0
  40. package/es/elements-ds/HIDScanner.d.ts +7 -0
  41. package/es/elements-ds/HIDScanner.js +36 -0
  42. package/es/elements-ds/ManualForm.d.ts +7 -0
  43. package/es/elements-ds/ManualForm.js +70 -0
  44. package/es/elements-ds/ProjectSelect.d.ts +28 -0
  45. package/es/elements-ds/ProjectSelect.js +45 -0
  46. package/es/elements-ds/elements-ds.css +157 -0
  47. package/es/elements-ds/index.d.ts +12 -0
  48. package/es/elements-ds/index.js +14 -0
  49. package/es/index.css +1 -1
  50. package/es/useTranslation/index.js +5 -3
  51. package/package.json +16 -4
  52. package/src/chrome/AppShell/AppShell.test.tsx +48 -0
  53. package/src/chrome/AppShell/index.tsx +29 -29
  54. package/src/chrome/openReport.test.ts +50 -0
  55. package/src/chrome/openReport.ts +34 -34
  56. package/src/colorScheme.test.ts +188 -0
  57. package/src/colorScheme.ts +125 -125
  58. package/src/components/Checkbox/Checkbox.browser.test.tsx +116 -0
  59. package/src/components/Checkbox/index.scss +2 -2
  60. package/src/components/Checkbox/index.tsx +1 -10
  61. package/src/components/CodeEditor/CodeEditor.css +6 -6
  62. package/src/components/CodeEditor/CodeEditor.tsx +173 -173
  63. package/src/components/CodeEditor/index.ts +1 -1
  64. package/src/components/ColorCard/ColorCard.test.tsx +34 -0
  65. package/src/components/ColorCard/index.css +17 -28
  66. package/src/components/DateNavigator/DateNavigator.test.tsx +175 -0
  67. package/src/components/DateNavigator/index.css +5 -7
  68. package/src/components/DateNavigator/index.tsx +186 -186
  69. package/src/components/FilePond.browser.test.tsx +389 -0
  70. package/src/components/FilePond.tsx +303 -312
  71. package/src/components/ImageEditor/ImageEditor.browser.test.tsx +286 -0
  72. package/src/components/ImageEditor/ImageEditor.css +10 -10
  73. package/src/components/ImageEditor/ImageEditor.test.tsx +299 -0
  74. package/src/components/ImageEditor/ImageEditor.tsx +348 -348
  75. package/src/components/ImageEditor/ResponsiveButton.tsx +2 -2
  76. package/src/components/ImageEditor/index.ts +9 -9
  77. package/src/components/ImageEditor/plugins/cropPlugin/CropOverlay.tsx +165 -165
  78. package/src/components/ImageEditor/plugins/cropPlugin/CropToolbar.tsx +42 -48
  79. package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.test.ts +225 -0
  80. package/src/components/ImageEditor/plugins/cropPlugin/useCropTool.ts +405 -405
  81. package/src/components/ImageEditor/plugins/plugins.browser.test.ts +252 -0
  82. package/src/components/ImageEditor/plugins/redactPlugin/RedactToolbar.tsx +47 -47
  83. package/src/components/ImageEditor/plugins/redactPlugin/useRedactTool.test.ts +89 -0
  84. package/src/components/ImageEditor/plugins/redactPlugin/useRedactTool.ts +16 -13
  85. package/src/components/ImageEditor/plugins/zoomPlugin/ZoomContext.tsx +23 -23
  86. package/src/components/ImageEditor/plugins/zoomPlugin/ZoomControls.tsx +40 -40
  87. package/src/components/ImageEditor/plugins/zoomPlugin/useZoomPan.test.ts +154 -0
  88. package/src/components/ImageEditor/useActiveTool.ts +21 -21
  89. package/src/components/ImageEditor/useEdgeDetection.test.ts +96 -0
  90. package/src/components/ImageEditor/useEdgeDetection.ts +319 -319
  91. package/src/components/ImageEditor/useImageHistory.test.ts +409 -0
  92. package/src/components/ImageEditor/useImageHistory.ts +206 -197
  93. package/src/components/ImageEditor/useOpenCV.test.ts +208 -0
  94. package/src/components/ImageEditor/useOpenCV.ts +184 -184
  95. package/src/components/LinkedCardList/index.scss +95 -95
  96. package/src/components/MergeProgressDialog/MergeProgressDialogDs.browser.test.tsx +156 -0
  97. package/src/components/MergeProgressDialog/MergeProgressDialogDs.tsx +4 -3
  98. package/src/components/MergeProgressDialog/useMergeJob.test.ts +326 -0
  99. package/src/components/MergeProgressDialog/useMergeJob.ts +4 -3
  100. package/src/components/PageBanner/index.scss +10 -10
  101. package/src/components/PageBanner/index.tsx +80 -80
  102. package/src/components/PdfViewer/PdfViewer.browser.test.tsx +78 -0
  103. package/src/components/PdfViewer/PdfViewer.tsx +158 -158
  104. package/src/components/Portal/Portal.test.tsx +106 -0
  105. package/src/components/Portal/index.tsx +22 -25
  106. package/src/components/Sidebar/Sidebar.browser.test.tsx +57 -0
  107. package/src/components/Sidebar/Sidebar.test.tsx +123 -0
  108. package/src/components/Spinner/index.css +1 -1
  109. package/src/components/SplitContainer/SplitContainer.browser.test.tsx +304 -0
  110. package/src/components/SplitContainer/index.scss +5 -1
  111. package/src/components/SplitContainer/index.tsx +335 -333
  112. package/src/components/Toolbar/Toolbar.css +137 -137
  113. package/src/components/Toolbar/Toolbar.test.tsx +216 -0
  114. package/src/components/Toolbar/ToolbarBreadcrumbs.browser.test.tsx +137 -0
  115. package/src/components/Toolbar/index.tsx +270 -265
  116. package/src/components/ValueToggleGroup/ValueToggleGroup.test.tsx +112 -0
  117. package/src/components/ValueToggleGroup/index.css +2 -2
  118. package/src/designsystemet-web-types.d.ts +24 -24
  119. package/src/elements/ElementDamage/DamageFormButtons.tsx +35 -35
  120. package/src/elements/ElementDamage/DescriptionInput.tsx +120 -120
  121. package/src/elements/ElementDamage/DuplicateDamage/DesktopView.tsx +82 -82
  122. package/src/elements/ElementDamage/DuplicateDamage/DublicateElementDamageDialog.tsx +67 -67
  123. package/src/elements/ElementDamage/DuplicateDamage/PhoneView.tsx +45 -45
  124. package/src/elements/ElementDamage/ElementNumberTextField.tsx +41 -41
  125. package/src/elements/ElementDamage/RegisterFix.tsx +100 -100
  126. package/src/elements/ElementDamage/SubmitButtons.tsx +40 -40
  127. package/src/elements/ElementDamage/TransportElementSelect.tsx +52 -52
  128. package/src/elements/ElementLookup.tsx +123 -123
  129. package/src/elements/ElementLookupDialog/BarcodeScanner/BarcodeScanner.browser.test.tsx +175 -0
  130. package/src/elements/ElementLookupDialog/BarcodeScanner/BarcodeScanner.css +7 -7
  131. package/src/elements/ElementLookupDialog/BarcodeScanner/decoders/zxing.ts +8 -0
  132. package/src/elements/ElementLookupDialog/BarcodeScanner/engines.browser.test.ts +225 -0
  133. package/src/elements/ElementLookupDialog/HIDScanner/index.tsx +67 -67
  134. package/src/elements/ElementLookupDialog/HIDScanner/useHIDScanner.test.ts +124 -0
  135. package/src/elements/ElementLookupDialog/HIDScanner/useHIDScanner.ts +2 -1
  136. package/src/elements/ElementLookupDialog/Manual/ManualForm.tsx +141 -141
  137. package/src/elements/ElementLookupDialog/getElement.test.ts +133 -0
  138. package/src/elements/ElementLookupDialog/index.tsx +226 -226
  139. package/src/elements/ElementLookupDialog/utils/Giai.test.ts +53 -0
  140. package/src/elements/ElementLookupDialog/utils/Giai.ts +209 -209
  141. package/src/elements/ProjectSelect.tsx +130 -130
  142. package/src/elements/index.ts +15 -15
  143. package/src/elements-ds/DamageForm.browser.test.tsx +530 -0
  144. package/src/elements-ds/DescriptionInput.tsx +114 -0
  145. package/src/elements-ds/DuplicateElementDamage.tsx +185 -0
  146. package/src/elements-ds/ElementLookup.tsx +114 -0
  147. package/src/elements-ds/ElementLookupDialog.browser.test.tsx +207 -0
  148. package/src/elements-ds/ElementLookupDialog.tsx +194 -0
  149. package/src/elements-ds/FormFields.tsx +176 -0
  150. package/src/elements-ds/HIDScanner.tsx +53 -0
  151. package/src/elements-ds/ManualForm.tsx +129 -0
  152. package/src/elements-ds/ProjectSelect.tsx +120 -0
  153. package/src/elements-ds/elements-ds.css +157 -0
  154. package/src/elements-ds/index.ts +16 -0
  155. package/src/filter/index.ts +34 -34
  156. package/src/i18n.test.ts +31 -0
  157. package/src/i18n.ts +13 -13
  158. package/src/mui/MuiThemeProvider.tsx +135 -135
  159. package/src/test/fakeCamera.ts +98 -0
  160. package/src/test/fakeDataApi.ts +90 -0
  161. package/src/test/setup.browser.ts +21 -0
  162. package/src/test/setup.ts +23 -23
  163. package/src/useTranslation/index.ts +5 -3
  164. package/src/useTranslation/useTranslation.test.ts +128 -0
  165. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders.test.d.ts +0 -1
  166. package/es/elements/ElementLookupDialog/BarcodeScanner/decoders.test.js +0 -103
  167. package/es/test/setup.d.ts +0 -6
  168. 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
- });