@equinor/fusion-framework-vitest-plugin-react-app 0.2.0-next.2 → 1.0.0-next.4

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 (38) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/README.md +9 -6
  3. package/dist/esm/app-test.js +1 -0
  4. package/dist/esm/app-test.js.map +1 -1
  5. package/dist/esm/merge-env-config.js +37 -0
  6. package/dist/esm/merge-env-config.js.map +1 -0
  7. package/dist/esm/resolve-app-test-env.js +5 -5
  8. package/dist/esm/scope/resolve-app-scope.js +1 -1
  9. package/dist/esm/scope/resolve-app-scope.js.map +1 -1
  10. package/dist/esm/scope/resolve-fusion.js +15 -4
  11. package/dist/esm/scope/resolve-fusion.js.map +1 -1
  12. package/dist/esm/test-app.js +39 -6
  13. package/dist/esm/test-app.js.map +1 -1
  14. package/dist/esm/test.js +12 -5
  15. package/dist/esm/test.js.map +1 -1
  16. package/dist/esm/version.js +1 -1
  17. package/dist/tsconfig.tsbuildinfo +1 -1
  18. package/dist/types/app-test.d.ts +1 -0
  19. package/dist/types/merge-env-config.d.ts +33 -0
  20. package/dist/types/resolve-app-test-env.d.ts +5 -5
  21. package/dist/types/scope/resolve-fusion.d.ts +13 -3
  22. package/dist/types/test-app.d.ts +44 -11
  23. package/dist/types/test.d.ts +25 -14
  24. package/dist/types/version.d.ts +1 -1
  25. package/docs/advanced.md +76 -12
  26. package/docs/migrating-an-existing-app.md +9 -9
  27. package/docs/module-mocks.md +2 -2
  28. package/package.json +7 -6
  29. package/src/__tests__/merge-env-config.test.ts +67 -0
  30. package/src/app-test.ts +1 -0
  31. package/src/merge-env-config.ts +58 -0
  32. package/src/resolve-app-test-env.ts +5 -5
  33. package/src/scope/resolve-app-scope.ts +1 -1
  34. package/src/scope/resolve-fusion.ts +21 -10
  35. package/src/test-app.tsx +49 -7
  36. package/src/test.tsx +12 -5
  37. package/src/version.ts +1 -1
  38. package/tsconfig.json +1 -0
