@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,94 +0,0 @@
1
- import { getJson } from '../../common';
2
- import { getCachedMetadata } from '../get-cached-metadata';
3
-
4
- jest.mock('../../common', () => ({
5
- ...jest.requireActual('../../common'),
6
- getJson: jest.fn(),
7
- }));
8
-
9
- describe(`[web-components] ${getCachedMetadata.name}`, () => {
10
- const response = {
11
- data: { name: 'foo', sharedDependencies: {} },
12
- headers: {} as Headers,
13
- url: 'https://example.com/foo/dist/metadata.json',
14
- };
15
-
16
- let props: Parameters<typeof getCachedMetadata>[0];
17
- let resolveRequest: ((value: typeof response) => void) | undefined;
18
- let rejectRequest: ((reason: Error) => void) | undefined;
19
-
20
- beforeEach(() => {
21
- jest.clearAllMocks();
22
- resolveRequest = undefined;
23
- rejectRequest = undefined;
24
- props = { cache: false, mainPackageUrl: 'https://example.com/foo' };
25
- jest.mocked(getJson).mockImplementation(
26
- () =>
27
- new Promise((resolve, reject) => {
28
- resolveRequest = resolve;
29
- rejectRequest = reject;
30
- })
31
- );
32
- });
33
-
34
- afterEach(() => resolveRequest?.(response));
35
-
36
- const subject = () => getCachedMetadata(props);
37
-
38
- describe('when a request is pending', () => {
39
- let first: ReturnType<typeof subject>;
40
-
41
- beforeEach(() => {
42
- first = subject();
43
- first.catch(() => {});
44
- });
45
-
46
- test('reuses the pending request', () => {
47
- subject();
48
-
49
- expect(getJson).toHaveBeenCalledTimes(1);
50
- });
51
-
52
- describe('when another caller starts a request', () => {
53
- let second: ReturnType<typeof subject>;
54
-
55
- beforeEach(() => {
56
- second = subject();
57
- second.catch(() => {});
58
- });
59
-
60
- describe('when the pending request resolves', () => {
61
- beforeEach(() => resolveRequest?.(response));
62
-
63
- test('delivers the response to all callers', async () => {
64
- expect(await Promise.all([first, second])).toEqual([response, response]);
65
- });
66
-
67
- test('subsequent caller requests metadata again', () => {
68
- subject();
69
-
70
- expect(getJson).toHaveBeenCalledTimes(2);
71
- });
72
- });
73
-
74
- describe('when the pending request fails', () => {
75
- const error = new Error('metadata request failed');
76
-
77
- beforeEach(() => rejectRequest?.(error));
78
-
79
- test('rejects all callers', async () => {
80
- expect(await Promise.allSettled([first, second])).toEqual([
81
- { status: 'rejected', reason: error },
82
- { status: 'rejected', reason: error },
83
- ]);
84
- });
85
-
86
- test('subsequent caller requests metadata again', () => {
87
- subject().catch(() => {});
88
-
89
- expect(getJson).toHaveBeenCalledTimes(2);
90
- });
91
- });
92
- });
93
- });
94
- });
@@ -1,54 +0,0 @@
1
- import { ExposedInstanceDependencies } from '../../common';
2
- import { Metadata } from '../get-bundle-info';
3
- import { getMFEHasCompatibleLaunchDarklyVersion } from '../get-mfe-has-compatible-launchdarkly-version';
4
-
5
- describe(`[web-components] ${getMFEHasCompatibleLaunchDarklyVersion.name}`, () => {
6
- const version = '1.0.0';
7
- let metadata: Pick<Metadata, 'bundledWith'>;
8
- let exposedInstanceDependencies: ExposedInstanceDependencies;
9
-
10
- beforeEach(() => {
11
- metadata = {
12
- bundledWith: { 'launchdarkly-js-client-sdk': version },
13
- };
14
- });
15
-
16
- function itReturns(value: boolean | undefined) {
17
- test(`return ${value}`, () => {
18
- expect(subject()).toBe(value);
19
- });
20
- }
21
-
22
- const subject = () =>
23
- getMFEHasCompatibleLaunchDarklyVersion(metadata.bundledWith, exposedInstanceDependencies);
24
-
25
- describe('when host and MFE use same LaunchDarkly version', () => {
26
- beforeEach(() => (exposedInstanceDependencies = { launchDarkly: { version } }));
27
-
28
- itReturns(true);
29
- });
30
-
31
- describe('when host uses different LaunchDarkly version', () => {
32
- beforeEach(() => (exposedInstanceDependencies = { launchDarkly: { version: '1.0.1' } }));
33
-
34
- itReturns(false);
35
- });
36
-
37
- describe('when host does not use LaunchDarkly', () => {
38
- beforeEach(() => (exposedInstanceDependencies = {}));
39
-
40
- itReturns(undefined);
41
- });
42
-
43
- describe('when MFE does not use LaunchDarkly', () => {
44
- beforeEach(() => (metadata.bundledWith = {}));
45
-
46
- itReturns(undefined);
47
- });
48
-
49
- describe('when metadata omits "bundledWith"', () => {
50
- beforeEach(() => delete metadata.bundledWith);
51
-
52
- itReturns(undefined);
53
- });
54
- });
@@ -1,99 +0,0 @@
1
- import { Metadata } from '../get-bundle-info';
2
- import { getMFESupportsRerendering } from '../get-mfe-supports-rerendering';
3
-
4
- describe(`[web-components] ${getMFESupportsRerendering.name}`, () => {
5
- let metadata: Required<Pick<Metadata, 'bundledWith' | 'dependencies'>>;
6
-
7
- beforeEach(() => {
8
- metadata = {
9
- bundledWith: {},
10
- dependencies: {},
11
- };
12
- });
13
-
14
- function itReturns(value: boolean) {
15
- test(`returns ${value}`, () => {
16
- expect(subject()).toBe(value);
17
- });
18
- }
19
-
20
- function itRaises(error: any) {
21
- test(`raises ${error.name}`, () => {
22
- expect(() => subject()).toThrow(error);
23
- });
24
- }
25
-
26
- function subject() {
27
- return getMFESupportsRerendering(metadata.bundledWith, metadata.dependencies);
28
- }
29
-
30
- itReturns(false);
31
-
32
- const bundledWith: { name: string; version: string; expected: boolean }[] = [
33
- { name: 'startup', version: '22.11.0', expected: false },
34
- { name: 'startup', version: '22.12.0', expected: true },
35
- { name: 'web-components', version: '22.11.0', expected: false },
36
- { name: 'web-components', version: '22.12.0', expected: true },
37
- ];
38
-
39
- describe.each(bundledWith)(
40
- `when bundledWith $name version $version`,
41
- ({ name, version, expected }) => {
42
- beforeEach(() => (metadata.bundledWith[`@servicetitan/${name}`] = version));
43
- itReturns(expected);
44
- }
45
- );
46
-
47
- const dependencies: { name: string; version: string; expected: boolean }[] = [
48
- { name: 'web-components', version: '22.11.0', expected: false },
49
- { name: 'web-components', version: '22.12.0', expected: true },
50
- /*
51
- * The reason we expect this to be false is because we do not know the true version
52
- * of web-components when there is a semver range. We can only assume that it is the
53
- * lowest version in the range.
54
- */
55
- { name: 'web-components', version: '^22.11.0', expected: false },
56
- { name: 'web-components', version: '~22.12.0', expected: true },
57
- ];
58
-
59
- describe.each(dependencies)(
60
- `when dependencies contains $name version $version`,
61
- ({ name, version, expected }) => {
62
- beforeEach(() => (metadata.dependencies[`@servicetitan/${name}`] = version));
63
- test(`returns ${expected}`, () => expect(subject()).toBe(expected));
64
- }
65
- );
66
-
67
- describe('when bundledWith startup version 22.12.0 and dependencies contains web-components 22.11.0', () => {
68
- beforeEach(() => {
69
- metadata = {
70
- bundledWith: { '@servicetitan/startup': '22.12.0' },
71
- dependencies: { '@servicetitan/web-components': '22.11.0' },
72
- };
73
- });
74
- itReturns(false);
75
- });
76
-
77
- describe('when bundledWith startup version 22.12.0 and bundledWith web-components version is 22.11.0', () => {
78
- beforeEach(() => {
79
- metadata = {
80
- bundledWith: {
81
- '@servicetitan/startup': '22.12.0',
82
- '@servicetitan/web-components': '22.11.0',
83
- },
84
- dependencies: {},
85
- };
86
- });
87
- itReturns(false);
88
- });
89
-
90
- const deps: ['bundledWith' | 'dependencies', string][] = [
91
- ['bundledWith', '@servicetitan/web-components'],
92
- ['dependencies', '@servicetitan/web-components'],
93
- ['bundledWith', '@servicetitan/startup'],
94
- ];
95
- describe.each(deps)(`when %s[%s] version is not a semver`, (key, depName) => {
96
- beforeEach(() => (metadata[key][depName] = 'foobar'));
97
- itRaises(TypeError);
98
- });
99
- });
@@ -1,38 +0,0 @@
1
- import type { Metadata } from '../../types';
2
- import { getMFESupportsVersionedLDClient } from '../get-mfe-supports-versioned-ld-client';
3
-
4
- describe(`[web-components] ${getMFESupportsVersionedLDClient.name}`, () => {
5
- let bundledWith: NonNullable<Metadata['bundledWith']>;
6
-
7
- beforeEach(() => {
8
- bundledWith = {};
9
- });
10
-
11
- const subject = () => getMFESupportsVersionedLDClient(bundledWith);
12
-
13
- function itReturns(value: boolean) {
14
- test(`returns ${value}`, () => {
15
- expect(subject()).toBe(value);
16
- });
17
- }
18
-
19
- itReturns(false);
20
-
21
- describe('when bundledWith @servicetitan/launchdarkly-service is below the threshold version', () => {
22
- beforeEach(() => (bundledWith['@servicetitan/launchdarkly-service'] = '39.5.0'));
23
-
24
- itReturns(false);
25
- });
26
-
27
- describe('when bundledWith @servicetitan/launchdarkly-service meets the threshold version', () => {
28
- beforeEach(() => (bundledWith['@servicetitan/launchdarkly-service'] = '40.0.0'));
29
-
30
- itReturns(true);
31
- });
32
-
33
- describe('when bundledWith @servicetitan/launchdarkly-service exceeds the threshold version', () => {
34
- beforeEach(() => (bundledWith['@servicetitan/launchdarkly-service'] = '41.0.0'));
35
-
36
- itReturns(true);
37
- });
38
- });
@@ -1,18 +0,0 @@
1
- import { isVersionedUrl } from '../is-versioned-url';
2
-
3
- describe(`${isVersionedUrl.name}`, () => {
4
- const results: [string, boolean][] = [
5
- ['localhost:9090', false],
6
- ['localhost:9090@1.1.1', false],
7
- ['localhost:9090@dev', false],
8
- ['//unpkg.servicetitan.com/@servicetitan/test', false],
9
- ['//unpkg.servicetitan.com/@servicetitan/test@dev', false],
10
- ['//unpkg.servicetitan.com/@servicetitan/test@0.0.0', true],
11
- ['//unpkg.servicetitan.com/@servicetitan/test@0.0.0-hash', true],
12
- ['//unpkg.servicetitan.com/@servicetitan/test@^16', false],
13
- ];
14
-
15
- test.each(results)(`providing %s returns %s`, (url, result) => {
16
- expect(isVersionedUrl(url)).toEqual(result);
17
- });
18
- });
@@ -1,171 +0,0 @@
1
- import '@testing-library/jest-dom';
2
- import { BundleInfo } from '../get-bundle-info';
3
- import { loadScripts } from '../load-scripts';
4
-
5
- describe('[web-components] loadScripts', () => {
6
- const url = 'https://unpkg.servicetitan.com/mfe/dist/bundle/light/light.js';
7
- const webComponentName = 'my-mfe';
8
-
9
- let bundleInfo: BundleInfo;
10
- let onloadRef: (() => void) | null;
11
- let onerrorRef: (() => void) | null;
12
-
13
- beforeAll(() => {
14
- /*
15
- * mock <script>'s onload/onerror so that we can call them directly from the test,
16
- * because they won't call themselves in the test environment
17
- */
18
- Object.defineProperty(HTMLScriptElement.prototype, 'onload', {
19
- configurable: true,
20
- get() {
21
- return this._onload;
22
- },
23
- set(onload) {
24
- onloadRef = onload;
25
- this._onload = onload;
26
- },
27
- });
28
- Object.defineProperty(HTMLScriptElement.prototype, 'onerror', {
29
- configurable: true,
30
- get() {
31
- return this._onerror;
32
- },
33
- set(onerror) {
34
- onerrorRef = onerror;
35
- this._onerror = onerror;
36
- },
37
- });
38
- });
39
-
40
- beforeEach(() => {
41
- document.querySelectorAll('script').forEach(script => script.remove());
42
- onloadRef = null;
43
- onerrorRef = null;
44
- bundleInfo = {
45
- bundledWith: undefined,
46
- cacheHit: undefined,
47
- mfeSupportsRerendering: false,
48
- mfeHasUniqueChunkNamespace: false,
49
- mfeHasCompatibleLaunchDarklyVersion: false,
50
- mismatch: undefined,
51
- resolvedBundleType: 'light',
52
- urls: { js: [url], css: [] },
53
- WebComponent: webComponentName,
54
- version: undefined,
55
- module: undefined,
56
- };
57
- });
58
-
59
- const subject = () => loadScripts(bundleInfo);
60
-
61
- function scriptTag() {
62
- return document.querySelector(`script[src="${url}"]`) as HTMLScriptElement | null;
63
- }
64
-
65
- test('creates a script tag for the js url', () => {
66
- subject();
67
-
68
- expect(scriptTag()).toBeInTheDocument();
69
- });
70
-
71
- test('sets crossOrigin to anonymous', () => {
72
- subject();
73
-
74
- expect(scriptTag()).toHaveAttribute('crossorigin', 'anonymous');
75
- });
76
-
77
- describe('when the script loads successfully', () => {
78
- const subject = () => {
79
- const promise = loadScripts(bundleInfo);
80
- onloadRef!();
81
- return promise;
82
- };
83
-
84
- test('resolves', async () => {
85
- await expect(subject()).resolves.toEqual([undefined]);
86
- });
87
-
88
- test('marks the script tag with the web component name', async () => {
89
- await subject();
90
-
91
- expect(scriptTag()).toHaveAttribute('data-web-component', webComponentName);
92
- });
93
-
94
- test('unassigns the onload and onerror handlers', async () => {
95
- await subject();
96
-
97
- expect(scriptTag()!.onload).toBeNull();
98
- expect(scriptTag()!.onerror).toBeNull();
99
- });
100
- });
101
-
102
- describe('when the script fails to load', () => {
103
- const subject = () => {
104
- const promise = loadScripts(bundleInfo);
105
- onerrorRef!();
106
- return promise;
107
- };
108
-
109
- test('rejects with a network error', async () => {
110
- await expect(subject()).rejects.toThrow(`Failed to load script: "${url}".`);
111
- });
112
-
113
- test('removes the failed script tag from the DOM', async () => {
114
- await subject().catch(() => {});
115
-
116
- expect(scriptTag()).not.toBeInTheDocument();
117
- });
118
- });
119
-
120
- describe('when a script tag for this bundle already exists without a webComponent data attribute', () => {
121
- let originalOnloadSpy: jest.Mock;
122
- let originalOnerrorSpy: jest.Mock;
123
-
124
- beforeEach(() => {
125
- originalOnloadSpy = jest.fn();
126
- originalOnerrorSpy = jest.fn();
127
-
128
- const script = document.createElement('script');
129
- script.src = url;
130
- script.onload = originalOnloadSpy;
131
- script.onerror = originalOnerrorSpy;
132
- document.body.appendChild(script);
133
- });
134
-
135
- const subject = () => {
136
- const promise = loadScripts(bundleInfo);
137
- onloadRef!();
138
- return promise;
139
- };
140
-
141
- test('calls the original onload handler', async () => {
142
- await subject();
143
-
144
- expect(originalOnloadSpy).toHaveBeenCalled();
145
- });
146
-
147
- describe('when the script fails to load', () => {
148
- const subject = () => {
149
- const promise = loadScripts(bundleInfo);
150
- onerrorRef!();
151
- return promise;
152
- };
153
-
154
- test('calls the original onerror handler', async () => {
155
- await subject().catch(() => {});
156
-
157
- expect(originalOnerrorSpy).toHaveBeenCalled();
158
- });
159
-
160
- test('rejects with a network error', async () => {
161
- await expect(subject()).rejects.toThrow(`Failed to load script: "${url}".`);
162
- });
163
-
164
- test('removes the failed script tag from the DOM', async () => {
165
- await subject().catch(() => {});
166
-
167
- expect(scriptTag()).not.toBeInTheDocument();
168
- });
169
- });
170
- });
171
- });
@@ -1,27 +0,0 @@
1
- describe(`[web-components] supportsPrefetch`, () => {
2
- beforeEach(() => jest.resetModules());
3
-
4
- const subject = () => {
5
- const supportsPrefetch = require('../supports-prefetch').supportsPrefetch;
6
- return supportsPrefetch();
7
- };
8
-
9
- test('returns false', () => {
10
- expect(subject()).toBe(false);
11
- });
12
-
13
- describe('when prefetch is supported', () => {
14
- beforeEach(() => {
15
- const mockLink = {
16
- relList: { supports: (prop: string) => prop === 'prefetch' },
17
- } as any;
18
- jest.spyOn(document, 'createElement').mockImplementation(tag => {
19
- return tag === 'link' ? mockLink : (undefined as any);
20
- });
21
- });
22
-
23
- test('returns true', () => {
24
- expect(subject()).toBe(true);
25
- });
26
- });
27
- });
@@ -1,98 +0,0 @@
1
- import { TtlCache } from '../ttl-cache';
2
-
3
- describe(`${TtlCache.name}`, () => {
4
- const key = 'foo';
5
- const value = 'bar';
6
-
7
- let cache: TtlCache<string>;
8
-
9
- beforeEach(() => {
10
- jest.useFakeTimers();
11
- cache = new TtlCache();
12
- });
13
-
14
- test('cache rejects invalid default ttl', () => {
15
- expect(() => new TtlCache({ ttlMs: -1 })).toThrow();
16
- });
17
-
18
- describe('when cache omits key', () => {
19
- test('has returns false', () => {
20
- expect(cache.has(key)).toBe(false);
21
- });
22
-
23
- test('get returns undefined', () => {
24
- expect(cache.get(key)).toBeUndefined();
25
- });
26
- });
27
-
28
- describe('when cache contains key', () => {
29
- const elements = {
30
- [key]: value,
31
- [`other-${key}`]: `other-${value}`,
32
- };
33
-
34
- beforeEach(() => {
35
- Object.entries(elements).map(([key, value]) => cache.set(key, value));
36
- });
37
-
38
- test('has returns true', () => {
39
- expect(cache.has(key)).toBe(true);
40
- });
41
-
42
- test('get returns value', () => {
43
- expect(cache.get(key)).toBe(value);
44
- });
45
-
46
- test('set replaces value', () => {
47
- const newValue = `new-${value}`;
48
-
49
- cache.set(key, newValue);
50
-
51
- expect(cache.get(key)).toBe(newValue);
52
- });
53
-
54
- test('delete removes key', () => {
55
- cache.delete(key);
56
-
57
- expect(cache.has(key)).toBe(false);
58
- });
59
-
60
- test('clear removes all keys', () => {
61
- cache.clear();
62
-
63
- Object.keys(elements).forEach(key => expect(cache.has(key)).toBe(false));
64
- });
65
-
66
- test('removes keys after five minutes', () => {
67
- jest.advanceTimersByTime(1000 * 60 * 5 - 1);
68
- Object.keys(elements).forEach(key => expect(cache.has(key)).toBe(true));
69
-
70
- jest.advanceTimersByTime(1);
71
- Object.keys(elements).forEach(key => expect(cache.has(key)).toBe(false));
72
- });
73
-
74
- describe('when key has custom ttl', () => {
75
- const ttl = 1000 * 60 * 10;
76
-
77
- beforeEach(() => cache.set(key, value, ttl));
78
-
79
- test('removes key after custom ttl', () => {
80
- jest.advanceTimersByTime(ttl - 1);
81
- expect(cache.has(key)).toBe(true);
82
-
83
- jest.advanceTimersByTime(1);
84
- expect(cache.has(key)).toBe(false);
85
- });
86
- });
87
-
88
- describe('when key has negative ttl', () => {
89
- beforeEach(() => cache.set(key, value, -1));
90
-
91
- test('never removes key', () => {
92
- jest.runOnlyPendingTimers();
93
-
94
- expect(cache.has(key)).toBe(true);
95
- });
96
- });
97
- });
98
- });