@tramvai/test-child-app 1.55.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/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # Tramvai test child-app
2
+
3
+ Helpers library for writing unit tests for tramvai child-app
4
+
5
+ Uses [`@tramvai/test-unit`](./test-unit.md) under hood to create test root-app that will wrap child-app.
6
+
7
+ ## How to
8
+
9
+ ### Test child-app main component render
10
+
11
+ You can get React Component returned by child-app from return value of `testChildApp` function and use for example `testComponent` helper from the [`@tramvai/test-react`](./test-react.md)
12
+
13
+ :::warning
14
+
15
+ To properly render child-app component pass as props to it its di and optionally props object, that will be passed to the underlying child-app component.
16
+
17
+ :::
18
+
19
+ ```ts
20
+ import { testComponent } from '@tramvai/test-react';
21
+ import childApp from './child-app.tsx';
22
+
23
+ (async () => {
24
+ const {
25
+ childApp: { Component, di },
26
+ close,
27
+ } = await testChildApp(childApp);
28
+ const { render } = testComponent(<Component di={di} props={{ test: 'abc' }} />);
29
+
30
+ expect(render.getByTestId('from-root').textContent).toBe('Value from Root: abc');
31
+ })();
32
+ ```
33
+
34
+ ### Test child-app di
35
+
36
+ ```ts
37
+ import childApp from './child-app.tsx';
38
+
39
+ (async () => {
40
+ const {
41
+ childApp: { di },
42
+ close,
43
+ } = await testChildApp(childApp);
44
+
45
+ expect(di.get(CHILD_APP_BASE_TOKEN)).toBe("I'm little child app");
46
+ })();
47
+ ```
48
+
49
+ <p>
50
+ <details>
51
+ <summary>More examples</summary>
52
+
53
+ @inline src/testChildApp.spec.tsx
54
+
55
+ </details>
56
+ </p>
package/lib/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export { testChildApp } from './testChildApp';
@@ -0,0 +1,72 @@
1
+ import { createTestApp, testApp } from '@tramvai/test-unit';
2
+ import { provide } from '@tramvai/core';
3
+ import { CHILD_APP_RESOLVE_BASE_URL_TOKEN, CHILD_APP_RESOLUTION_CONFIGS_TOKEN, CHILD_APP_LOADER_TOKEN, CHILD_APP_RESOLVE_CONFIG_TOKEN, CHILD_APP_DI_MANAGER_TOKEN, CHILD_APP_INTERNAL_RENDER_TOKEN } from '@tramvai/child-app-core';
4
+ import { ChildAppModule } from '@tramvai/module-child-app';
5
+
6
+ // mock module federation internal stuff
7
+ // @ts-ignore
8
+ global.__webpack_init_sharing__ = async () => { };
9
+ // @ts-ignore
10
+ global.__webpack_share_scopes__ = { default: { react: {}, 'react-dom': {} } };
11
+ // @ts-ignore
12
+ global.setImmediate = (fn) => fn();
13
+ const testChildApp = async (childApp, options = {}) => {
14
+ var _a, _b;
15
+ const testAppInstance = await createTestApp({
16
+ ...options,
17
+ modules: [...((_a = options.modules) !== null && _a !== void 0 ? _a : []), ChildAppModule],
18
+ providers: [
19
+ ...((_b = options.providers) !== null && _b !== void 0 ? _b : []),
20
+ provide({
21
+ provide: CHILD_APP_RESOLVE_BASE_URL_TOKEN,
22
+ useValue: 'http://test',
23
+ }),
24
+ provide({
25
+ provide: CHILD_APP_RESOLUTION_CONFIGS_TOKEN,
26
+ useValue: [
27
+ {
28
+ name: childApp.name,
29
+ byTag: {
30
+ latest: {
31
+ version: 'test',
32
+ },
33
+ },
34
+ },
35
+ ],
36
+ }),
37
+ provide({
38
+ // replace loader for child-app as we do not need any logic for loading child-app
39
+ provide: CHILD_APP_LOADER_TOKEN,
40
+ useValue: {
41
+ get(config) {
42
+ if (config.name !== childApp.name) {
43
+ throw new Error(`Cannot resolve child-app with name "${config.name}",
44
+ This test wrapper supports only child-app with name "${childApp.name}"`);
45
+ }
46
+ return childApp;
47
+ },
48
+ async init(_) { },
49
+ async load(_) { },
50
+ },
51
+ }),
52
+ ],
53
+ });
54
+ const testAppWrapper = await testApp(testAppInstance.app);
55
+ const appDi = testAppWrapper.app.di;
56
+ const resolveExternalConfig = appDi.get(CHILD_APP_RESOLVE_CONFIG_TOKEN);
57
+ const diManager = appDi.get(CHILD_APP_DI_MANAGER_TOKEN);
58
+ const di = diManager.getChildDi(resolveExternalConfig({ name: childApp.name }));
59
+ if (!di) {
60
+ throw new Error('Cannot resolve child-app di');
61
+ }
62
+ const Component = di.get(CHILD_APP_INTERNAL_RENDER_TOKEN);
63
+ return {
64
+ ...testAppWrapper,
65
+ childApp: {
66
+ di,
67
+ Component,
68
+ },
69
+ };
70
+ };
71
+
72
+ export { testChildApp };
package/lib/index.js ADDED
@@ -0,0 +1,76 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var testUnit = require('@tramvai/test-unit');
6
+ var core = require('@tramvai/core');
7
+ var childAppCore = require('@tramvai/child-app-core');
8
+ var moduleChildApp = require('@tramvai/module-child-app');
9
+
10
+ // mock module federation internal stuff
11
+ // @ts-ignore
12
+ global.__webpack_init_sharing__ = async () => { };
13
+ // @ts-ignore
14
+ global.__webpack_share_scopes__ = { default: { react: {}, 'react-dom': {} } };
15
+ // @ts-ignore
16
+ global.setImmediate = (fn) => fn();
17
+ const testChildApp = async (childApp, options = {}) => {
18
+ var _a, _b;
19
+ const testAppInstance = await testUnit.createTestApp({
20
+ ...options,
21
+ modules: [...((_a = options.modules) !== null && _a !== void 0 ? _a : []), moduleChildApp.ChildAppModule],
22
+ providers: [
23
+ ...((_b = options.providers) !== null && _b !== void 0 ? _b : []),
24
+ core.provide({
25
+ provide: childAppCore.CHILD_APP_RESOLVE_BASE_URL_TOKEN,
26
+ useValue: 'http://test',
27
+ }),
28
+ core.provide({
29
+ provide: childAppCore.CHILD_APP_RESOLUTION_CONFIGS_TOKEN,
30
+ useValue: [
31
+ {
32
+ name: childApp.name,
33
+ byTag: {
34
+ latest: {
35
+ version: 'test',
36
+ },
37
+ },
38
+ },
39
+ ],
40
+ }),
41
+ core.provide({
42
+ // replace loader for child-app as we do not need any logic for loading child-app
43
+ provide: childAppCore.CHILD_APP_LOADER_TOKEN,
44
+ useValue: {
45
+ get(config) {
46
+ if (config.name !== childApp.name) {
47
+ throw new Error(`Cannot resolve child-app with name "${config.name}",
48
+ This test wrapper supports only child-app with name "${childApp.name}"`);
49
+ }
50
+ return childApp;
51
+ },
52
+ async init(_) { },
53
+ async load(_) { },
54
+ },
55
+ }),
56
+ ],
57
+ });
58
+ const testAppWrapper = await testUnit.testApp(testAppInstance.app);
59
+ const appDi = testAppWrapper.app.di;
60
+ const resolveExternalConfig = appDi.get(childAppCore.CHILD_APP_RESOLVE_CONFIG_TOKEN);
61
+ const diManager = appDi.get(childAppCore.CHILD_APP_DI_MANAGER_TOKEN);
62
+ const di = diManager.getChildDi(resolveExternalConfig({ name: childApp.name }));
63
+ if (!di) {
64
+ throw new Error('Cannot resolve child-app di');
65
+ }
66
+ const Component = di.get(childAppCore.CHILD_APP_INTERNAL_RENDER_TOKEN);
67
+ return {
68
+ ...testAppWrapper,
69
+ childApp: {
70
+ di,
71
+ Component,
72
+ },
73
+ };
74
+ };
75
+
76
+ exports.testChildApp = testChildApp;
@@ -0,0 +1,27 @@
1
+ /// <reference types="node" />
2
+ import { createTestApp } from '@tramvai/test-unit';
3
+ import type { ChildApp } from '@tramvai/child-app-core';
4
+ declare type Options = Parameters<typeof createTestApp>[0];
5
+ export declare const testChildApp: (childApp: ChildApp, options?: Options) => Promise<{
6
+ childApp: {
7
+ di: import("@tinkoff/dippy").Container;
8
+ Component: import("react").ComponentType<import("@tramvai/child-app-core").WrapperProps<any>>;
9
+ };
10
+ app: import("@tramvai/core").App;
11
+ request: (path: string, { method }?: {
12
+ method?: "get" | "post" | "put" | undefined;
13
+ } | undefined) => import("supertest").Test;
14
+ render: (path: string, { method, parserOptions, }?: {
15
+ method?: "get" | "post" | "put" | undefined;
16
+ parserOptions?: Partial<import("node-html-parser").Options> | undefined;
17
+ } | undefined) => Promise<{
18
+ initialState: any;
19
+ parsed: import("node-html-parser").HTMLElement;
20
+ head: string;
21
+ body: string;
22
+ application: string;
23
+ }>;
24
+ mocker: import("@tinkoff/mocker").Mocker | null;
25
+ close: () => import("http").Server;
26
+ }>;
27
+ export {};
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@tramvai/test-child-app",
3
+ "version": "1.55.0",
4
+ "description": "",
5
+ "main": "lib/index.js",
6
+ "typings": "lib/index.d.ts",
7
+ "files": [
8
+ "lib"
9
+ ],
10
+ "sideEffects": false,
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git@github.com:Tinkoff/tramvai.git"
14
+ },
15
+ "scripts": {
16
+ "build": "tramvai-build --for-publish",
17
+ "watch": "tsc -w",
18
+ "build-for-publish": "true"
19
+ },
20
+ "dependencies": {
21
+ "@tramvai/child-app-core": "1.55.0",
22
+ "@tramvai/core": "1.55.0",
23
+ "@tramvai/test-unit": "1.55.0",
24
+ "tslib": "^2.0.3"
25
+ },
26
+ "devDependencies": {
27
+ "@tramvai/react": "1.55.0",
28
+ "react": "^17.0.2"
29
+ },
30
+ "peerDependencies": {
31
+ "@tramvai/module-child-app": "1.55.0"
32
+ },
33
+ "license": "Apache-2.0",
34
+ "module": "lib/index.es.js"
35
+ }