@@ -0,0 +1,67 @@
1
+ import { AppConfig } from '@equinor/fusion-framework-module-app';
2
+ import type { AppEnv } from '@equinor/fusion-framework-app';
3
+ import { describe, expect, it } from 'vitest';
4
+
5
+ import { mergeEnvConfig } from '../merge-env-config.js';
6
+
7
+ describe('mergeEnvConfig', () => {
8
+ const baseEnv: AppEnv = {
9
+ manifest: {
10
+ appKey: 'test-app',
11
+ displayName: 'Test App',
12
+ description: 'A test application',
13
+ type: 'standalone',
14
+ },
15
+ config: new AppConfig({
16
+ environment: { foo: 'bar' },
17
+ endpoints: { api: { url: 'https://api.example.com', scopes: ['api.read'] } },
18
+ }),
19
+ };
20
+
21
+ it('adds a new endpoint without dropping existing ones', () => {
22
+ const merged = mergeEnvConfig(baseEnv, {
23
+ endpoints: { 'cpr-api': { url: 'https://cpr.example.com' } },
24
+ });
25
+
26
+ expect(merged.config?.endpoints).toEqual({
27
+ api: { url: 'https://api.example.com', scopes: ['api.read'] },
28
+ 'cpr-api': { url: 'https://cpr.example.com', scopes: [] },
29
+ });
30
+ });
31
+
32
+ it('overrides one field of an existing endpoint, keeping the rest', () => {
33
+ const merged = mergeEnvConfig(baseEnv, {
34
+ endpoints: { api: { url: 'https://fake.example.com' } },
35
+ });
36
+
37
+ expect(merged.config?.endpoints.api).toEqual({
38
+ url: 'https://fake.example.com',
39
+ scopes: ['api.read'],
40
+ });
41
+ });
42
+
43
+ it('merges environment overrides over the existing environment', () => {
44
+ const merged = mergeEnvConfig(baseEnv, { environment: { baz: 'qux' } });
45
+
46
+ expect(merged.config?.environment).toEqual({ foo: 'bar', baz: 'qux' });
47
+ });
48
+
49
+ it('leaves the original env and its config untouched', () => {
50
+ mergeEnvConfig(baseEnv, { endpoints: { api: { url: 'https://fake.example.com' } } });
51
+
52
+ expect(baseEnv.config?.endpoints.api).toEqual({
53
+ url: 'https://api.example.com',
54
+ scopes: ['api.read'],
55
+ });
56
+ });
57
+
58
+ it('handles an env with no existing config', () => {
59
+ const env: AppEnv = { manifest: baseEnv.manifest };
60
+ const merged = mergeEnvConfig(env, { endpoints: { api: { url: 'https://api.example.com' } } });
61
+
62
+ expect(merged.config?.endpoints).toEqual({
63
+ api: { url: 'https://api.example.com', scopes: [] },
64
+ });
65
+ expect(merged.config?.environment).toEqual({});
66
+ });
67
+ });
package/src/app-test.ts CHANGED
@@ -9,6 +9,7 @@ export {
9
9
  type RenderAppComponentResult,
10
10
  } from './render-app-component';
11
11
  export { testApp } from './test-app';
12
+ export { mergeEnvConfig, type MergeEnvConfigOverrides } from './merge-env-config.js';
12
13
  export type { AppMockConfigureFn } from '@equinor/fusion-framework-app/mock';
13
14
 
14
15
  // `test`/`render` import virtual modules only served once `appTestVitePlugin`
