@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,32 +0,0 @@
1
- /*
2
- * Split from register-anvil2.test.tsx because register.tsx changed from
3
- * dynamic require() to static `import * as` for Vite/ESM compatibility.
4
- * The previous approach used jest.resetModules() mid-file to re-mock
5
- * modules between tests, but static imports are resolved once at module
6
- * load time and aren't affected by resetModules. Separate files give
7
- * each test suite its own jest.mock declarations.
8
- */
9
- import '@testing-library/jest-dom';
10
- import { within } from '@testing-library/react';
11
- import {
12
- createRegisterHarness,
13
- optionalLibraryWrappers,
14
- } from '../__mocks__/register-anvil2-harness';
15
-
16
- jest.mock('@servicetitan/anvil2', () => ({}));
17
- jest.mock('@servicetitan/design-system', () => ({}));
18
-
19
- describe('[web-components] register', () => {
20
- const { Page, subject } = createRegisterHarness('mock-no-optional-deps');
21
-
22
- for (const [library, wrappers] of Object.entries(optionalLibraryWrappers)) {
23
- describe(`when ${library} is not present`, () => {
24
- test.each(wrappers)('does not wrap component within %s', name => {
25
- subject();
26
-
27
- const element = within(Page.componentShadowRoot as any).queryByText(`<${name}>`);
28
- expect(element).not.toBeInTheDocument();
29
- });
30
- });
31
- }
32
- });
@@ -1,80 +0,0 @@
1
- import '@testing-library/jest-dom';
2
- import { within } from '@testing-library/react';
3
- import { Fragment } from 'react';
4
- import { expectStylesInDom } from '../__mocks__';
5
- import {
6
- createRegisterHarness,
7
- optionalLibraryWrappers,
8
- } from '../__mocks__/register-anvil2-harness';
9
-
10
- function mockComponent(name: string) {
11
- return {
12
- [name]: ({ children }: any) => (
13
- <Fragment>
14
- <div>{`<${name}>`}</div>
15
- {children}
16
- </Fragment>
17
- ),
18
- };
19
- }
20
-
21
- function mockContext(name: string) {
22
- return {
23
- [name]: {
24
- Provider: ({ children }: any) => (
25
- <Fragment>
26
- <div>{`<${name}.Provider>`}</div>
27
- {children}
28
- </Fragment>
29
- ),
30
- },
31
- };
32
- }
33
-
34
- jest.mock('@servicetitan/anvil2', () => ({
35
- ...mockComponent('PortalProvider'),
36
- }));
37
-
38
- jest.mock('@servicetitan/design-system', () => ({
39
- ...mockContext('DefaultPortalContext'),
40
- ...mockContext('TablePopupPropsContext'),
41
- }));
42
-
43
- describe('[web-components] register', () => {
44
- const { Page, subject } = createRegisterHarness('mock-anvil2-app');
45
-
46
- describe('when Anvil2 bundle is present in the DOM', () => {
47
- let cssLink: HTMLLinkElement;
48
-
49
- beforeEach(() => {
50
- cssLink = Object.assign(document.createElement('link'), {
51
- href: 'http://localhost/anvil2.foobar.bundle.css',
52
- rel: 'stylesheet',
53
- });
54
- document.body.appendChild(cssLink);
55
- });
56
-
57
- afterEach(() => {
58
- cssLink.remove();
59
- });
60
-
61
- test('loads the styles into the component shadow DOM', () => {
62
- subject();
63
-
64
- expectStylesInDom(cssLink.href, () => Page.componentShadowRoot);
65
- });
66
-
67
- test('loads the styles into the portal shadow DOM', () => {
68
- subject();
69
-
70
- expectStylesInDom(cssLink.href, () => Page.portalShadowRoot);
71
- });
72
- });
73
-
74
- test.each(Object.values(optionalLibraryWrappers).flat())('wraps component within %s', name => {
75
- subject();
76
-
77
- const element = within(Page.componentShadowRoot as any).queryByText(`<${name}>`);
78
- expect(element).toBeInTheDocument();
79
- });
80
- });
@@ -1,97 +0,0 @@
1
- import '@testing-library/jest-dom';
2
- import { render, waitFor } from '@testing-library/react';
3
- import { Entries, IWebComponent } from '../common';
4
- import { HeadlessCallback } from '../register-headless';
5
-
6
- export function registerComponent(
7
- connectedCallback: HeadlessCallback<unknown>,
8
- disconnectedCallback: HeadlessCallback<unknown>,
9
- componentName: string | undefined
10
- ) {
11
- /*
12
- * This register can only happen once, unfortunately. So the connectedCallback and
13
- * disconnectedCallback need to work for all tests. If run more than once,
14
- * window.customElements.define will throw the error:
15
- * "NotSupportedError: This name has already been registered in the registry."
16
- *
17
- * Also note that there is no way to "unregister" a web component, so an afterAll
18
- * cannot be used.
19
- */
20
- (global as any).WEB_COMPONENT_NAME = componentName;
21
- const { registerHeadless } = require('../register-headless');
22
- registerHeadless(connectedCallback, disconnectedCallback);
23
- }
24
-
25
- describe('[web-components] registerHeadless', () => {
26
- const baseComponentName = 'mock-app';
27
- const componentName = `${baseComponentName}-40b38a39`;
28
- const connectedCallback = jest.fn().mockName('connectedCallback');
29
- const disconnectedCallback = jest.fn().mockName('disconnectedCallback');
30
- const provideProps = {
31
- ldService: 'ld-service',
32
- logService: 'log-service',
33
- eventBus: 'event-bus',
34
- } as unknown as Entries;
35
- const mfeData = { foo: 'bar' };
36
- const handleRef = (webComponent: IWebComponent | null) => {
37
- webComponent?.provide(provideProps);
38
- };
39
-
40
- beforeAll(() => {
41
- registerComponent(connectedCallback, disconnectedCallback, componentName);
42
- });
43
-
44
- const subject = (props: Record<string, any> = {}, dataAttrs: Record<string, any> = {}) => {
45
- jest.spyOn(console, 'error').mockImplementation(jest.fn()); // suppress "attempt to unmount root while rendering" error
46
-
47
- const data = props && { 'data-mfe-data': JSON.stringify(props) };
48
- const Component: any = `headless-${componentName}`;
49
- const renderResult = render(<Component ref={handleRef} {...data} {...dataAttrs} />);
50
-
51
- return renderResult;
52
- };
53
-
54
- beforeEach(() => {
55
- jest.clearAllMocks();
56
- });
57
-
58
- [connectedCallback, disconnectedCallback].forEach(callback => {
59
- test(`calls ${callback.getMockName()} with mfeData`, async () => {
60
- const renderResult = subject({}, { 'data-mfe-data': JSON.stringify(mfeData) });
61
- renderResult.unmount();
62
-
63
- await waitFor(() => {
64
- expect(callback).toHaveBeenCalledWith(
65
- expect.objectContaining({
66
- mfeData,
67
- })
68
- );
69
- });
70
- });
71
- });
72
-
73
- [connectedCallback, disconnectedCallback].forEach(callback => {
74
- (['ldService', 'logService', 'eventBus'] as const).forEach(service => {
75
- test(`calls ${callback.getMockName()} with ${service}`, async () => {
76
- const renderResult = subject();
77
- renderResult.unmount();
78
-
79
- await waitFor(() => {
80
- expect(callback).toHaveBeenCalledWith(
81
- expect.objectContaining({
82
- [service]: provideProps[service],
83
- })
84
- );
85
- });
86
- });
87
- });
88
- });
89
-
90
- test('logs an error when WEB_COMPONENT_NAME is not set', () => {
91
- const consoleSpy = jest.spyOn(console, 'error').mockImplementation(jest.fn());
92
- jest.isolateModules(() => {
93
- registerComponent(connectedCallback, disconnectedCallback, undefined);
94
- });
95
- expect(consoleSpy).toHaveBeenCalledWith('"WEB_COMPONENT_NAME" is not defined!');
96
- });
97
- });
@@ -1,38 +0,0 @@
1
- import '@testing-library/jest-dom';
2
- import { render } from '@testing-library/react';
3
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
4
- // @ts-ignore because Jest mistakenly complains that "'shadow-dom-testing-library' cannot be imported using this construct".
5
- import { screen } from 'shadow-dom-testing-library';
6
-
7
- import { MockComponent, registerComponent } from '../__mocks__';
8
-
9
- describe('[web-components] register', () => {
10
- const componentName = 'mock-legacy-root-app';
11
-
12
- beforeAll(() => {
13
- registerComponent(MockComponent, componentName, { legacyRoot: true });
14
- });
15
-
16
- const subject = () => {
17
- jest.spyOn(console, 'error').mockImplementation(jest.fn()); // suppress "ReactDOM.render deprecated" error
18
- const Component: any = componentName;
19
- return render(<Component />);
20
- };
21
-
22
- describe('when the web component data attribute is changed', () => {
23
- jest.retryTimes(3, { logErrorsBeforeRetry: true });
24
-
25
- const setup = () => {
26
- subject();
27
-
28
- const webComponent = document.getElementsByTagName(componentName);
29
- webComponent[0]?.setAttribute('data-mfe-data', JSON.stringify({ string: 'baz' }));
30
- };
31
-
32
- test('useMFEDataContext provides updated data', async () => {
33
- setup();
34
-
35
- expect(await screen.findByShadowText(`Data: string, Value: "baz"`)).toBeInTheDocument();
36
- });
37
- });
38
- });
@@ -1,247 +0,0 @@
1
- import '@testing-library/jest-dom';
2
- import { act, render } from '@testing-library/react';
3
- import { screen } from 'shadow-dom-testing-library';
4
- import { expectStylesInDom, MockComponent, registerComponent } from '../__mocks__';
5
- import { IWebComponent } from '../common';
6
-
7
- describe('[web-components] register', () => {
8
- const baseComponentName = 'mock-app';
9
- const componentName = `${baseComponentName}-40b38a38`;
10
- let props: Record<string, any> | undefined;
11
- let dataAttrs: Record<string, any> | undefined;
12
- let onReady: (() => void) | undefined;
13
- let onDispose: (() => void) | undefined;
14
- let onloadRefs: Function[];
15
-
16
- class Page {
17
- static get componentShadowRoot() {
18
- return document.querySelector(componentName)!.shadowRoot!;
19
- }
20
-
21
- static get portalElement() {
22
- return document.querySelector('body')!.lastElementChild!;
23
- }
24
-
25
- static get portalShadowRoot() {
26
- return Page.portalElement.shadowRoot!;
27
- }
28
- }
29
-
30
- beforeAll(() => {
31
- registerComponent(MockComponent, componentName, {
32
- sharedDependenciesNames: ['@servicetitan/design-system'],
33
- });
34
- /*
35
- * mock <link>'s onload so that we can call it directly from the test,
36
- * because it won't call itself in the test environment
37
- */
38
- Object.defineProperty(HTMLLinkElement.prototype, 'onload', {
39
- get() {
40
- return this._onload;
41
- },
42
- set(onload: Function) {
43
- onloadRefs.push(onload);
44
- this._onload = onload;
45
- },
46
- });
47
- });
48
-
49
- beforeEach(() => {
50
- props = undefined;
51
- dataAttrs = undefined;
52
- onReady = undefined;
53
- onDispose = undefined;
54
- onloadRefs = [];
55
- });
56
-
57
- const subject = () => {
58
- jest.spyOn(console, 'error').mockImplementation(jest.fn()); // suppress "attempt to unmount root while rendering" error
59
-
60
- const handleRef = (webComponent: IWebComponent | null) => {
61
- webComponent?.provide({ onReady, onDispose });
62
- };
63
-
64
- const data = props && { 'data-mfe-data': JSON.stringify(props) };
65
- const Component: any = componentName;
66
- const renderResult = render(<Component ref={handleRef} {...data} {...dataAttrs} />);
67
-
68
- act(() => {
69
- for (const onload of onloadRefs) {
70
- onload();
71
- }
72
- });
73
-
74
- return renderResult;
75
- };
76
-
77
- test('exposes metadata', () => {
78
- const metadata = {
79
- shadowRoot: 'ShadowRoot',
80
- portalShadowRoot: 'ShadowRoot',
81
- };
82
-
83
- subject();
84
-
85
- Object.entries(metadata).forEach(([name, value]) =>
86
- expect(
87
- screen.getByShadowText(`Metadata: ${name}, Value: ${JSON.stringify(value)}`)
88
- ).toBeInTheDocument()
89
- );
90
- });
91
-
92
- describe('when component has style urls', () => {
93
- const styleUrls = ['foo.css', 'bar.css'];
94
-
95
- beforeEach(() => {
96
- dataAttrs = { 'style-urls': JSON.stringify(styleUrls) };
97
- });
98
-
99
- test('loads the styles into the component shadow DOM', () => {
100
- subject();
101
-
102
- expectStylesInDom(styleUrls, () => Page.componentShadowRoot);
103
- });
104
-
105
- test('loads the styles into the portal shadow DOM', () => {
106
- subject();
107
-
108
- expectStylesInDom(styleUrls, () => Page.portalShadowRoot);
109
- });
110
-
111
- describe('when a design system link is present in the DOM', () => {
112
- let designSystemLink: HTMLLinkElement;
113
-
114
- beforeEach(() => {
115
- designSystemLink = Object.assign(document.createElement('link'), {
116
- href: 'http://localhost/design-system.foobar.bundle.css',
117
- rel: 'stylesheet',
118
- });
119
- document.body.appendChild(designSystemLink);
120
- });
121
-
122
- afterEach(() => {
123
- designSystemLink.remove();
124
- });
125
-
126
- test('loads the styles and design system styles into the component shadow DOM', () => {
127
- subject();
128
-
129
- expectStylesInDom(
130
- [...styleUrls, designSystemLink.href],
131
- () => Page.componentShadowRoot
132
- );
133
- });
134
-
135
- test('loads the styles and design system styles into the portal shadow DOM', () => {
136
- subject();
137
-
138
- expectStylesInDom(
139
- [...styleUrls, designSystemLink.href],
140
- () => Page.portalShadowRoot
141
- );
142
- });
143
- });
144
- });
145
-
146
- describe('when component has custom data-attributes', () => {
147
- beforeEach(() => {
148
- props = {
149
- string: 'foo',
150
- array: JSON.stringify(['foo', 'bar']),
151
- object: JSON.stringify({ foo: 'bar' }),
152
- objectNotStringified: { foo: 'bar' },
153
- };
154
- });
155
-
156
- it('passes data attributes as props', () => {
157
- subject();
158
-
159
- Object.entries(props!).forEach(([name, value]) =>
160
- expect(
161
- screen.getByShadowText(`Prop: ${name}, Value: ${JSON.stringify(value)}`)
162
- ).toBeInTheDocument()
163
- );
164
- });
165
-
166
- test('useMFEDataContext exposes data attributes', () => {
167
- subject();
168
-
169
- Object.entries(props!).forEach(([name, value]) =>
170
- expect(
171
- screen.getByShadowText(`Data: ${name}, Value: ${JSON.stringify(value)}`)
172
- ).toBeInTheDocument()
173
- );
174
- });
175
- });
176
-
177
- test('logs an error when WEB_COMPONENT_NAME is not set', () => {
178
- const consoleSpy = jest.spyOn(console, 'error').mockImplementation(jest.fn());
179
-
180
- jest.isolateModules(() => {
181
- registerComponent(MockComponent, undefined);
182
- });
183
-
184
- expect(consoleSpy).toHaveBeenCalledWith('"WEB_COMPONENT_NAME" is not defined!');
185
- });
186
-
187
- test('configures the shadow root in PopupPropsContext', () => {
188
- subject();
189
-
190
- expect(screen.getByShadowText(/TablePopupPropsContext appendTo:/)).toHaveTextContent(
191
- /\[object ShadowRoot\]/
192
- );
193
- });
194
-
195
- test('assigns "data-mfe-portal-name" to portal element', () => {
196
- subject();
197
-
198
- expect(Page.portalElement).toHaveAttribute('data-mfe-portal-name', baseComponentName);
199
- });
200
-
201
- describe('with a "data-mfe-version" attribute', () => {
202
- const version = '1.2.3';
203
-
204
- beforeEach(() => {
205
- dataAttrs = { 'data-mfe-version': version };
206
- });
207
-
208
- test('assigns "data-mfe-version" to portal element', () => {
209
- subject();
210
-
211
- expect(Page.portalElement).toHaveAttribute('data-mfe-version', version);
212
- });
213
- });
214
-
215
- test('does not throw when document body is removed', () => {
216
- const body = document.body;
217
- const parentNode = body.parentNode!;
218
- try {
219
- subject();
220
- expect(() => parentNode.removeChild(body)).not.toThrow();
221
- } finally {
222
- parentNode.appendChild(body);
223
- }
224
- });
225
-
226
- describe('with onReady callback', () => {
227
- beforeEach(() => (onReady = jest.fn()));
228
-
229
- test('calls onReady', () => {
230
- subject();
231
-
232
- expect(onReady).toHaveBeenCalled();
233
- });
234
- });
235
-
236
- describe('with onDispose callback', () => {
237
- beforeEach(() => (onDispose = jest.fn()));
238
-
239
- test('calls onDispose when element is removed from DOM', () => {
240
- const { container } = subject();
241
-
242
- container.parentNode!.removeChild(container);
243
-
244
- expect(onDispose).toHaveBeenCalled();
245
- });
246
- });
247
- });