@servicetitan/web-components 40.2.2 → 40.3.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.
Files changed (76) hide show
  1. package/dist/loader.js +4 -1
  2. package/dist/loader.js.map +1 -1
  3. package/dist/patches/kendo-scrollbar-width/get-mfe-maybe-uses-kendo-grid.d.ts +3 -0
  4. package/dist/patches/kendo-scrollbar-width/get-mfe-maybe-uses-kendo-grid.d.ts.map +1 -0
  5. package/dist/patches/kendo-scrollbar-width/get-mfe-maybe-uses-kendo-grid.js +12 -0
  6. package/dist/patches/kendo-scrollbar-width/get-mfe-maybe-uses-kendo-grid.js.map +1 -0
  7. package/dist/patches/kendo-scrollbar-width/index.d.ts +3 -0
  8. package/dist/patches/kendo-scrollbar-width/index.d.ts.map +1 -0
  9. package/dist/patches/kendo-scrollbar-width/index.js +4 -0
  10. package/dist/patches/kendo-scrollbar-width/index.js.map +1 -0
  11. package/dist/patches/kendo-scrollbar-width/set-kendo-scrollbar-width.d.ts +2 -0
  12. package/dist/patches/kendo-scrollbar-width/set-kendo-scrollbar-width.d.ts.map +1 -0
  13. package/dist/patches/kendo-scrollbar-width/set-kendo-scrollbar-width.js +26 -0
  14. package/dist/patches/kendo-scrollbar-width/set-kendo-scrollbar-width.js.map +1 -0
  15. package/dist/register.js +2 -2
  16. package/dist/register.js.map +1 -1
  17. package/dist/use-prefetch.d.ts +2 -3
  18. package/dist/use-prefetch.d.ts.map +1 -1
  19. package/dist/use-prefetch.js +14 -7
  20. package/dist/use-prefetch.js.map +1 -1
  21. package/dist/utils/get-bundle-info.d.ts +1 -0
  22. package/dist/utils/get-bundle-info.d.ts.map +1 -1
  23. package/dist/utils/get-bundle-info.js +2 -0
  24. package/dist/utils/get-bundle-info.js.map +1 -1
  25. package/dist/utils/web-component.d.ts.map +1 -1
  26. package/dist/utils/web-component.js +13 -2
  27. package/dist/utils/web-component.js.map +1 -1
  28. package/package.json +18 -14
  29. package/src/loader.tsx +5 -1
  30. package/src/patches/kendo-scrollbar-width/get-mfe-maybe-uses-kendo-grid.ts +11 -0
  31. package/src/patches/kendo-scrollbar-width/index.ts +2 -0
  32. package/src/patches/kendo-scrollbar-width/set-kendo-scrollbar-width.ts +27 -0
  33. package/src/register.tsx +1 -1
  34. package/src/use-prefetch.ts +36 -32
  35. package/src/utils/get-bundle-info.ts +2 -0
  36. package/src/utils/web-component.tsx +20 -3
  37. package/dist/contexts/tests/mfe-data-context.test.d.ts +0 -2
  38. package/dist/contexts/tests/mfe-data-context.test.d.ts.map +0 -1
  39. package/dist/contexts/tests/mfe-data-context.test.js +0 -60
  40. package/dist/contexts/tests/mfe-data-context.test.js.map +0 -1
  41. package/dist/contexts/tests/mfe-metadata-context.test.d.ts +0 -2
  42. package/dist/contexts/tests/mfe-metadata-context.test.d.ts.map +0 -1
  43. package/dist/contexts/tests/mfe-metadata-context.test.js +0 -41
  44. package/dist/contexts/tests/mfe-metadata-context.test.js.map +0 -1
  45. package/src/__mocks__/expect-styles-in-dom.ts +0 -11
  46. package/src/__mocks__/index.ts +0 -3
  47. package/src/__mocks__/register-anvil2-harness.tsx +0 -49
  48. package/src/__mocks__/register-component.tsx +0 -20
  49. package/src/__mocks__/test-component.tsx +0 -41
  50. package/src/__tests__/common.test.ts +0 -204
  51. package/src/__tests__/delayed.test.tsx +0 -49
  52. package/src/__tests__/event-bus-types.test.ts +0 -620
  53. package/src/__tests__/event-bus.test.ts +0 -240
  54. package/src/__tests__/get-value-for-environment.test.ts +0 -95
  55. package/src/__tests__/globals.test.ts +0 -16
  56. package/src/__tests__/headless-loader.test.tsx +0 -123
  57. package/src/__tests__/history-manager.test.ts +0 -111
  58. package/src/__tests__/loader.test.tsx +0 -1178
  59. package/src/__tests__/register-anvil2-optional.test.tsx +0 -32
  60. package/src/__tests__/register-anvil2.test.tsx +0 -80
  61. package/src/__tests__/register-headless.test.tsx +0 -97
  62. package/src/__tests__/register-legacy-root.test.tsx +0 -38
  63. package/src/__tests__/register.test.tsx +0 -247
  64. package/src/__tests__/use-prefetch.test.tsx +0 -235
  65. package/src/contexts/tests/mfe-data-context.test.tsx +0 -64
  66. package/src/contexts/tests/mfe-metadata-context.test.tsx +0 -34
  67. package/src/css-injector/__tests__/mfe-style-registry.test.ts +0 -163
  68. package/src/css-injector/__tests__/with-css-injector.test.tsx +0 -128
  69. package/src/utils/__tests__/get-cached-metadata.test.ts +0 -94
  70. package/src/utils/__tests__/get-mfe-has-compatible-launchdarkly-version.test.ts +0 -54
  71. package/src/utils/__tests__/get-mfe-supports-rerendering.test.ts +0 -99
  72. package/src/utils/__tests__/get-mfe-supports-versioned-ld-client.test.ts +0 -38
  73. package/src/utils/__tests__/is-versioned-url.test.ts +0 -18
  74. package/src/utils/__tests__/load-scripts.test.ts +0 -171
  75. package/src/utils/__tests__/supports-prefetch.test.ts +0 -27
  76. package/src/utils/__tests__/ttl-cache.test.ts +0 -98
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@servicetitan/web-components",
3
- "version": "40.2.2",
3
+ "version": "40.3.0",
4
4
  "description": "Framework to build, register, and load Microfrontends into host applications",