@@ -0,0 +1,58 @@
1
+ import { AppConfig } from '@equinor/fusion-framework-module-app';
2
+ import type { ConfigEnvironment } from '@equinor/fusion-framework-module-app';
3
+ import type { AppEnv } from '@equinor/fusion-framework-app';
4
+
5
+ type EndpointOverride = Partial<NonNullable<AppEnv['config']>['endpoints'][string]>;
6
+
7
+ /**
8
+ * Overrides accepted by {@link mergeEnvConfig}.
9
+ */
10
+ export type MergeEnvConfigOverrides<TConfig extends ConfigEnvironment = ConfigEnvironment> = {
11
+ environment?: Partial<TConfig>;
12
+ endpoints?: Record<string, EndpointOverride>;
13
+ };
14
+
15
+ /**
16
+ * Merges `environment`/`endpoints` overrides into an `AppEnv`'s `config`.
17
+ *
18
+ * @remarks
19
+ * `AppConfig` stores both behind private fields exposed only through getters, so
20
+ * `{ ...env.config, endpoints: {...} }` silently drops everything it doesn't explicitly
21
+ * restate — a plain object spread copies no own enumerable properties off an `AppConfig`
22
+ * instance. Reach for this instead of hand-rolling that merge in a test fixture.
23
+ *
24
+ * @template TEnv - The `AppEnv` shape being merged into.
25
+ * @param env - The `AppEnv` to merge overrides into; left untouched, `config` may be omitted.
26
+ * @param overrides - Partial `environment`/`endpoints` values, merged over any existing config.
27
+ * @returns A new `AppEnv` with a new `AppConfig` reflecting the merge.
28
+ * @example
29
+ * ```ts
30
+ * const test = testApp.extend('appEnv', ({ appEnv }) =>
31
+ * mergeEnvConfig(appEnv, { endpoints: { 'cpr-api': { url: backendBaseUrl } } }),
32
+ * );
33
+ * ```
34
+ */
35
+ export function mergeEnvConfig<TEnv extends AppEnv = AppEnv>(
36
+ env: TEnv,
37
+ overrides: MergeEnvConfigOverrides<
38
+ TEnv['config'] extends AppConfig<infer TConfig> ? TConfig : ConfigEnvironment
39
+ >,
40
+ ): TEnv {
41
+ // each overridden endpoint keeps any field the caller didn't explicitly override
42
+ const endpoints = Object.entries(overrides.endpoints ?? {}).reduce(
43
+ // defaults, then any existing endpoint, then the override (override wins)
44
+ (acc, [key, override]) =>
45
+ Object.assign(acc, { [key]: Object.assign({ url: '', scopes: [] }, acc[key], override) }),
46
+ { ...env.config?.endpoints },
47
+ );
48
+ // caller-supplied environment values win over the existing ones
49
+ return {
50
+ ...env,
51
+ config: new AppConfig({
52
+ environment: { ...env.config?.environment, ...overrides.environment },
53
+ endpoints,
54
+ }),
55
+ };
56
+ }
57
+
58
+ export default mergeEnvConfig;
@@ -46,11 +46,11 @@ export type ResolveAppTestEnvOptions = {
46
46
  * back to an empty config if none exists.
47
47
  *
48
48
  * @remarks
49
- * Intended to seed `@equinor/fusion-framework-vitest-plugin-react-app/test`'s `testApp` `env` fixture, so
49
+ * Intended to seed `@equinor/fusion-framework-vitest-plugin-react-app/test`'s `testApp` `appEnv` fixture, so
50
50
  * a test suite exercises the application's real manifest/config instead of a hand-maintained
51
51
  * duplicate. Anything a specific test still needs faked (a missing endpoint, a different
52
- * `appKey`) can be layered on top with `testApp.extend('env', ...)` or a per-test
53
- * `test.override('env', ...)`.
52
+ * `appKey`) can be layered on top with `testApp.extend('appEnv', ...)` or a per-test
53
+ * `test.override('appEnv', ...)`.
54
54
  *
55
55
  * @param options - Resolution options; `entrypoint` defaults to the current working directory.
56
56
  * @returns The resolved application manifest and config.
