@trackunit/react-test-setup 1.0.3 → 1.0.6-alpha-dd061b2024b.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.
- package/index.cjs.js +92 -4
- package/index.esm.js +74 -5
- package/package.json +1 -1
- package/src/setupTranslationsImpl.d.ts +0 -18
- package/src/setupWebStreamsImpl.d.ts +0 -16
package/index.cjs.js
CHANGED
|
@@ -8,6 +8,28 @@ 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);
|
|
11
33
|
|
|
12
34
|
/**
|
|
13
35
|
* Sets up a mock implementation for HTML Canvas API in testing environments.
|
|
@@ -488,8 +510,74 @@ const setupTimeAndLanguage = () => {
|
|
|
488
510
|
* setupTranslations();
|
|
489
511
|
*/
|
|
490
512
|
const setupTranslations = () => {
|
|
491
|
-
|
|
492
|
-
|
|
513
|
+
const hasChildren = (node) => node && (node.children || (node.props && node.props.children));
|
|
514
|
+
const getChildren = (node) => (node && node.children ? node.children : node.props && node.props.children);
|
|
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
|
+
}));
|
|
493
581
|
};
|
|
494
582
|
|
|
495
583
|
/**
|
|
@@ -509,8 +597,8 @@ const setupTranslations = () => {
|
|
|
509
597
|
* setupWebStreams();
|
|
510
598
|
*/
|
|
511
599
|
const setupWebStreams = () => {
|
|
512
|
-
|
|
513
|
-
|
|
600
|
+
global.TransformStream = webStreamsPolyfill.TransformStream;
|
|
601
|
+
global.WritableStream = webStreamsPolyfill.WritableStream;
|
|
514
602
|
};
|
|
515
603
|
|
|
516
604
|
/**
|
package/index.esm.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import failOnConsole from 'jest-fail-on-console';
|
|
2
|
-
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import { jsx, Fragment } 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';
|
|
9
12
|
|
|
10
13
|
/**
|
|
11
14
|
* Sets up a mock implementation for HTML Canvas API in testing environments.
|
|
@@ -486,8 +489,74 @@ const setupTimeAndLanguage = () => {
|
|
|
486
489
|
* setupTranslations();
|
|
487
490
|
*/
|
|
488
491
|
const setupTranslations = () => {
|
|
489
|
-
|
|
490
|
-
|
|
492
|
+
const hasChildren = (node) => node && (node.children || (node.props && node.props.children));
|
|
493
|
+
const getChildren = (node) => (node && node.children ? node.children : node.props && node.props.children);
|
|
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
|
+
}));
|
|
491
560
|
};
|
|
492
561
|
|
|
493
562
|
/**
|
|
@@ -507,8 +576,8 @@ const setupTranslations = () => {
|
|
|
507
576
|
* setupWebStreams();
|
|
508
577
|
*/
|
|
509
578
|
const setupWebStreams = () => {
|
|
510
|
-
|
|
511
|
-
|
|
579
|
+
global.TransformStream = TransformStream;
|
|
580
|
+
global.WritableStream = WritableStream;
|
|
512
581
|
};
|
|
513
582
|
|
|
514
583
|
/**
|
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.
|
|
4
|
+
"version": "1.0.6-alpha-dd061b2024b.0",
|
|
5
5
|
"repository": "https://github.com/Trackunit/manager",
|
|
6
6
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
7
7
|
"engines": {
|
|
@@ -1,18 +0,0 @@
|
|
|
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;
|
|
@@ -1,16 +0,0 @@
|
|
|
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;
|