@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
@@ -1,235 +0,0 @@
1
- import { Log } from '@servicetitan/log-service';
2
- import { Provider } from '@servicetitan/react-ioc';
3
- import { renderHook } from '@testing-library/react';
4
- import { FC, PropsWithChildren } from 'react';
5
- import {
6
- EXPOSED_DEPENDENCIES_TOKEN,
7
- EXPOSED_INSTANCE_DEPENDENCIES_TOKEN,
8
- ExposedDependencies,
9
- ExposedInstanceDependencies,
10
- } from '../common';
11
- import { usePrefetch } from '../use-prefetch';
12
- import { getBundleInfo, supportsPrefetch } from '../utils';
13
-
14
- jest.mock('../utils', () => ({
15
- ...jest.requireActual('../utils'),
16
- getBundleInfo: jest.fn(),
17
- supportsPrefetch: jest.fn(),
18
- }));
19
-
20
- describe(`[web-components] ${usePrefetch.name}`, () => {
21
- const src = 'https://example.com';
22
- let singletons: Parameters<typeof Provider>[0]['singletons'];
23
- let urls: Awaited<ReturnType<typeof getBundleInfo>>['urls'];
24
- let options: Parameters<ReturnType<typeof usePrefetch>['prefetch']>[1];
25
-
26
- beforeAll(() => {
27
- Object.assign(globalThis, {
28
- // eslint-disable-next-line @typescript-eslint/naming-convention
29
- EXPOSED_DEPENDENCIES: { foo: { version: '1.0.0', variable: 'bar' } },
30
- // eslint-disable-next-line @typescript-eslint/naming-convention
31
- EXPOSED_INSTANCE_DEPENDENCIES: { launchDarkly: { version: '1.0.0' } },
32
- });
33
- });
34
-
35
- beforeEach(() => {
36
- jest.clearAllMocks();
37
- singletons = undefined;
38
- urls = { css: [], js: [] };
39
- options = undefined;
40
- jest.mocked(getBundleInfo).mockResolvedValue({ urls } as any);
41
- jest.mocked(supportsPrefetch).mockReturnValue(true);
42
- });
43
-
44
- const subject = async () => {
45
- const Wrapper: FC<PropsWithChildren> = ({ children }) => (
46
- <Provider singletons={singletons}>{children}</Provider>
47
- );
48
-
49
- const { prefetch } = renderHook(() => usePrefetch(), { wrapper: Wrapper }).result.current;
50
-
51
- return prefetch(src, options);
52
- };
53
-
54
- test('retrieves bundle info', async () => {
55
- await subject();
56
-
57
- expect(getBundleInfo).toHaveBeenCalledWith({
58
- exposedDependencies: EXPOSED_DEPENDENCIES,
59
- exposedInstanceDependencies: EXPOSED_INSTANCE_DEPENDENCIES,
60
- mainPackageUrl: src,
61
- cache: -1,
62
- retries: 0,
63
- });
64
- });
65
-
66
- describe('with EXPOSED_DEPENDENCIES_TOKEN', () => {
67
- const exposedDependencies: ExposedDependencies = {
68
- baz: { version: '2.0.0', variable: 'qux' },
69
- };
70
-
71
- beforeEach(() => {
72
- singletons = [
73
- {
74
- provide: EXPOSED_DEPENDENCIES_TOKEN,
75
- useValue: exposedDependencies,
76
- },
77
- ];
78
- });
79
-
80
- test('passes exposed dependencies to getBundleInfo', async () => {
81
- await subject();
82
-
83
- expect(getBundleInfo).toHaveBeenCalledWith(
84
- expect.objectContaining({ exposedDependencies })
85
- );
86
- });
87
- });
88
-
89
- describe('with EXPOSED_INSTANCE_DEPENDENCIES_TOKEN', () => {
90
- const exposedInstanceDependencies: ExposedInstanceDependencies = {
91
- launchDarkly: { version: '2.0.0', ldService: {} as any },
92
- };
93
-
94
- beforeEach(() => {
95
- singletons = [
96
- {
97
- provide: EXPOSED_INSTANCE_DEPENDENCIES_TOKEN,
98
- useValue: exposedInstanceDependencies,
99
- },
100
- ];
101
- });
102
-
103
- test('passes exposed instance dependencies to getBundleInfo', async () => {
104
- await subject();
105
-
106
- expect(getBundleInfo).toHaveBeenCalledWith(
107
- expect.objectContaining({
108
- exposedInstanceDependencies,
109
- })
110
- );
111
- });
112
- });
113
-
114
- describe('with cache option', () => {
115
- beforeEach(() => (options = { cache: 1000 }));
116
-
117
- test('passes cache option to getBundleInto', async () => {
118
- await subject();
119
-
120
- expect(getBundleInfo).toHaveBeenCalledWith(
121
- expect.objectContaining({ cache: options!.cache })
122
- );
123
- });
124
- });
125
-
126
- describe('when bundle contains urls', () => {
127
- const css = ['foo', 'bar'].map(name => `${src}/${name}.bundle.css`);
128
- const js = ['foo', 'bar'].map(name => `${src}/${name}.bundle.js`);
129
-
130
- beforeEach(() => {
131
- Object.assign(urls, { css, js });
132
- Array.from(document.head.getElementsByTagName('link')).forEach(link => link.remove());
133
- });
134
-
135
- function getLinks() {
136
- return Array.from(document.head.getElementsByTagName('link')).map(
137
- ({ as, crossOrigin, href, rel }) => ({ as, crossOrigin, href, rel })
138
- );
139
- }
140
-
141
- function itDoesNotAddLinks() {
142
- test('does not add links', async () => {
143
- const originalLinks = getLinks();
144
-
145
- await subject();
146
-
147
- expect(getLinks()).toEqual(originalLinks);
148
- });
149
- }
150
-
151
- test('adds prefetch links to document head', async () => {
152
- await subject();
153
-
154
- expect(getLinks()).toEqual([
155
- ...css.map(href => ({
156
- as: 'style',
157
- crossOrigin: 'anonymous',
158
- href,
159
- rel: 'prefetch',
160
- })),
161
- ...js.map(href => ({
162
- as: 'script',
163
- crossOrigin: 'anonymous',
164
- href,
165
- rel: 'prefetch',
166
- })),
167
- ]);
168
- });
169
-
170
- describe('when bundle is already cached', () => {
171
- beforeEach(() => {
172
- jest.mocked(getBundleInfo).mockResolvedValue({ urls, cacheHit: true } as any);
173
- });
174
-
175
- itDoesNotAddLinks();
176
- });
177
-
178
- describe('when links already exist', () => {
179
- function addLink(props: Record<string, any>) {
180
- const link = document.createElement('link');
181
- Object.assign(link, props);
182
- document.head.append(link);
183
- }
184
-
185
- beforeEach(() => {
186
- css.forEach(href => addLink({ href }));
187
- js.forEach(href => addLink({ href }));
188
- });
189
-
190
- itDoesNotAddLinks();
191
- });
192
-
193
- describe('when rel=prefetch is not supported', () => {
194
- beforeEach(() => jest.mocked(supportsPrefetch).mockReturnValue(false));
195
-
196
- test('manually fetches urls with low priority', async () => {
197
- const fetchSpy = jest.spyOn(globalThis, 'fetch').mockResolvedValue({} as any);
198
-
199
- await subject();
200
-
201
- [...css, ...js].forEach(url =>
202
- expect(fetchSpy).toHaveBeenCalledWith(url, { priority: 'low' })
203
- );
204
- });
205
- });
206
- });
207
-
208
- describe('when getBundleInfo throws an error', () => {
209
- const error = 'Oops!';
210
-
211
- beforeEach(() => jest.mocked(getBundleInfo).mockRejectedValue(error));
212
-
213
- test('suppresses the error', async () => {
214
- await expect(subject()).resolves.toBe(undefined);
215
- });
216
-
217
- describe('when Log is provided', () => {
218
- const log = { warning: jest.fn() };
219
-
220
- beforeEach(() => {
221
- singletons = [{ provide: Log, useValue: log }];
222
- });
223
-
224
- test('logs warning', async () => {
225
- await subject();
226
-
227
- expect(log.warning).toHaveBeenCalledWith({
228
- category: 'Microfrontends.Prefetch',
229
- message: expect.stringMatching(/failed to prefetch/i),
230
- data: error,
231
- });
232
- });
233
- });
234
- });
235
- });
@@ -1,64 +0,0 @@
1
- import { renderHook } from '@testing-library/react';
2
- import { FC, PropsWithChildren } from 'react';
3
- import { JSON } from '../../types';
4
- import { MFEDataContext, useMFEDataContext } from '../mfe-data-context';
5
-
6
- describe(`[web-components] ${useMFEDataContext.name}`, () => {
7
- const subject = (wrapper?: FC) => renderHook(() => useMFEDataContext(), { wrapper });
8
-
9
- describe('when the MFEDataContext is provided by a parent', () => {
10
- const data = { foo: 'bar' };
11
- const Wrapper: FC<PropsWithChildren> = ({ children }) => {
12
- return <MFEDataContext.Provider value={data}>{children}</MFEDataContext.Provider>;
13
- };
14
-
15
- test('returns the context', () => {
16
- expect(subject(Wrapper).result.current).toEqual(data);
17
- });
18
- });
19
-
20
- describe('when the MFEDataContext is not provided by a parent', () => {
21
- const Wrapper: FC<PropsWithChildren> = ({ children }) => {
22
- return <div>{children}</div>;
23
- };
24
-
25
- test('returns an empty object', () => {
26
- expect(subject(Wrapper).result.current).toEqual({});
27
- });
28
- });
29
-
30
- describe('type definitions', () => {
31
- test('accepts properties typed as JSON', () => {
32
- renderHook(() => useMFEDataContext<{ data: JSON }>());
33
- });
34
-
35
- test('accepts properties typed as Record<string, JSON>', () => {
36
- renderHook(() => useMFEDataContext<{ metadata: Record<string, JSON> }>());
37
- });
38
-
39
- test('accepts primitive properties', () => {
40
- renderHook(() => useMFEDataContext<{ name: string; count: number; active: boolean }>());
41
- });
42
-
43
- test('accepts nested objects and arrays', () => {
44
- renderHook(() => useMFEDataContext<{ nested: { value: number }; list: string[] }>());
45
- });
46
-
47
- test('rejects non-serializable properties', () => {
48
- // @ts-expect-error Type '() => void' is not assignable to type 'never'.
49
- renderHook(() => useMFEDataContext<{ callback: () => void }>());
50
-
51
- // @ts-expect-error Type 'Date' is not assignable to type 'never'.
52
- renderHook(() => useMFEDataContext<{ created: Date }>());
53
-
54
- // @ts-expect-error Type 'Map<string, any>' is not assignable to type 'never'.
55
- renderHook(() => useMFEDataContext<{ lookup: Map<string, any> }>());
56
-
57
- // @ts-expect-error Type 'Set<string>' is not assignable to type 'never'.
58
- renderHook(() => useMFEDataContext<{ tags: Set<string> }>());
59
-
60
- // @ts-expect-error Type 'symbol' is not assignable to type 'never'.
61
- renderHook(() => useMFEDataContext<{ id: symbol }>());
62
- });
63
- });
64
- });
@@ -1,34 +0,0 @@
1
- import { renderHook } from '@testing-library/react';
2
- import { FC, PropsWithChildren } from 'react';
3
- import { MFEMetadataContext, useMFEMetadataContext } from '../mfe-metadata-context';
4
-
5
- describe(`[web-components] ${useMFEMetadataContext.name}`, () => {
6
- const subject = (wrapper?: FC) => renderHook(() => useMFEMetadataContext(), { wrapper });
7
-
8
- describe('when the MFEMetadataContext is provided by a parent', () => {
9
- const shadowRoot = document.createElement('div').attachShadow({ mode: 'open' });
10
- const portalShadowRoot = document.createElement('span').attachShadow({ mode: 'open' });
11
- const metadata = { shadowRoot, portalShadowRoot };
12
- const Wrapper: FC<PropsWithChildren> = ({ children }) => {
13
- return (
14
- <MFEMetadataContext.Provider value={metadata}>
15
- {children}
16
- </MFEMetadataContext.Provider>
17
- );
18
- };
19
-
20
- test('returns the context', () => {
21
- expect(subject(Wrapper).result.current).toEqual(metadata);
22
- });
23
- });
24
-
25
- describe('when the MFEMetadataContext is not provided by a parent', () => {
26
- const Wrapper: FC<PropsWithChildren> = ({ children }) => {
27
- return <div>{children}</div>;
28
- };
29
-
30
- test('returns an empty object', () => {
31
- expect(subject(Wrapper).result.current).toEqual({});
32
- });
33
- });
34
- });
@@ -1,163 +0,0 @@
1
- import { MfeStyleRegistry } from '../mfe-style-registry';
2
-
3
- beforeAll(() => {
4
- if (!CSSStyleSheet.prototype.replaceSync) {
5
- CSSStyleSheet.prototype.replaceSync = jest.fn();
6
- }
7
- });
8
-
9
- describe('[web-components] MfeStyleRegistry', () => {
10
- const createRoot = () => {
11
- const root = document.createElement('div').attachShadow({ mode: 'open' });
12
- root.adoptedStyleSheets = [];
13
- return root;
14
- };
15
-
16
- beforeEach(() => {
17
- delete globalThis.__mfeStyles__;
18
- });
19
-
20
- describe(MfeStyleRegistry.for.name, () => {
21
- test('initializes globalThis.__mfeStyles__', () => {
22
- MfeStyleRegistry.for('test-mfe');
23
-
24
- expect(globalThis.__mfeStyles__).toBeDefined();
25
- });
26
-
27
- test('returns the same registry for the same component', () => {
28
- const a = MfeStyleRegistry.for('test-mfe');
29
- const b = MfeStyleRegistry.for('test-mfe');
30
-
31
- expect(a).toBe(b);
32
- });
33
-
34
- test('returns different registries for different components', () => {
35
- const a = MfeStyleRegistry.for('mfe-a');
36
- const b = MfeStyleRegistry.for('mfe-b');
37
-
38
- expect(a).not.toBe(b);
39
- });
40
- });
41
-
42
- describe('injectSheet', () => {
43
- const defaultCss = '.foo { color: red; }';
44
- const defaultSheetId = 'sheet-1';
45
-
46
- let registry: MfeStyleRegistry;
47
-
48
- beforeEach(() => {
49
- registry = MfeStyleRegistry.for('test-mfe');
50
- });
51
-
52
- const subject = (sheetId = defaultSheetId, css = defaultCss) =>
53
- registry.injectSheet(sheetId, css);
54
-
55
- test('creates a new CSSStyleSheet and stores it', () => {
56
- subject();
57
-
58
- const sheet = registry.sheets.get('sheet-1');
59
- expect(sheet).toBeInstanceOf(CSSStyleSheet);
60
- expect(sheet!.replaceSync).toHaveBeenCalledWith(defaultCss);
61
- });
62
-
63
- test('adopts new sheets into observed roots', () => {
64
- const root = createRoot();
65
- registry.observeRoot(root);
66
-
67
- subject();
68
-
69
- expect(root.adoptedStyleSheets).toContain(registry.sheets.get('sheet-1'));
70
- });
71
-
72
- describe('when sheet with the same ID already exists', () => {
73
- beforeEach(() => {
74
- subject();
75
- });
76
-
77
- test('updates existing sheet in place via replaceSync', () => {
78
- const sheet = registry.sheets.get(defaultSheetId)!;
79
-
80
- const newCSS = '.foo { color: blue; }';
81
- subject(defaultSheetId, newCSS);
82
-
83
- expect(registry.sheets.get(defaultSheetId)).toBe(sheet);
84
- expect(sheet.replaceSync).toHaveBeenLastCalledWith(newCSS);
85
- });
86
- });
87
- });
88
-
89
- describe('observeRoot', () => {
90
- let registry: MfeStyleRegistry;
91
-
92
- beforeEach(() => {
93
- registry = MfeStyleRegistry.for('test-mfe');
94
- });
95
-
96
- test('adds root to the roots set', () => {
97
- const root = createRoot();
98
-
99
- registry.observeRoot(root);
100
-
101
- expect(registry.roots.has(root)).toBe(true);
102
- });
103
-
104
- describe('when root is already observed', () => {
105
- let root: ShadowRoot;
106
-
107
- beforeEach(() => {
108
- registry.injectSheet('a', '.a {}');
109
- root = createRoot();
110
- registry.observeRoot(root);
111
- });
112
-
113
- test('does not duplicate sheets', () => {
114
- registry.observeRoot(root);
115
-
116
- expect(root.adoptedStyleSheets).toHaveLength(1);
117
- });
118
- });
119
-
120
- test('adopts all existing sheets into the root', () => {
121
- registry.injectSheet('a', '.a {}');
122
- registry.injectSheet('b', '.b {}');
123
- const root = createRoot();
124
-
125
- registry.observeRoot(root);
126
-
127
- expect(root.adoptedStyleSheets).toEqual([...registry.sheets.values()]);
128
- });
129
- });
130
-
131
- describe('unobserveRoot', () => {
132
- let registry: MfeStyleRegistry;
133
- let root: ShadowRoot;
134
-
135
- beforeEach(() => {
136
- registry = MfeStyleRegistry.for('test-mfe');
137
- root = createRoot();
138
- const defaultSheetId = 'sheet-1';
139
- registry.injectSheet(defaultSheetId, '.foo {}');
140
- registry.observeRoot(root);
141
- });
142
-
143
- test('removes root from the roots set', () => {
144
- registry.unobserveRoot(root);
145
-
146
- expect(registry.roots.has(root)).toBe(false);
147
- });
148
-
149
- test('removes adopted sheets from the root', () => {
150
- registry.unobserveRoot(root);
151
-
152
- expect(root.adoptedStyleSheets).toHaveLength(0);
153
- });
154
-
155
- test('stops receiving newly adopted sheets', () => {
156
- registry.unobserveRoot(root);
157
-
158
- registry.injectSheet('sheet-2', '.bar {}');
159
-
160
- expect(root.adoptedStyleSheets).toHaveLength(0);
161
- });
162
- });
163
- });
@@ -1,128 +0,0 @@
1
- import { render } from '@testing-library/react';
2
- import { FC } from 'react';
3
- import { useMFEMetadataContext } from '../../contexts/mfe-metadata-context';
4
- import { MfeStyleRegistry } from '../mfe-style-registry';
5
- import { withCssInjector } from '../with-css-injector';
6
-
7
- jest.mock('../../contexts/mfe-metadata-context');
8
- jest.mock('../../globals', () => ({
9
- // eslint-disable-next-line @typescript-eslint/naming-convention
10
- WEB_COMPONENT_NAME: 'test-mfe',
11
- }));
12
- jest.mock('../mfe-style-registry');
13
-
14
- describe(`[web-components] ${withCssInjector.name}`, () => {
15
- const innerTestId = 'inner';
16
- const innerLabel = 'foo';
17
- const InnerComponent: FC<{ label: string }> = ({ label }) => (
18
- <div data-testid={innerTestId}>{label}</div>
19
- );
20
- const WrappedComponent = withCssInjector(InnerComponent);
21
-
22
- let registry: jest.Mocked<MfeStyleRegistry>;
23
- let shadowRoot: ShadowRoot;
24
- let portalShadowRoot: ShadowRoot;
25
-
26
- beforeEach(() => {
27
- jest.clearAllMocks();
28
- delete globalThis.__mfeSelfHosted__;
29
- registry = {
30
- observeRoot: jest.fn(),
31
- unobserveRoot: jest.fn(),
32
- } as unknown as jest.Mocked<MfeStyleRegistry>;
33
- jest.mocked(MfeStyleRegistry.for).mockReturnValue(registry);
34
- shadowRoot = document.createElement('div').attachShadow({ mode: 'open' });
35
- portalShadowRoot = document.createElement('span').attachShadow({ mode: 'open' });
36
- jest.mocked(useMFEMetadataContext).mockImplementation(() => ({
37
- shadowRoot,
38
- portalShadowRoot,
39
- }));
40
- });
41
-
42
- const subject = () => render(<WrappedComponent label={innerLabel} />);
43
-
44
- test('renders the wrapped component with its props', () => {
45
- const { getByTestId } = subject();
46
-
47
- expect(getByTestId(innerTestId).textContent).toBe(innerLabel);
48
- });
49
-
50
- test('retrieves the registry for the component', () => {
51
- subject();
52
-
53
- expect(MfeStyleRegistry.for).toHaveBeenCalledWith('test-mfe');
54
- });
55
-
56
- test('observes shadow root', () => {
57
- subject();
58
-
59
- expect(registry.observeRoot).toHaveBeenCalledWith(shadowRoot);
60
- });
61
-
62
- test('observes portal shadow root', () => {
63
- subject();
64
-
65
- expect(registry.observeRoot).toHaveBeenCalledWith(portalShadowRoot);
66
- });
67
-
68
- describe('when self-hosted', () => {
69
- beforeEach(() => {
70
- globalThis.__mfeSelfHosted__ = true;
71
- });
72
-
73
- test('observes document', () => {
74
- subject();
75
-
76
- expect(registry.observeRoot).toHaveBeenCalledWith(document);
77
- });
78
-
79
- test('unobserves document on unmount', () => {
80
- const { unmount } = subject();
81
-
82
- unmount();
83
-
84
- expect(registry.unobserveRoot).toHaveBeenCalledWith(document);
85
- });
86
- });
87
-
88
- describe('when unmounted', () => {
89
- test('unobserves shadow root', () => {
90
- const { unmount } = subject();
91
-
92
- unmount();
93
-
94
- expect(registry.unobserveRoot).toHaveBeenCalledWith(shadowRoot);
95
- });
96
-
97
- test('unobserves portal shadow root', () => {
98
- const { unmount } = subject();
99
-
100
- unmount();
101
-
102
- expect(registry.unobserveRoot).toHaveBeenCalledWith(portalShadowRoot);
103
- });
104
- });
105
-
106
- describe('in production', () => {
107
- const originalEnv = process.env.NODE_ENV;
108
-
109
- beforeEach(() => {
110
- process.env.NODE_ENV = 'production';
111
- });
112
-
113
- afterEach(() => {
114
- process.env.NODE_ENV = originalEnv;
115
- });
116
-
117
- test('returns Component directly', () => {
118
- expect.assertions(1);
119
-
120
- jest.isolateModules(() => {
121
- const { withCssInjector: freshWithCssInjector } = require('../with-css-injector');
122
- const Dummy: FC = () => <div />;
123
-
124
- expect(freshWithCssInjector(Dummy)).toBe(Dummy);
125
- });
126
- });
127
- });
128
- });