@@ -63,8 +63,8 @@ export type ResolveAppTestEnvOptions = {
63
63
  * import { configure } from '../config';
64
64
  *
65
65
  * const test = testApp
66
- * .extend('env', { injected: true }, () => resolveAppTestEnv())
67
- * .extend('configure', { injected: true }, () => configure);
66
+ * .extend('appEnv', { injected: true }, () => resolveAppTestEnv())
67
+ * .extend('configureApp', { injected: true }, () => configure);
68
68
  * ```
69
69
  */
70
70
  export const resolveAppTestEnv = async (
@@ -40,7 +40,7 @@ export async function resolveAppScope<
40
40
  }): Promise<AppScope<TModules>> {
41
41
  const { configure, env, fusion: providedFusion } = options ?? {};
42
42
  const resolvedEnv = env ?? (defaultAppEnv as TEnv);
43
- const framework = await resolveFusion(resolvedEnv, providedFusion);
43
+ const framework = await resolveFusion({ env: resolvedEnv, fusion: providedFusion });
44
44
  const app = await mockAppModules(configure, resolvedEnv, framework);
45
45
  return { framework, app };
46
46
  }
@@ -1,8 +1,10 @@
1
1
  import type { AppEnv } from '@equinor/fusion-framework-app';
2
2
  import type { Fusion } from '@equinor/fusion-framework';
3
- import { mockFramework } from '@equinor/fusion-framework/mock';
3
+ import { mockFramework, type FrameworkMockConfigureFn } from '@equinor/fusion-framework/mock';
4
4
  import { enableAppManifestMock } from '@equinor/fusion-framework-app/mock';
5
5
  import type { AppModule } from '@equinor/fusion-framework-module-app';
6
+ import { enableNavigation, createHistory } from '@equinor/fusion-framework-module-navigation';
7
+ import type { NavigationModule } from '@equinor/fusion-framework-module-navigation';
6
8
 
7
9
  import { defaultAppEnv } from './default-app-env';
8
10
 
@@ -12,18 +14,27 @@ import { defaultAppEnv } from './default-app-env';
12
14
  * given.
13
15
  *
14
16
  * @template TEnv - The application environment descriptor.
15
- * @param env - The application environment; defaults to {@link defaultAppEnv}.
16
- * @param fusion - An already-built parent Fusion instance to reuse instead.
17
+ * @param options - `env` seeds the built-in app manifest mock; navigation defaults to real
18
+ * browser history, matching Browser Mode. `configure` runs afterwards on the same
19
+ * configurator — e.g. call `enableNavigation` again to override the history, or register
20
+ * extra modules and service discovery entries. `fusion`, an already-built parent instance,
21
+ * is reused as-is and skips `configure` entirely.
17
22
  * @returns The given `fusion`, or a fresh mocked parent Fusion instance.
18
23
  */
19
- export async function resolveFusion<TEnv extends AppEnv = AppEnv>(
20
- env?: TEnv,
21
- fusion?: Fusion,
22
- ): Promise<Fusion> {
24
+ export async function resolveFusion<TEnv extends AppEnv = AppEnv>(options?: {
25
+ env?: TEnv;
26
+ fusion?: Fusion;
27
+ configure?: FrameworkMockConfigureFn<[AppModule, NavigationModule]>;
28
+ }): Promise<Fusion> {
29
+ const { env, fusion, configure } = options ?? {};
23
30
  return (
24
31
  fusion ??
25
- mockFramework<[AppModule]>((configurator) =>
26
- enableAppManifestMock(configurator, env ?? (defaultAppEnv as TEnv)),
27
- )
32
+ mockFramework<[AppModule, NavigationModule]>(async (configurator) => {
33
+ enableAppManifestMock(configurator, env ?? (defaultAppEnv as TEnv));
34
+ enableNavigation(configurator, {
35
+ configure: (config) => config.setHistory(createHistory('browser')),
36
+ });
37
+ await configure?.(configurator);
38
+ })
28
39
  );
29
40
  }
package/src/test-app.tsx CHANGED
@@ -6,6 +6,9 @@ import type { RenderOptions, RenderHookOptions } from 'vitest-browser-react';
6
6
  import { mockAppModules } from '@equinor/fusion-framework-app/mock';
7
7
  import type { AppMockConfigureFn } from '@equinor/fusion-framework-app/mock';
8
8
  import type { AppEnv } from '@equinor/fusion-framework-app';
9
+ import type { AppModule } from '@equinor/fusion-framework-module-app';
10
+ import type { NavigationModule } from '@equinor/fusion-framework-module-navigation';
11
+ import type { FrameworkMockConfigureFn } from '@equinor/fusion-framework/mock';
9
12
 
10
13
  import { defaultAppEnv, resolveFusion, createAppScopeWrapper } from './scope';
11
14
 
@@ -14,7 +17,7 @@ import { defaultAppEnv, resolveFusion, createAppScopeWrapper } from './scope';
14
17
  *
15
18
  * @remarks
16
19
  * An alternative to {@link renderAppComponent}/{@link renderAppHook} for a test file whose
17
- * cases share seeded fixture defaults: `env`/`configure` become suite-level concerns,
20
+ * cases share seeded fixture defaults: `appEnv`/`configureApp` become suite-level concerns,
18
21
  * overridden once per file (or per `describe` block) with `testApp.extend(...)`, rather than
19
22
  * an options object repeated on every call. `fusion`/`app` are still instantiated fresh per
20
23
  * test — only the seeded defaults are shared, not state between tests. They also resolve
@@ -25,6 +28,19 @@ import { defaultAppEnv, resolveFusion, createAppScopeWrapper } from './scope';
25
28
  * one-off test whose configuration is not shared by the rest of the file; reach for
26
29
  * `testApp` when several cases in a file share one set of seeded fixture defaults.
27
30
  *
31
+ * @remarks `configureApp`/`configureFusion` default to `undefined` here
32
+ * Unlike `@equinor/fusion-framework-vitest-plugin-react-app/test`'s `test`, this `testApp` does
33
+ * not resolve the app's real `src/config.ts` — it requires `appTestVitePlugin`'s Vite virtual
34
+ * modules to load that file as live code, which `testApp` (no Vite dependency) cannot do. Extend
35
+ * `test` from `/test` instead when a suite needs to compose with the app's real configuration.
36
+ *
37
+ * @remarks Overriding `fusion` bypasses `configureFusion`
38
+ * `fusion` and `configureFusion` are not independent: `fusion`'s default resolver is what
39
+ * calls `configureFusion`. `.override('fusion', ...)` replaces that resolver outright, so a
40
+ * `configureFusion` override on the same test/suite is silently never called. Use
41
+ * `configureFusion` to extend the base framework mock; use `fusion` only to replace it
42
+ * entirely (e.g. with a fully custom or non-mocked instance).
43
+ *
28
44
  * @example
29
45
  * ```tsx
30
46
  * testApp('resolves current context', async ({ app, render }) => {
@@ -36,7 +52,7 @@ import { defaultAppEnv, resolveFusion, createAppScopeWrapper } from './scope';
36
52
  * @example Seed a module for every test in a suite
37
53
  * ```tsx
38
54
  * describe('with a seeded context module', () => {
39
- * const test = testApp.extend('configure', { injected: true }, () =>
55
+ * const test = testApp.extend('configureApp', { injected: true }, () =>
40
56
  * (configurator) => enableContextMock(configurator, (mock) => mock.setCurrentContext(projectA)),
41
57
  * );
42
58
  *
@@ -46,15 +62,41 @@ import { defaultAppEnv, resolveFusion, createAppScopeWrapper } from './scope';
46
62
  * });
47
63
  * });
48
64
  * ```
65
+ *
66
+ * @example Extend the parent framework mock with an application module
67
+ * ```tsx
68
+ * const test = testApp.extend('configureFusion', { injected: true }, () =>
69
+ * (configurator) => {
70
+ * enableFeatureFlagMock(configurator);
71
+ * configurator.serviceDiscovery.addServices([
72
+ * { key: 'people', uri: baseUrl('people') },
73
+ * { key: 'context', uri: baseUrl('context') },
74
+ * ]);
75
+ * },
76
+ * );
77
+ * ```
49
78
  */
50
79
  export const testApp = baseTest
51
- .extend('env', { injected: true }, defaultAppEnv)
80
+ .extend('appEnv', { injected: true }, defaultAppEnv)
52
81
  // `test.extend`'s plain-`value` overload rejects function types (ambiguous with the
53
82
  // resolver-`fn` overload), so a function-typed fixture default must go through `fn` instead.
54
- .extend('configure', { injected: true }, () => undefined as AppMockConfigureFn | undefined)
55
- .extend('fusion', async ({ env }) => resolveFusion(env))
56
- .extend('app', async ({ configure, env, fusion }) =>
57
- mockAppModules<unknown, AppEnv>(configure, env as AppEnv, fusion),
83
+ .extend('configureApp', { injected: true }, () => undefined as AppMockConfigureFn | undefined)
84
+ // Runs after the built-in app manifest/navigation setup, so a test can register extra
85
+ // framework modules, service discovery entries, or call `enableNavigation` again to
86
+ // override the history, without reimplementing the base setup.
87
+ .extend(
88
+ 'configureFusion',
89
+ { injected: true },
90
+ () => undefined as FrameworkMockConfigureFn<[AppModule, NavigationModule]> | undefined,
91
+ )
92
+ // IMPORTANT: `.override('fusion', ...)` replaces this resolver entirely, so `configureFusion`
93
+ // is never called — reach for `configureFusion` to extend the base mock, `fusion` only to
94
+ // replace it outright (e.g. with a fully custom or non-mocked instance).
95
+ .extend('fusion', async ({ appEnv, configureFusion }) =>
96
+ resolveFusion({ env: appEnv, configure: configureFusion }),
97
+ )
98
+ .extend('app', async ({ configureApp, appEnv, fusion }) =>
99
+ mockAppModules<unknown, AppEnv>(configureApp, appEnv as AppEnv, fusion),
58
100
  )
59
101
  .extend('render', ({ fusion, app }) => {
60
102
  const wrapper = createAppScopeWrapper({ framework: fusion, app });
package/src/test.tsx CHANGED
@@ -3,7 +3,7 @@ import { testApp as baseTestApp } from './test-app';
3
3
  // resolved at test-time by `appTestVitePlugin` (@equinor/fusion-framework-vitest-plugin-react-app);
4
4
  // see virtual-modules.d.ts for the ambient module declarations
5
5
  import { manifest, config } from 'virtual:fusion-app-test-env';
6
- import { configure } from 'virtual:fusion-app-test-configure';
6
+ import { configure as configureApp } from 'virtual:fusion-app-test-configure';
7
7
 
8
8
  /**
9
9
  * `vitest`'s `test`, pre-seeded with the application's own manifest, config, and
@@ -14,8 +14,15 @@ import { configure } from 'virtual:fusion-app-test-configure';
14
14
  * your `vitest.config.ts` `plugins`, which serves the virtual modules backing this fixture.
15
15
  * Running the same test file without the plugin registered fails to resolve those imports.
16
16
  *
17
- * Per-test mocking still works exactly like the base `testApp`: `.extend('configure', ...)` or
18
- * a per-case `test.override('env', ...)` layers on top of the resolved values.
17
+ * Per-test mocking still works exactly like the base `testApp`: `.extend('configureApp', ...)` or
18
+ * a per-case `test.override('appEnv', ...)` layers on top of the resolved values.
19
+ *
20
+ * @remarks `.override('configureApp', ...)` replaces the app's real `configure`
21
+ * This fixture's default value *is* the app's real `src/config.ts` `configure` export.
22
+ * `.override('configureApp', ...)` replaces that default outright, so an override that doesn't
23
+ * itself call the real `configure(configurator, args)` skips the app's production module setup
24
+ * entirely, rather than composing with it. See [Advanced usage](../docs/advanced.md) for the
25
+ * compose-safely pattern.
19
26
  *
20
27
  * @example
21
28
  * ```tsx
@@ -29,7 +36,7 @@ import { configure } from 'virtual:fusion-app-test-configure';
29
36
  * ```
30
37
  */
31
38
  export const test = baseTestApp
32
- .extend('env', { injected: true }, { manifest, config })
33
- .extend('configure', { injected: true }, () => configure);
39
+ .extend('appEnv', { injected: true }, { manifest, config })
40
+ .extend('configureApp', { injected: true }, () => configureApp);
34
41
 
35
42
  export default test;
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  // Generated by genversion.
2
- export const version = '0.2.0-next.2';
2
+ export const version = '1.0.0-next.4';
package/tsconfig.json CHANGED
@@ -15,6 +15,7 @@
15
15
  { "path": "../../app" },
16
16
  { "path": "../../modules/module" },
17
17
  { "path": "../../modules/app" },
18
+ { "path": "../../modules/navigation" },
18
19
  { "path": "../../react/framework" },
19
20
  { "path": "../../react/modules/module" },
20
21
  { "path": "../../utils/imports" }