5
5
  "homepage": "https://docs.st.dev/docs/frontend/uikit/web-components",
6
6
  "repository": {
@@ -28,27 +28,31 @@
28
28
  "sideEffects": false,
29
29
  "files": [
30
30
  "dist",
31
- "src"
31
+ "src",
32
+ "!**/__tests__",
33
+ "!**/__mocks__",
34
+ "!**/__snapshots__",
35
+ "!**/*.test.*"
32
36
  ],
33
37
  "dependencies": {
34
- "@servicetitan/error-boundary": "40.2.2",
35
- "@servicetitan/startup-utils": "40.2.2",
38
+ "@servicetitan/error-boundary": "40.3.0",
39
+ "@servicetitan/startup-utils": "40.3.0",
36
40
  "mitt": "~3.0.1",
37
- "semver": "~7.8.5"
41
+ "semver": "^7.8.6"
38
42
  },
39
43
  "devDependencies": {
40
- "@servicetitan/anvil2": "^3.16.1",
41
- "@servicetitan/design-system": "~14.5.11",
42
- "@servicetitan/launchdarkly-service": "40.2.2",
43
- "@servicetitan/log-service": "40.2.2",
44
- "@servicetitan/react-ioc": "40.2.2",
44
+ "@servicetitan/anvil2": "^3.19.1",
45
+ "@servicetitan/design-system": "~14.5.12",
46
+ "@servicetitan/launchdarkly-service": "40.3.0",
47
+ "@servicetitan/log-service": "40.3.0",
48
+ "@servicetitan/react-ioc": "40.3.0",
45
49
  "@testing-library/dom": "^10.4.2",
46
50
  "@testing-library/jest-dom": "^7.0.1",
47
51
  "@testing-library/react": "^16.3.3",
48
52
  "@types/history": "~4.7.10",
49
53
  "@types/react": "~18.3.3",
50
54
  "@types/react-dom": "~18.3.0",
51
- "@types/semver": "~7.7.1",
55
+ "@types/semver": "^7.8.0",
52
56
  "history": "~4.10.1",
53
57
  "react": "~18.3.1",
54
58
  "react-dom": "~18.3.1",
@@ -57,9 +61,9 @@
57
61
  "peerDependencies": {
58
62
  "@servicetitan/anvil2": ">=1.5.0",
59
63
  "@servicetitan/design-system": ">=14.5.3",
60
- "@servicetitan/launchdarkly-service": "40.2.2",
61
- "@servicetitan/log-service": "40.2.2",
62
- "@servicetitan/react-ioc": "40.2.2",
64
+ "@servicetitan/launchdarkly-service": "40.3.0",
65
+ "@servicetitan/log-service": "40.3.0",
66
+ "@servicetitan/react-ioc": "40.3.0",
63
67
  "history": "~4.10.1",
64
68
  "react": ">=17.0.2",
65
69
  "react-dom": ">=17.0.2"
package/src/loader.tsx CHANGED
@@ -171,6 +171,10 @@ function lazyWebComponent(props: LazyLoaderProps) {
171
171
  } = props;
172
172
 
173
173
  return lazy<(_: LoaderDataProps) => JSX.Element | null>(async () => {
174
+ if (!src) {
175
+ throw new Error('Loader src is empty');
176
+ }
177
+
174
178
  const bundleInfo = await getBundleInfo({
175
179
  cache,
176
180
  mainPackageUrl: src,
@@ -223,7 +227,7 @@ function getFallback(
223
227
  }
224
228
 
225
229
  function isLocalHost(source: string) {
226
- return ['localhost', '127.0.0.1'].some(host => source.indexOf(host) >= 0);
230
+ return !!source && ['localhost', '127.0.0.1'].some(host => source.indexOf(host) >= 0);
227
231
  }
228
232
 
229
233
  function ensureLDService(
@@ -0,0 +1,11 @@
1
+ import type { Metadata } from '../../types';
2
+
3
+ /*
4
+ * Design system's Table bundles the Kendo grid as its own dependency, so MFEs that use it
5
+ * don't list the grid themselves.
6
+ */
7
+ const KENDO_GRID_PACKAGES = ['@progress/kendo-react-grid', '@servicetitan/design-system'];
8
+
9
+ export function getMFEMaybeUsesKendoGrid(dependencies: Metadata['dependencies']) {
10
+ return KENDO_GRID_PACKAGES.some(name => !!dependencies?.[name]);
11
+ }
@@ -0,0 +1,2 @@
1
+ export * from './get-mfe-maybe-uses-kendo-grid';
2
+ export * from './set-kendo-scrollbar-width';
@@ -0,0 +1,27 @@
1
+ const KENDO_SCROLLBAR_WIDTH = '--kendo-scrollbar-width';
2
+
3
+ /*
4
+ * Kendo measures the scrollbar on document.body, where host scrollbar styles apply but MFE
5
+ * styles don't. Measure inside the MFE's shadow root instead, and set the result on the MFE
6
+ * element and its portals so it overrides the value they inherit from the body.
7
+ */
8
+ export function setKendoScrollbarWidth(element: HTMLElement, elementName: string) {
9
+ const root = element.shadowRoot;
10
+ if (!root) {
11
+ return;
12
+ }
13
+
14
+ const probe = document.createElement('div');
15
+ probe.style.cssText = 'overflow:scroll;overflow-x:hidden;zoom:1;clear:both;display:block';
16
+ probe.innerHTML = '&nbsp;';
17
+ root.appendChild(probe);
18
+ const width = `${probe.offsetWidth - probe.scrollWidth}px`;
19
+ probe.remove();
20
+
21
+ const portals = document.querySelectorAll<HTMLElement>(
22
+ `[data-mfe-portal-name="${elementName}"]`
23
+ );
24
+ for (const target of [element, ...portals]) {
25
+ target.style.setProperty(KENDO_SCROLLBAR_WIDTH, width);
26
+ }
27
+ }
package/src/register.tsx CHANGED
@@ -128,7 +128,7 @@ export function register(Component: ComponentType, light: boolean, options?: Reg
128
128
  }
129
129
 
130
130
  this.view?.unmount();
131
- document.body?.removeChild(this.portal!.host);
131
+ this.portal!.host.remove();
132
132
  this.root = undefined;
133
133
  this.portal = undefined;
134
134
  this.portalAnvilContainer = undefined;
@@ -1,13 +1,14 @@
1
1
  import { Log } from '@servicetitan/log-service';
2
2
  import { useOptionalDependencies } from '@servicetitan/react-ioc';
3
+ import { useCallback, useMemo } from 'react';
3
4
  import { EXPOSED_DEPENDENCIES_TOKEN, EXPOSED_INSTANCE_DEPENDENCIES_TOKEN } from './common';
5
+ import { EXPOSED_DEPENDENCIES, EXPOSED_INSTANCE_DEPENDENCIES } from './globals';
4
6
  import { LoaderProps } from './loader';
5
7
  import { getBundleInfo, supportsPrefetch } from './utils';
6
8
 
7
9
  /**
8
- * Prefetch and cache bundle info for a given MFE, so that Loader can load it faster later.
9
- * @param src - The main package URL for the MFE
10
- * @returns Promise resolving to the BundleInfo
10
+ * Returns a stable `prefetch` function that prefetches and caches an MFE's bundle,
11
+ * so that Loader can load it faster later.
11
12
  */
12
13
  export function usePrefetch() {
13
14
  const [logService, exposedDependencies = EXPOSED_DEPENDENCIES] = useOptionalDependencies(
@@ -17,39 +18,42 @@ export function usePrefetch() {
17
18
  const [exposedInstanceDependencies = EXPOSED_INSTANCE_DEPENDENCIES] = useOptionalDependencies(
18
19
  EXPOSED_INSTANCE_DEPENDENCIES_TOKEN
19
20
  );
20
- const prefetch = async (src: string, { cache = -1 }: Pick<LoaderProps<any>, 'cache'> = {}) => {
21
- try {
22
- const bundleInfo = await getBundleInfo({
23
- exposedDependencies,
24
- exposedInstanceDependencies,
25
- mainPackageUrl: src,
26
- cache,
27
- retries: 0,
28
- });
21
+ const prefetch = useCallback(
22
+ async (src: string, { cache = -1 }: Pick<LoaderProps<any>, 'cache'> = {}) => {
23
+ try {
24
+ const bundleInfo = await getBundleInfo({
25
+ exposedDependencies,
26
+ exposedInstanceDependencies,
27
+ mainPackageUrl: src,
28
+ cache,
29
+ retries: 0,
30
+ });
29
31
 
30
- if (bundleInfo.cacheHit) {
31
- return;
32
- }
32
+ if (bundleInfo.cacheHit) {
33
+ return;
34
+ }
33
35
 
34
- const { css, js } = bundleInfo.urls;
35
- if (supportsPrefetch()) {
36
- addLinks([
37
- ...css.map(href => ({ href, as: 'style' })),
38
- ...js.map(href => ({ href, as: 'script' })),
39
- ]);
40
- } else {
41
- await Promise.all([...css, ...js].map(url => fetch(url, { priority: 'low' })));
36
+ const { css, js } = bundleInfo.urls;
37
+ if (supportsPrefetch()) {
38
+ addLinks([
39
+ ...css.map(href => ({ href, as: 'style' })),
40
+ ...js.map(href => ({ href, as: 'script' })),
41
+ ]);
42
+ } else {
43
+ await Promise.all([...css, ...js].map(url => fetch(url, { priority: 'low' })));
44
+ }
45
+ } catch (e: any) {
46
+ logService?.warning({
47
+ category: 'Microfrontends.Prefetch',
48
+ message: `Failed to prefetch ${src}`,
49
+ data: e,
50
+ });
42
51
  }
43
- } catch (e: any) {
44
- logService?.warning({
45
- category: 'Microfrontends.Prefetch',
46
- message: `Failed to prefetch ${src}`,
47
- data: e,
48
- });
49
- }
50
- };
52
+ },
53
+ [logService, exposedDependencies, exposedInstanceDependencies]
54
+ );
51
55
 
52
- return { prefetch };
56
+ return useMemo(() => ({ prefetch }), [prefetch]);
53
57
  }
54
58
 
55
59
  interface Link {
@@ -1,6 +1,7 @@
1
1
  import { ExposedDependencies, getVersionMismatches } from '@servicetitan/startup-utils';
2
2
  import semver from 'semver';
3
3
  import { ExposedInstanceDependencies, getJson, GetJsonError } from '../common';
4
+ import { getMFEMaybeUsesKendoGrid } from '../patches/kendo-scrollbar-width';
4
5
  import type { Metadata } from '../types';
5
6
  import { getCacheSetting } from './get-cache-setting';
6
7
  import { getCachedMetadata } from './get-cached-metadata';
@@ -108,6 +109,7 @@ export async function getBundleInfo({
108
109
  exposedInstanceDependencies
109
110
  ),
110
111
  mfeSupportsVersionedLDClient: getMFESupportsVersionedLDClient(bundledWith),
112
+ mfeMaybeUsesKendoGrid: getMFEMaybeUsesKendoGrid(dependencies),
111
113
  mismatch,
112
114
  resolvedBundleType,
113
115
  urls,
@@ -2,12 +2,13 @@ import { type LDService } from '@servicetitan/launchdarkly-service';
2
2
  import { Log } from '@servicetitan/log-service';
3
3
  import { useOptionalDependencies } from '@servicetitan/react-ioc';
4
4
  import { ExposedDependencies } from '@servicetitan/startup-utils';
5
- import { Fragment, useMemo, useState } from 'react';
5
+ import { Fragment, useLayoutEffect, useMemo, useRef, useState } from 'react';
6
6
  import { ExposedInstanceDependencies, IWebComponent } from '../common';
7
7
  import { MFEContextData } from '../contexts/mfe-data-context';
8
8
  import { EventBus } from '../event-bus';
9
9
  import { HistoryManager } from '../history-manager';
10
10
  import { LoaderDataProps } from '../loader';
11
+ import { setKendoScrollbarWidth } from '../patches/kendo-scrollbar-width';
11
12
  import { BundleInfo } from './get-bundle-info';
12
13
  import { getElementName } from './get-element-name';
13
14
 
@@ -23,7 +24,13 @@ interface WebComponentProps {
23
24
 
24
25
  export function webComponent(props: WebComponentProps) {
25
26
  const { basename, bundleInfo, eventBus, exposedDependencies, ldService, logService } = props;
26
- const { WebComponent, resolvedBundleType, mfeSupportsRerendering, version } = bundleInfo;
27
+ const {
28
+ WebComponent,
29
+ resolvedBundleType,
30
+ mfeSupportsRerendering,
31
+ mfeMaybeUsesKendoGrid,
32
+ version,
33
+ } = bundleInfo;
27
34
  const elementName = getElementName(bundleInfo);
28
35
 
29
36
  const exposedInstanceDependencies = ensureLDVersion(
@@ -34,8 +41,10 @@ export function webComponent(props: WebComponentProps) {
34
41
  return ({ LoadingFallback, ...innerProps }: LoaderDataProps) => {
35
42
  const [isLoading, setIsLoading] = useState(true);
36
43
  const [historyManager] = useOptionalDependencies(HistoryManager);
44
+ const elementRef = useRef<(IWebComponent & HTMLElement) | null>(null);
37
45
 
38
- const handleRef = (webComponent: IWebComponent | null) => {
46
+ const handleRef = (webComponent: (IWebComponent & HTMLElement) | null) => {
47
+ elementRef.current = webComponent;
39
48
  webComponent?.provide({
40
49
  ldService,
41
50
  logService,
@@ -60,6 +69,14 @@ export function webComponent(props: WebComponentProps) {
60
69
  * This is only used as a fallback when the MFE's version is older.
61
70
  */
62
71
  const key = mfeSupportsRerendering ? undefined : stringifiedData;
72
+
73
+ // Measure after the element is visible, since a hidden element has no scrollbar.
74
+ useLayoutEffect(() => {
75
+ if (mfeMaybeUsesKendoGrid && !isLoading && elementRef.current) {
76
+ setKendoScrollbarWidth(elementRef.current, elementName);
77
+ }
78
+ }, [isLoading]);
79
+
63
80
  const dataAttrs = useMemo(() => composeDataAttributes(innerProps.data), [innerProps.data]);
64
81
  const styleUrls = getStyleUrls(bundleInfo);
65
82
 
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=mfe-data-context.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"mfe-data-context.test.d.ts","sourceRoot":"","sources":["../../../src/contexts/tests/mfe-data-context.test.tsx"],"names":[],"mappings":""}
@@ -1,60 +0,0 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
2
- import { renderHook } from '@testing-library/react';
3
- import { MFEDataContext, useMFEDataContext } from '../mfe-data-context';
4
- describe(`[web-components] ${useMFEDataContext.name}`, ()=>{
5
- const subject = (wrapper)=>renderHook(()=>useMFEDataContext(), {
6
- wrapper
7
- });
8
- describe('when the MFEDataContext is provided by a parent', ()=>{
9
- const data = {
10
- foo: 'bar'
11
- };
12
- const Wrapper = ({ children })=>{
13
- return /*#__PURE__*/ _jsx(MFEDataContext.Provider, {
14
- value: data,
15
- children: children
16
- });
17
- };
18
- test('returns the context', ()=>{
19
- expect(subject(Wrapper).result.current).toEqual(data);
20
- });
21
- });
22
- describe('when the MFEDataContext is not provided by a parent', ()=>{
23
- const Wrapper = ({ children })=>{
24
- return /*#__PURE__*/ _jsx("div", {
25
- children: children
26
- });
27
- };
28
- test('returns an empty object', ()=>{
29
- expect(subject(Wrapper).result.current).toEqual({});
30
- });
31
- });
32
- describe('type definitions', ()=>{
33
- test('accepts properties typed as JSON', ()=>{
34
- renderHook(()=>useMFEDataContext());
35
- });
36
- test('accepts properties typed as Record<string, JSON>', ()=>{
37
- renderHook(()=>useMFEDataContext());
38
- });
39
- test('accepts primitive properties', ()=>{
40
- renderHook(()=>useMFEDataContext());
41
- });
42
- test('accepts nested objects and arrays', ()=>{
43
- renderHook(()=>useMFEDataContext());
44
- });
45
- test('rejects non-serializable properties', ()=>{
46
- // @ts-expect-error Type '() => void' is not assignable to type 'never'.
47
- renderHook(()=>useMFEDataContext());
48
- // @ts-expect-error Type 'Date' is not assignable to type 'never'.
49
- renderHook(()=>useMFEDataContext());
50
- // @ts-expect-error Type 'Map<string, any>' is not assignable to type 'never'.
51
- renderHook(()=>useMFEDataContext());
52
- // @ts-expect-error Type 'Set<string>' is not assignable to type 'never'.
53
- renderHook(()=>useMFEDataContext());
54
- // @ts-expect-error Type 'symbol' is not assignable to type 'never'.
55
- renderHook(()=>useMFEDataContext());
56
- });
57
- });
58
- });
59
-
60
- //# sourceMappingURL=mfe-data-context.test.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../../src/contexts/tests/mfe-data-context.test.tsx"],"sourcesContent":["import { renderHook } from '@testing-library/react';\nimport { FC, PropsWithChildren } from 'react';\nimport { JSON } from '../../types';\nimport { MFEDataContext, useMFEDataContext } from '../mfe-data-context';\n\ndescribe(`[web-components] ${useMFEDataContext.name}`, () => {\n const subject = (wrapper?: FC) => renderHook(() => useMFEDataContext(), { wrapper });\n\n describe('when the MFEDataContext is provided by a parent', () => {\n const data = { foo: 'bar' };\n const Wrapper: FC<PropsWithChildren> = ({ children }) => {\n return <MFEDataContext.Provider value={data}>{children}</MFEDataContext.Provider>;\n };\n\n test('returns the context', () => {\n expect(subject(Wrapper).result.current).toEqual(data);\n });\n });\n\n describe('when the MFEDataContext is not provided by a parent', () => {\n const Wrapper: FC<PropsWithChildren> = ({ children }) => {\n return <div>{children}</div>;\n };\n\n test('returns an empty object', () => {\n expect(subject(Wrapper).result.current).toEqual({});\n });\n });\n\n describe('type definitions', () => {\n test('accepts properties typed as JSON', () => {\n renderHook(() => useMFEDataContext<{ data: JSON }>());\n });\n\n test('accepts properties typed as Record<string, JSON>', () => {\n renderHook(() => useMFEDataContext<{ metadata: Record<string, JSON> }>());\n });\n\n test('accepts primitive properties', () => {\n renderHook(() => useMFEDataContext<{ name: string; count: number; active: boolean }>());\n });\n\n test('accepts nested objects and arrays', () => {\n renderHook(() => useMFEDataContext<{ nested: { value: number }; list: string[] }>());\n });\n\n test('rejects non-serializable properties', () => {\n // @ts-expect-error Type '() => void' is not assignable to type 'never'.\n renderHook(() => useMFEDataContext<{ callback: () => void }>());\n\n // @ts-expect-error Type 'Date' is not assignable to type 'never'.\n renderHook(() => useMFEDataContext<{ created: Date }>());\n\n // @ts-expect-error Type 'Map<string, any>' is not assignable to type 'never'.\n renderHook(() => useMFEDataContext<{ lookup: Map<string, any> }>());\n\n // @ts-expect-error Type 'Set<string>' is not assignable to type 'never'.\n renderHook(() => useMFEDataContext<{ tags: Set<string> }>());\n\n // @ts-expect-error Type 'symbol' is not assignable to type 'never'.\n renderHook(() => useMFEDataContext<{ id: symbol }>());\n });\n });\n});\n"],"names":["renderHook","MFEDataContext","useMFEDataContext","describe","name","subject","wrapper","data","foo","Wrapper","children","Provider","value","test","expect","result","current","toEqual","div"],"mappings":";AAAA,SAASA,UAAU,QAAQ,yBAAyB;AAGpD,SAASC,cAAc,EAAEC,iBAAiB,QAAQ,sBAAsB;AAExEC,SAAS,CAAC,iBAAiB,EAAED,kBAAkBE,IAAI,EAAE,EAAE;IACnD,MAAMC,UAAU,CAACC,UAAiBN,WAAW,IAAME,qBAAqB;YAAEI;QAAQ;IAElFH,SAAS,mDAAmD;QACxD,MAAMI,OAAO;YAAEC,KAAK;QAAM;QAC1B,MAAMC,UAAiC,CAAC,EAAEC,QAAQ,EAAE;YAChD,qBAAO,KAACT,eAAeU,QAAQ;gBAACC,OAAOL;0BAAOG;;QAClD;QAEAG,KAAK,uBAAuB;YACxBC,OAAOT,QAAQI,SAASM,MAAM,CAACC,OAAO,EAAEC,OAAO,CAACV;QACpD;IACJ;IAEAJ,SAAS,uDAAuD;QAC5D,MAAMM,UAAiC,CAAC,EAAEC,QAAQ,EAAE;YAChD,qBAAO,KAACQ;0BAAKR;;QACjB;QAEAG,KAAK,2BAA2B;YAC5BC,OAAOT,QAAQI,SAASM,MAAM,CAACC,OAAO,EAAEC,OAAO,CAAC,CAAC;QACrD;IACJ;IAEAd,SAAS,oBAAoB;QACzBU,KAAK,oCAAoC;YACrCb,WAAW,IAAME;QACrB;QAEAW,KAAK,oDAAoD;YACrDb,WAAW,IAAME;QACrB;QAEAW,KAAK,gCAAgC;YACjCb,WAAW,IAAME;QACrB;QAEAW,KAAK,qCAAqC;YACtCb,WAAW,IAAME;QACrB;QAEAW,KAAK,uCAAuC;YACxC,wEAAwE;YACxEb,WAAW,IAAME;YAEjB,kEAAkE;YAClEF,WAAW,IAAME;YAEjB,8EAA8E;YAC9EF,WAAW,IAAME;YAEjB,yEAAyE;YACzEF,WAAW,IAAME;YAEjB,oEAAoE;YACpEF,WAAW,IAAME;QACrB;IACJ;AACJ"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=mfe-metadata-context.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"mfe-metadata-context.test.d.ts","sourceRoot":"","sources":["../../../src/contexts/tests/mfe-metadata-context.test.tsx"],"names":[],"mappings":""}
@@ -1,41 +0,0 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
2
- import { renderHook } from '@testing-library/react';
3
- import { MFEMetadataContext, useMFEMetadataContext } from '../mfe-metadata-context';
4
- describe(`[web-components] ${useMFEMetadataContext.name}`, ()=>{
5
- const subject = (wrapper)=>renderHook(()=>useMFEMetadataContext(), {
6
- wrapper
7
- });
8
- describe('when the MFEMetadataContext is provided by a parent', ()=>{
9
- const shadowRoot = document.createElement('div').attachShadow({
10
- mode: 'open'
11
- });
12
- const portalShadowRoot = document.createElement('span').attachShadow({
13
- mode: 'open'
14
- });
15
- const metadata = {
16
- shadowRoot,
17
- portalShadowRoot
18
- };
19
- const Wrapper = ({ children })=>{
20
- return /*#__PURE__*/ _jsx(MFEMetadataContext.Provider, {
21
- value: metadata,
22
- children: children
23
- });
24
- };
25
- test('returns the context', ()=>{
26
- expect(subject(Wrapper).result.current).toEqual(metadata);
27
- });
28
- });
29
- describe('when the MFEMetadataContext is not provided by a parent', ()=>{
30
- const Wrapper = ({ children })=>{
31
- return /*#__PURE__*/ _jsx("div", {
32
- children: children
33
- });
34
- };
35
- test('returns an empty object', ()=>{
36
- expect(subject(Wrapper).result.current).toEqual({});
37
- });
38
- });
39
- });
40
-
41
- //# sourceMappingURL=mfe-metadata-context.test.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../../src/contexts/tests/mfe-metadata-context.test.tsx"],"sourcesContent":["import { renderHook } from '@testing-library/react';\nimport { FC, PropsWithChildren } from 'react';\nimport { MFEMetadataContext, useMFEMetadataContext } from '../mfe-metadata-context';\n\ndescribe(`[web-components] ${useMFEMetadataContext.name}`, () => {\n const subject = (wrapper?: FC) => renderHook(() => useMFEMetadataContext(), { wrapper });\n\n describe('when the MFEMetadataContext is provided by a parent', () => {\n const shadowRoot = document.createElement('div').attachShadow({ mode: 'open' });\n const portalShadowRoot = document.createElement('span').attachShadow({ mode: 'open' });\n const metadata = { shadowRoot, portalShadowRoot };\n const Wrapper: FC<PropsWithChildren> = ({ children }) => {\n return (\n <MFEMetadataContext.Provider value={metadata}>\n {children}\n </MFEMetadataContext.Provider>\n );\n };\n\n test('returns the context', () => {\n expect(subject(Wrapper).result.current).toEqual(metadata);\n });\n });\n\n describe('when the MFEMetadataContext is not provided by a parent', () => {\n const Wrapper: FC<PropsWithChildren> = ({ children }) => {\n return <div>{children}</div>;\n };\n\n test('returns an empty object', () => {\n expect(subject(Wrapper).result.current).toEqual({});\n });\n });\n});\n"],"names":["renderHook","MFEMetadataContext","useMFEMetadataContext","describe","name","subject","wrapper","shadowRoot","document","createElement","attachShadow","mode","portalShadowRoot","metadata","Wrapper","children","Provider","value","test","expect","result","current","toEqual","div"],"mappings":";AAAA,SAASA,UAAU,QAAQ,yBAAyB;AAEpD,SAASC,kBAAkB,EAAEC,qBAAqB,QAAQ,0BAA0B;AAEpFC,SAAS,CAAC,iBAAiB,EAAED,sBAAsBE,IAAI,EAAE,EAAE;IACvD,MAAMC,UAAU,CAACC,UAAiBN,WAAW,IAAME,yBAAyB;YAAEI;QAAQ;IAEtFH,SAAS,uDAAuD;QAC5D,MAAMI,aAAaC,SAASC,aAAa,CAAC,OAAOC,YAAY,CAAC;YAAEC,MAAM;QAAO;QAC7E,MAAMC,mBAAmBJ,SAASC,aAAa,CAAC,QAAQC,YAAY,CAAC;YAAEC,MAAM;QAAO;QACpF,MAAME,WAAW;YAAEN;YAAYK;QAAiB;QAChD,MAAME,UAAiC,CAAC,EAAEC,QAAQ,EAAE;YAChD,qBACI,KAACd,mBAAmBe,QAAQ;gBAACC,OAAOJ;0BAC/BE;;QAGb;QAEAG,KAAK,uBAAuB;YACxBC,OAAOd,QAAQS,SAASM,MAAM,CAACC,OAAO,EAAEC,OAAO,CAACT;QACpD;IACJ;IAEAV,SAAS,2DAA2D;QAChE,MAAMW,UAAiC,CAAC,EAAEC,QAAQ,EAAE;YAChD,qBAAO,KAACQ;0BAAKR;;QACjB;QAEAG,KAAK,2BAA2B;YAC5BC,OAAOd,QAAQS,SAASM,MAAM,CAACC,OAAO,EAAEC,OAAO,CAAC,CAAC;QACrD;IACJ;AACJ"}
@@ -1,11 +0,0 @@
1
- import '@testing-library/jest-dom';
2
-
3
- export function expectStylesInDom(styleUrls: string | string[], shadowRootFn: () => ShadowRoot) {
4
- [styleUrls].flat().forEach(styleUrl => {
5
- const link = shadowRootFn().querySelector(
6
- `link[href='${styleUrl}'][rel='stylesheet'][crossorigin='anonymous']`
7
- );
8
- expect(link).toBeInTheDocument();
9
- });
10
- expect(shadowRootFn().querySelectorAll('link').length).toBe([styleUrls].flat().length);
11
- }
@@ -1,3 +0,0 @@
1
- export * from './expect-styles-in-dom';
2
- export * from './register-component';
3
- export * from './test-component';
@@ -1,49 +0,0 @@
1
- import { render } from '@testing-library/react';
2
- import { MockComponent, registerComponent } from './index';
3
-
4
- /* Wrapper components injected by each optional library. */
5
- export const optionalLibraryWrappers: Record<string, string[]> = {
6
- '@servicetitan/anvil2': ['PortalProvider'],
7
- '@servicetitan/design-system': [
8
- 'DefaultPortalContext.Provider',
9
- 'TablePopupPropsContext.Provider',
10
- ],
11
- };
12
-
13
- export interface RegisterHarness {
14
- Page: {
15
- readonly componentShadowRoot: ShadowRoot;
16
- readonly portalShadowRoot: ShadowRoot;
17
- };
18
- subject: () => ReturnType<typeof render>;
19
- }
20
-
21
- export function createRegisterHarness(
22
- componentName: string,
23
- options?: Parameters<typeof registerComponent>[2]
24
- ): RegisterHarness {
25
- const Page = {
26
- get componentShadowRoot() {
27
- return document.querySelector(componentName)!.shadowRoot!;
28
- },
29
- get portalShadowRoot() {
30
- return document.querySelector('body')!.lastElementChild!.shadowRoot!;
31
- },
32
- };
33
-
34
- beforeAll(() => {
35
- registerComponent(MockComponent, componentName, {
36
- sharedDependenciesNames: ['@servicetitan/anvil2'],
37
- ...options,
38
- });
39
- });
40
-
41
- const subject = () => {
42
- jest.spyOn(console, 'error').mockImplementation(jest.fn()); // suppress "ReactDOM.render deprecated" error
43
- const Component: any = componentName;
44
- const handleRef = (wc: any) => wc?.provide({ foo: 'bar' }); // triggers rendering
45
- return render(<Component ref={handleRef} />);
46
- };
47
-
48
- return { Page, subject };
49
- }
@@ -1,20 +0,0 @@
1
- import { FunctionComponent } from 'react';
2
- import { RegisterOptions } from '../register';
3
-
4
- export function registerComponent(
5
- TestComponent: FunctionComponent,
6
- componentName: string | undefined,
7
- options?: RegisterOptions
8
- ) {
9
- /*
10
- * This register can only happen once, unfortunately. So the TestComponent needs to
11
- * work for all tests. If ran more than once, window.customElements.define will throw
12
- * the error: "NotSupportedError: This name has already been registered in the registry."
13
- *
14
- * Also note that there is not way to "unregister" a web component, so an afterAll
15
- * cannot be used.
16
- */
17
- (global as any).WEB_COMPONENT_NAME = componentName;
18
- const { register } = require('../register');
19
- register(TestComponent, true, options);
20
- }
@@ -1,41 +0,0 @@
1
- import { TablePopupPropsContext } from '@servicetitan/design-system';
2
- import { createContext, FC, useContext } from 'react';
3
- import { useMFEDataContext, useMFEMetadataContext } from '../contexts';
4
-
5
- jest.mock('@progress/kendo-licensing', () => ({ validatePackage: jest.fn() }));
6
-
7
- const FallbackContext = createContext(undefined);
8
-
9
- export const MockComponent: FC<Record<string, any>> = props => {
10
- const data = useMFEDataContext();
11
- const metadata = useMFEMetadataContext();
12
- /*
13
- * TablePopupPropsContext is undefined when a test mocks
14
- * @servicetitan/design-system as an empty object. Fall back to
15
- * a no-op context so the component renders without it.
16
- */
17
- const popupProps = useContext(TablePopupPropsContext ?? FallbackContext);
18
- const appendTo = popupProps?.({}).appendTo;
19
-
20
- return (
21
- <div>
22
- {Object.entries(props).map(([name, value]) => (
23
- <span key={name}>{`Prop: ${name}, Value: ${stringifyValue(value)}`}</span>
24
- ))}
25
- {Object.entries(data).map(([name, value]) => (
26
- <span key={name}>{`Data: ${name}, Value: ${stringifyValue(value)}`}</span>
27
- ))}
28
- {Object.entries(metadata).map(([name, value]) => (
29
- <span key={name}>{`Metadata: ${name}, Value: ${stringifyValue(value)}`}</span>
30
- ))}
31
- {/* eslint-disable-next-line @typescript-eslint/no-base-to-string */}
32
- <span>TablePopupPropsContext appendTo: {String(appendTo)}</span>
33
- </div>
34
- );
35
- };
36
-
37
- function stringifyValue(value: any) {
38
- return JSON.stringify(value, (_key, value) =>
39
- value instanceof ShadowRoot ? 'ShadowRoot' : value
40
- );
41
- }