@trackunit/react-test-setup 1.0.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.
@@ -0,0 +1,38 @@
1
+ import { setupFailOnConsole } from "./setupFailOnConsole";
2
+ export interface SetupAllMocksOptions {
3
+ failOnConsoleOptions?: Parameters<typeof setupFailOnConsole>[0];
4
+ }
5
+ /**
6
+ * Sets up all available testing mocks in a single function call.
7
+ *
8
+ * This convenience function sets up all the test mocks provided by the library:
9
+ * - Canvas API mocks
10
+ * - Console error reporting to fail tests
11
+ * - Google Maps API and components mocks
12
+ * - React Helmet mocks
13
+ * - IntersectionObserver mocks
14
+ * - MatchMedia API mocks
15
+ * - React Testing Library and Okta authentication mocks
16
+ * - React Virtualized AutoSizer mocks
17
+ * - ResizeObserver mocks
18
+ * - Tanstack React Virtual mocks
19
+ * - Time and language related mocks (timezone, timers, etc.)
20
+ * - Translation mocks (i18n)
21
+ * - Web Streams API mocks
22
+ *
23
+ * Using this function is equivalent to calling each setup function individually.
24
+ *
25
+ * @param options Configuration options for individual mocks
26
+ * @param options.failOnConsoleOptions Options for setupFailOnConsole
27
+ * @example
28
+ * // In your jest setup file
29
+ * import { setupAllMocks } from '@trackunit/react-test-setup';
30
+ *
31
+ * setupAllMocks();
32
+ *
33
+ * // Or with options for specific mocks:
34
+ * setupAllMocks({
35
+ * failOnConsoleOptions: { shouldFailOnWarn: true }
36
+ * });
37
+ */
38
+ export declare const setupAllMocks: (options?: SetupAllMocksOptions) => void;
@@ -0,0 +1,12 @@
1
+ import { SetupAllMocksOptions } from "./setupAllMocks";
2
+ /**
3
+ * Options for configuring the setupBasicMocks function.
4
+ * Currently shares the same options structure as SetupAllMocksOptions.
5
+ */
6
+ export type SetupBasicMocksOptions = SetupAllMocksOptions;
7
+ /**
8
+ * Sets up essential testing mocks with no external library dependencies.
9
+ *
10
+ * @param options Configuration options for the mocks
11
+ */
12
+ export declare const setupBasicMocks: (options?: SetupBasicMocksOptions) => void;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Sets up a mock implementation for HTML Canvas API in testing environments.
3
+ *
4
+ * This function uses jest-canvas-mock to provide a mock implementation of the
5
+ * HTML Canvas API, allowing tests to run without requiring a real DOM canvas.
6
+ * Useful for testing components that use canvas rendering.
7
+ *
8
+ * @example
9
+ * // In your jest setup file
10
+ * import { setupCanvasMock } from '@trackunit/react-test-setup';
11
+ *
12
+ * setupCanvasMock();
13
+ */
14
+ export declare const setupCanvasMock: () => void;
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Sets up default testing mocks that cover most common React testing needs.
3
+ *
4
+ * This convenience function provides a balanced set of mocks that cover most React
5
+ * application testing needs. It includes all the basic mocks plus essential React
6
+ * testing utilities. Specifically, it includes:
7
+ *
8
+ * From setupBasicMocks:
9
+ * - Canvas API mocks
10
+ * - Console error reporting to fail tests
11
+ * - IntersectionObserver mocks
12
+ * - MatchMedia API mocks
13
+ * - ResizeObserver mocks
14
+ * - Timer mocks (setTimeout, setInterval)
15
+ * - Web Streams API mocks
16
+ *
17
+ * Plus these additional mocks:
18
+ * - React Testing Library and Okta authentication mocks
19
+ * - Translation mocks (i18n)
20
+ *
21
+ * This is ideal for most React applications that use i18n translations and need
22
+ * standard testing environment setup, without requiring specialized mocks for things
23
+ * like Google Maps or virtualized lists.
24
+ *
25
+ * @param options Configuration options for individual mocks
26
+ * @param options.failOnConsoleOptions Options for setupFailOnConsole
27
+ * @example
28
+ * // In your jest setup file
29
+ * import { setupDefaultMocks } from '@trackunit/react-test-setup';
30
+ *
31
+ * setupDefaultMocks();
32
+ *
33
+ * // Or with options for specific mocks:
34
+ * setupDefaultMocks({
35
+ * failOnConsoleOptions: { shouldFailOnWarn: true }
36
+ * });
37
+ */
38
+ import { SetupBasicMocksOptions } from "./setupBasicMocks";
39
+ /**
40
+ * Options for configuring the setupDefaultMocks function.
41
+ * Currently shares the same options structure as SetupBasicMocksOptions.
42
+ */
43
+ export type SetupDefaultMocksOptions = SetupBasicMocksOptions;
44
+ /**
45
+ * Sets up default testing mocks covering most common React testing scenarios.
46
+ *
47
+ * @param options Configuration options for the mocks
48
+ */
49
+ export declare const setupDefaultMocks: (options?: SetupDefaultMocksOptions) => void;
@@ -0,0 +1,13 @@
1
+ import failOnConsole from "jest-fail-on-console";
2
+ /**
3
+ * This will make your tests fail if they log to console.error during the tests.
4
+ * See more details here: https://www.npmjs.com/package/jest-fail-on-console
5
+ *
6
+ * If your tests logs to console.error on purpose, you should spy on the console like so:
7
+ * ```
8
+ * jest.spyOn(console, 'error').mockImplementation()
9
+ * // Do your logic ...
10
+ * expect(console.error).toHaveBeenCalledWith('your error message')
11
+ * ```
12
+ */
13
+ export declare const setupFailOnConsole: (overrides?: Partial<failOnConsole.InitOptions>) => void;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Sets up mocks for Google Maps API and @vis.gl/react-google-maps components in testing environments.
3
+ *
4
+ * This function mocks both the Google Maps JavaScript API and React components from @vis.gl/react-google-maps.
5
+ * It provides mock implementations of Maps, Markers, Geocoder, geometry functions, and places services,
6
+ * allowing tests of map-dependent components to run without requiring the actual Google Maps API.
7
+ *
8
+ * Key features:
9
+ * - Mocks core Map and Marker components with simple test-friendly implementations
10
+ * - Provides placeholders for Google Maps geometry calculations
11
+ * - Stubs out Places API services
12
+ * - Makes map-related hooks always return loaded state
13
+ *
14
+ * @example
15
+ * // In your jest setup file
16
+ * import { setupGoogleMaps } from '@trackunit/react-test-setup';
17
+ *
18
+ * setupGoogleMaps();
19
+ */
20
+ export declare const setupGoogleMaps: () => void;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Mock helmet module
3
+ * See more details here: https://www.npmjs.com/package/react-helmet-async
4
+ */
5
+ export declare const setupHelmetMock: () => void;
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Mocks the IntersectionObserver API for testing environments.
3
+ *
4
+ * This function adds a mock implementation of the IntersectionObserver API to the global window object.
5
+ * The mock implementation provides all the necessary methods (observe, unobserve, disconnect, takeRecords)
6
+ * but with empty implementations, allowing tests of components that use IntersectionObserver to run
7
+ * without errors in Jest's JSDOM environment.
8
+ *
9
+ * Useful for testing components that rely on:
10
+ * - Lazy loading
11
+ * - Infinite scrolling
12
+ * - Visibility-based rendering
13
+ * - Any other intersection-based functionality
14
+ *
15
+ * @example
16
+ * // In your jest setup file
17
+ * import { setupIntersectionObserver } from '@trackunit/react-test-setup';
18
+ *
19
+ * setupIntersectionObserver();
20
+ */
21
+ export declare const setupIntersectionObserver: () => typeof MockIntersectionObserver;
22
+ declare class MockIntersectionObserver {
23
+ readonly root: Element | null;
24
+ readonly rootMargin: string;
25
+ readonly thresholds: readonly number[];
26
+ constructor();
27
+ disconnect(): void;
28
+ observe(): void;
29
+ takeRecords(): IntersectionObserverEntry[];
30
+ unobserve(): void;
31
+ }
32
+ export {};
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Mocks the window.matchMedia API for testing environments.
3
+ *
4
+ * This function creates a mock implementation of the window.matchMedia method that
5
+ * is commonly used for responsive design and media queries. The mock always returns
6
+ * a MediaQueryList-like object with standard methods and properties, but with default
7
+ * values (matches set to false).
8
+ *
9
+ * This allows tests of components that use media queries to run without errors in
10
+ * Jest's JSDOM environment, which doesn't implement matchMedia natively.
11
+ *
12
+ * Ideal for testing:
13
+ * - Responsive components
14
+ * - Components that adapt to screen size changes
15
+ * - Components that use CSS media query matching in JavaScript
16
+ *
17
+ * @example
18
+ * // In your jest setup file
19
+ * import { setupMatchMediaMock } from '@trackunit/react-test-setup';
20
+ *
21
+ * setupMatchMediaMock();
22
+ */
23
+ export declare const setupMatchMediaMock: () => void;
@@ -0,0 +1,25 @@
1
+ import "@testing-library/jest-dom";
2
+ /**
3
+ * Sets up React Testing Library and Okta authentication mocks for testing.
4
+ *
5
+ * This function configures the testing environment with essential mocks for Okta authentication,
6
+ * which is necessary for testing components that use Okta for authentication. It creates
7
+ * mock implementations of Okta authentication objects, tokens, and authentication state,
8
+ * allowing tests to run without requiring an actual Okta authentication flow.
9
+ *
10
+ * The module also imports @testing-library/jest-dom to extend Jest with DOM testing assertions
11
+ * and sets up afterEach hooks to clear mocks and clean up after each test.
12
+ *
13
+ * Key features:
14
+ * - Mocks Okta tokens and authentication state
15
+ * - Creates mock implementations of Okta auth methods
16
+ * - Extends Jest with DOM testing assertions
17
+ * - Automatically cleans up after each test
18
+ *
19
+ * @example
20
+ * // In your jest setup file
21
+ * import { setupReactTestingLibrary } from '@trackunit/react-test-setup';
22
+ *
23
+ * setupReactTestingLibrary();
24
+ */
25
+ export declare const setupReactTestingLibrary: () => void;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Mocks the react-virtualized-auto-sizer component for testing environments.
3
+ *
4
+ * This function creates a mock implementation of the AutoSizer component from
5
+ * react-virtualized-auto-sizer, which is commonly used to measure and adapt to
6
+ * container dimensions. Instead of actual dimension measurements, the mock provides
7
+ * fixed dimensions (600x600), allowing tests to run consistently without actual DOM measuring.
8
+ *
9
+ * This is especially useful for testing components that use virtualized lists or grids,
10
+ * as it eliminates the need for setting up complex DOM environments just to get measurements.
11
+ *
12
+ * @example
13
+ * // In your jest setup file
14
+ * import { setupReactVirtualizedAutoSizer } from '@trackunit/react-test-setup';
15
+ *
16
+ * setupReactVirtualizedAutoSizer();
17
+ */
18
+ export declare const setupReactVirtualizedAutoSizer: () => void;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Mock the ResizeObserver to be able to test components that use a resize observer
3
+ * Like useGeometry, useContainerBreakpoints, etc.
4
+ * Also componets that _use_ useGeometry, like <BaseInput/> and many more
5
+ *
6
+ * Recommended for all react libs
7
+ */
8
+ export declare const setupResizeObserver: () => void;
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Mocks the @tanstack/react-virtual library for testing environments.
3
+ *
4
+ * This function creates a mock implementation of the useVirtualizer hook from
5
+ * the @tanstack/react-virtual library, which is used for efficiently rendering
6
+ * large lists and grids. The mock returns a simplified version that generates
7
+ * virtual items with deterministic sizes and provides all expected methods.
8
+ *
9
+ * Each virtual item has:
10
+ * - A consistent height (40px)
11
+ * - Index and key properties matching the item's position
12
+ * - Empty implementation of the measureRef function
13
+ *
14
+ * This allows for testing components that use virtualized lists without requiring
15
+ * actual DOM measurements or complex setup.
16
+ *
17
+ * @example
18
+ * // In your jest setup file
19
+ * import { setupTanstackReactVirtual } from '@trackunit/react-test-setup';
20
+ *
21
+ * setupTanstackReactVirtual();
22
+ */
23
+ export declare const setupTanstackReactVirtual: () => void;
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Mocks the Temporal API to use a fixed time zone for testing.
3
+ *
4
+ * This function specifically mocks the Temporal.Now.timeZoneId method from the
5
+ * Temporal library to always return "Europe/Copenhagen", ensuring consistent
6
+ * time zone-dependent behavior in tests regardless of where they run.
7
+ *
8
+ * @example
9
+ * // In your jest setup file
10
+ * import { setupTimeZone } from '@trackunit/react-test-setup';
11
+ *
12
+ * setupTimeZone();
13
+ */
14
+ export declare const setupTimeZone: () => void;
15
+ /**
16
+ * Sets up time, animation, and language-related mocks for testing environments.
17
+ *
18
+ * This function configures multiple aspects of the testing environment:
19
+ * 1. Sets a fixed time zone using setupTimeZone()
20
+ * 2. Disables React Spring animations for faster, deterministic tests
21
+ * 3. Mocks the useDebounce hook to immediately return values without delays
22
+ * 4. Overrides setTimeout and setInterval to execute immediately (0ms delay)
23
+ *
24
+ * These changes make tests faster and more predictable by eliminating real-time
25
+ * delays, animations, and time zone dependencies that could cause flaky tests.
26
+ *
27
+ * @example
28
+ * // In your jest setup file
29
+ * import { setupTimeAndLanguage } from '@trackunit/react-test-setup';
30
+ *
31
+ * setupTimeAndLanguage();
32
+ */
33
+ export declare const setupTimeAndLanguage: () => void;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Sets up internationalization and translation mocks for testing environments.
3
+ *
4
+ * This function delegates to the implementation in setupTranslationsImpl.tsx to configure
5
+ * mock translations for react-i18next and @trackunit/i18n-library-translation. It creates
6
+ * simple mock implementations that return the key string as the translation, which allows
7
+ * for testing internationalized components without the complexity of actual translations.
8
+ *
9
+ * The mocks support common translation components and hooks like Trans, useTranslation,
10
+ * NamespaceTrans, and useNamespaceTranslation.
11
+ *
12
+ * @example
13
+ * // In your jest setup file
14
+ * import { setupTranslations } from '@trackunit/react-test-setup';
15
+ *
16
+ * setupTranslations();
17
+ */
18
+ export declare const setupTranslations: () => void;
@@ -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,17 @@
1
+ /**
2
+ * Sets up Web Streams API polyfills for testing environments.
3
+ *
4
+ * This function delegates to the implementation in setupWebStreamsImpl.ts to configure
5
+ * Web Streams API polyfills from web-streams-polyfill. These polyfills provide implementations
6
+ * of modern streaming APIs that may not be available in the Jest/JSDOM testing environment.
7
+ *
8
+ * The setup enables testing of components and utilities that rely on the Web Streams API,
9
+ * such as those that process streaming data or implement custom stream transformations.
10
+ *
11
+ * @example
12
+ * // In your jest setup file
13
+ * import { setupWebStreams } from '@trackunit/react-test-setup';
14
+ *
15
+ * setupWebStreams();
16
+ */
17
+ export declare const setupWebStreams: () => 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;