@trackunit/react-test-setup 1.0.6-alpha-dd061b2024b.0 → 1.0.6
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.
- package/index.cjs.js +4 -92
- package/index.esm.js +5 -74
- package/package.json +1 -1
- package/src/setupTranslationsImpl.d.ts +18 -0
- package/src/setupWebStreamsImpl.d.ts +16 -0
package/index.cjs.js
CHANGED
|
@@ -8,28 +8,6 @@ require('@testing-library/jest-dom');
|
|
|
8
8
|
var react = require('@testing-library/react');
|
|
9
9
|
var polyfill = require('@js-temporal/polyfill');
|
|
10
10
|
var web = require('@react-spring/web');
|
|
11
|
-
var react$1 = require('react');
|
|
12
|
-
var reactI18next = require('react-i18next');
|
|
13
|
-
var webStreamsPolyfill = require('web-streams-polyfill');
|
|
14
|
-
|
|
15
|
-
function _interopNamespaceDefault(e) {
|
|
16
|
-
var n = Object.create(null);
|
|
17
|
-
if (e) {
|
|
18
|
-
Object.keys(e).forEach(function (k) {
|
|
19
|
-
if (k !== 'default') {
|
|
20
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
21
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
22
|
-
enumerable: true,
|
|
23
|
-
get: function () { return e[k]; }
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
n.default = e;
|
|
29
|
-
return Object.freeze(n);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
var reactI18next__namespace = /*#__PURE__*/_interopNamespaceDefault(reactI18next);
|
|
33
11
|
|
|
34
12
|
/**
|
|
35
13
|
* Sets up a mock implementation for HTML Canvas API in testing environments.
|
|
@@ -510,74 +488,8 @@ const setupTimeAndLanguage = () => {
|
|
|
510
488
|
* setupTranslations();
|
|
511
489
|
*/
|
|
512
490
|
const setupTranslations = () => {
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
const renderNodes = (reactNodes) => {
|
|
516
|
-
if (typeof reactNodes === "string") {
|
|
517
|
-
return reactNodes;
|
|
518
|
-
}
|
|
519
|
-
return Object.keys(reactNodes).map((key, i) => {
|
|
520
|
-
const child = reactNodes[key];
|
|
521
|
-
const isElement = react$1.isValidElement(child);
|
|
522
|
-
if (typeof child === "string") {
|
|
523
|
-
return child;
|
|
524
|
-
}
|
|
525
|
-
if (hasChildren(child)) {
|
|
526
|
-
const inner = renderNodes(getChildren(child));
|
|
527
|
-
return react$1.cloneElement(child, { ...child.props, key: i }, inner);
|
|
528
|
-
}
|
|
529
|
-
if (typeof child === "object" && !isElement) {
|
|
530
|
-
return Object.keys(child).reduce((str, childKey) => `${str}${child[childKey]}`, "");
|
|
531
|
-
}
|
|
532
|
-
return child;
|
|
533
|
-
});
|
|
534
|
-
};
|
|
535
|
-
const i18n = { language: "en", exists: () => true };
|
|
536
|
-
const useMock = [
|
|
537
|
-
(k, extra) => k + (extra ? " props: " + JSON.stringify(extra) : ""),
|
|
538
|
-
i18n,
|
|
539
|
-
];
|
|
540
|
-
useMock.t = (k, extra) => k + (extra ? " props: " + JSON.stringify(extra) : "");
|
|
541
|
-
useMock.i18n = i18n;
|
|
542
|
-
const useTranslation = () => useMock;
|
|
543
|
-
const Trans = ({ i18nKey, components, children }) => {
|
|
544
|
-
const result = [];
|
|
545
|
-
result.push(renderNodes([i18nKey]));
|
|
546
|
-
if (Array.isArray(children)) {
|
|
547
|
-
result.push(...renderNodes(children));
|
|
548
|
-
}
|
|
549
|
-
else if (children && "toArray" in children) {
|
|
550
|
-
result.push(...renderNodes(children.toArray()));
|
|
551
|
-
}
|
|
552
|
-
else {
|
|
553
|
-
result.push(...renderNodes([children]));
|
|
554
|
-
}
|
|
555
|
-
if (components) {
|
|
556
|
-
result.push(...Object.keys(components).map(key => jsxRuntime.jsx("span", { children: components[key] }, key)));
|
|
557
|
-
}
|
|
558
|
-
return jsxRuntime.jsx(jsxRuntime.Fragment, { children: result });
|
|
559
|
-
};
|
|
560
|
-
const Translation = ({ children }) => children((k) => k, { i18n });
|
|
561
|
-
jest.doMock("react-i18next", () => {
|
|
562
|
-
return {
|
|
563
|
-
// this mock makes sure any components using the translate HoC receive the t function as a prop
|
|
564
|
-
Trans,
|
|
565
|
-
Translation,
|
|
566
|
-
useTranslation,
|
|
567
|
-
// mock if needed
|
|
568
|
-
I18nextProvider: reactI18next__namespace.I18nextProvider,
|
|
569
|
-
initReactI18next: reactI18next__namespace.initReactI18next,
|
|
570
|
-
setDefaults: reactI18next__namespace.setDefaults,
|
|
571
|
-
getDefaults: reactI18next__namespace.getDefaults,
|
|
572
|
-
setI18n: reactI18next__namespace.setI18n,
|
|
573
|
-
getI18n: reactI18next__namespace.getI18n,
|
|
574
|
-
};
|
|
575
|
-
});
|
|
576
|
-
jest.doMock("@trackunit/i18n-library-translation", () => ({
|
|
577
|
-
...jest.requireActual("@trackunit/i18n-library-translation"),
|
|
578
|
-
NamespaceTrans: (props) => jsxRuntime.jsx(Trans, { ...props }),
|
|
579
|
-
useNamespaceTranslation: useTranslation,
|
|
580
|
-
}));
|
|
491
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
|
|
492
|
+
require("./setupTranslationsImpl").setupTranslations();
|
|
581
493
|
};
|
|
582
494
|
|
|
583
495
|
/**
|
|
@@ -597,8 +509,8 @@ const setupTranslations = () => {
|
|
|
597
509
|
* setupWebStreams();
|
|
598
510
|
*/
|
|
599
511
|
const setupWebStreams = () => {
|
|
600
|
-
|
|
601
|
-
|
|
512
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
|
|
513
|
+
require("./setupWebStreamsImpl").setupWebStreams();
|
|
602
514
|
};
|
|
603
515
|
|
|
604
516
|
/**
|
package/index.esm.js
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
import failOnConsole from 'jest-fail-on-console';
|
|
2
|
-
import { jsx
|
|
2
|
+
import { jsx } from 'react/jsx-runtime';
|
|
3
3
|
import { APILoadingStatus } from '@vis.gl/react-google-maps';
|
|
4
4
|
import { initialize } from '@googlemaps/jest-mocks';
|
|
5
5
|
import '@testing-library/jest-dom';
|
|
6
6
|
import { cleanup, act } from '@testing-library/react';
|
|
7
7
|
import { Temporal } from '@js-temporal/polyfill';
|
|
8
8
|
import { Globals } from '@react-spring/web';
|
|
9
|
-
import { isValidElement, cloneElement } from 'react';
|
|
10
|
-
import * as reactI18next from 'react-i18next';
|
|
11
|
-
import { TransformStream, WritableStream } from 'web-streams-polyfill';
|
|
12
9
|
|
|
13
10
|
/**
|
|
14
11
|
* Sets up a mock implementation for HTML Canvas API in testing environments.
|
|
@@ -489,74 +486,8 @@ const setupTimeAndLanguage = () => {
|
|
|
489
486
|
* setupTranslations();
|
|
490
487
|
*/
|
|
491
488
|
const setupTranslations = () => {
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
const renderNodes = (reactNodes) => {
|
|
495
|
-
if (typeof reactNodes === "string") {
|
|
496
|
-
return reactNodes;
|
|
497
|
-
}
|
|
498
|
-
return Object.keys(reactNodes).map((key, i) => {
|
|
499
|
-
const child = reactNodes[key];
|
|
500
|
-
const isElement = isValidElement(child);
|
|
501
|
-
if (typeof child === "string") {
|
|
502
|
-
return child;
|
|
503
|
-
}
|
|
504
|
-
if (hasChildren(child)) {
|
|
505
|
-
const inner = renderNodes(getChildren(child));
|
|
506
|
-
return cloneElement(child, { ...child.props, key: i }, inner);
|
|
507
|
-
}
|
|
508
|
-
if (typeof child === "object" && !isElement) {
|
|
509
|
-
return Object.keys(child).reduce((str, childKey) => `${str}${child[childKey]}`, "");
|
|
510
|
-
}
|
|
511
|
-
return child;
|
|
512
|
-
});
|
|
513
|
-
};
|
|
514
|
-
const i18n = { language: "en", exists: () => true };
|
|
515
|
-
const useMock = [
|
|
516
|
-
(k, extra) => k + (extra ? " props: " + JSON.stringify(extra) : ""),
|
|
517
|
-
i18n,
|
|
518
|
-
];
|
|
519
|
-
useMock.t = (k, extra) => k + (extra ? " props: " + JSON.stringify(extra) : "");
|
|
520
|
-
useMock.i18n = i18n;
|
|
521
|
-
const useTranslation = () => useMock;
|
|
522
|
-
const Trans = ({ i18nKey, components, children }) => {
|
|
523
|
-
const result = [];
|
|
524
|
-
result.push(renderNodes([i18nKey]));
|
|
525
|
-
if (Array.isArray(children)) {
|
|
526
|
-
result.push(...renderNodes(children));
|
|
527
|
-
}
|
|
528
|
-
else if (children && "toArray" in children) {
|
|
529
|
-
result.push(...renderNodes(children.toArray()));
|
|
530
|
-
}
|
|
531
|
-
else {
|
|
532
|
-
result.push(...renderNodes([children]));
|
|
533
|
-
}
|
|
534
|
-
if (components) {
|
|
535
|
-
result.push(...Object.keys(components).map(key => jsx("span", { children: components[key] }, key)));
|
|
536
|
-
}
|
|
537
|
-
return jsx(Fragment, { children: result });
|
|
538
|
-
};
|
|
539
|
-
const Translation = ({ children }) => children((k) => k, { i18n });
|
|
540
|
-
jest.doMock("react-i18next", () => {
|
|
541
|
-
return {
|
|
542
|
-
// this mock makes sure any components using the translate HoC receive the t function as a prop
|
|
543
|
-
Trans,
|
|
544
|
-
Translation,
|
|
545
|
-
useTranslation,
|
|
546
|
-
// mock if needed
|
|
547
|
-
I18nextProvider: reactI18next.I18nextProvider,
|
|
548
|
-
initReactI18next: reactI18next.initReactI18next,
|
|
549
|
-
setDefaults: reactI18next.setDefaults,
|
|
550
|
-
getDefaults: reactI18next.getDefaults,
|
|
551
|
-
setI18n: reactI18next.setI18n,
|
|
552
|
-
getI18n: reactI18next.getI18n,
|
|
553
|
-
};
|
|
554
|
-
});
|
|
555
|
-
jest.doMock("@trackunit/i18n-library-translation", () => ({
|
|
556
|
-
...jest.requireActual("@trackunit/i18n-library-translation"),
|
|
557
|
-
NamespaceTrans: (props) => jsx(Trans, { ...props }),
|
|
558
|
-
useNamespaceTranslation: useTranslation,
|
|
559
|
-
}));
|
|
489
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
|
|
490
|
+
require("./setupTranslationsImpl").setupTranslations();
|
|
560
491
|
};
|
|
561
492
|
|
|
562
493
|
/**
|
|
@@ -576,8 +507,8 @@ const setupTranslations = () => {
|
|
|
576
507
|
* setupWebStreams();
|
|
577
508
|
*/
|
|
578
509
|
const setupWebStreams = () => {
|
|
579
|
-
|
|
580
|
-
|
|
510
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
|
|
511
|
+
require("./setupWebStreamsImpl").setupWebStreams();
|
|
581
512
|
};
|
|
582
513
|
|
|
583
514
|
/**
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trackunit/react-test-setup",
|
|
3
3
|
"description": "Test setup utilities for React applications",
|
|
4
|
-
"version": "1.0.6
|
|
4
|
+
"version": "1.0.6",
|
|
5
5
|
"repository": "https://github.com/Trackunit/manager",
|
|
6
6
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
7
7
|
"engines": {
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Implementation of translation mocks for testing environments.
|
|
3
|
+
*
|
|
4
|
+
* This function creates detailed mock implementations for react-i18next and
|
|
5
|
+
* Trackunit i18n library translation utilities. The mocks are designed to:
|
|
6
|
+
*
|
|
7
|
+
* 1. Return translation keys as the translated strings (with any provided props)
|
|
8
|
+
* 2. Render components within Trans components by preserving their React structure
|
|
9
|
+
* 3. Provide a simplified i18n object with expected properties
|
|
10
|
+
* 4. Handle nested components and rendering flows without complex translation logic
|
|
11
|
+
*
|
|
12
|
+
* These mocks allow components that use internationalization to be tested without
|
|
13
|
+
* requiring actual translation files or i18n initialization, simplifying test setup
|
|
14
|
+
* while preserving component rendering behavior.
|
|
15
|
+
*
|
|
16
|
+
* @internal
|
|
17
|
+
*/
|
|
18
|
+
export declare const setupTranslations: () => void;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Implementation for Web Streams API polyfills in testing environments.
|
|
3
|
+
*
|
|
4
|
+
* This function adds the TransformStream and WritableStream implementations from
|
|
5
|
+
* web-streams-polyfill to the global object, making them available to code running
|
|
6
|
+
* in the Jest/JSDOM environment which may not have native implementations of these APIs.
|
|
7
|
+
*
|
|
8
|
+
* These polyfills are essential for testing code that uses modern streaming features,
|
|
9
|
+
* particularly for scenarios involving:
|
|
10
|
+
* - Data processing pipelines
|
|
11
|
+
* - Chunk-by-chunk text or binary processing
|
|
12
|
+
* - Streaming responses from APIs
|
|
13
|
+
*
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
export declare const setupWebStreams: () => void;
|