@servicetitan/web-components 22.15.0 → 22.16.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.
@@ -0,0 +1,146 @@
1
+ import * as mitt from 'mitt';
2
+ import { renderHook } from '@testing-library/react-hooks';
3
+ import { useEventBus, EventBus } from '../event-bus';
4
+
5
+ jest.mock('mitt', () => jest.fn());
6
+
7
+ describe(`[web-components] ${EventBus.name}`, () => {
8
+ let eventBus: EventBus;
9
+ const type = 'event';
10
+ const handler = jest.fn();
11
+ const mittOn = jest.fn();
12
+ const mittOff = jest.fn();
13
+ const mittEmit = jest.fn();
14
+
15
+ beforeEach(() => {
16
+ jest.spyOn(mitt, 'default').mockImplementation((): any => ({
17
+ on: mittOn,
18
+ off: mittOff,
19
+ emit: mittEmit,
20
+ }));
21
+
22
+ eventBus = new EventBus();
23
+ });
24
+
25
+ describe("when the 'on' method is called", () => {
26
+ const subject = () => eventBus.on(type, handler);
27
+
28
+ test("calls the private mitt instance's 'on' method", () => {
29
+ subject();
30
+ expect(mittOn).toHaveBeenCalledWith(type, handler);
31
+ });
32
+ });
33
+
34
+ describe("when the 'off' method is called", () => {
35
+ const subject = () => eventBus.off(type, handler);
36
+
37
+ test("calls the private mitt instance's 'off' method", () => {
38
+ subject();
39
+ expect(mittOff).toHaveBeenCalledWith(type, handler);
40
+ });
41
+ });
42
+
43
+ describe("when the 'emit' method is called", () => {
44
+ const data = { foo: 'bar' };
45
+ const subject = () => eventBus.emit(type, data);
46
+
47
+ test("calls the private mitt instance's 'emit' method", () => {
48
+ subject();
49
+ expect(mittEmit).toHaveBeenCalledWith(type, data);
50
+ });
51
+ });
52
+
53
+ describe("when job's 'on' method is called", () => {
54
+ const subject = () => eventBus.job.on(type, handler);
55
+
56
+ test("calls the private mitt instance's 'on' method", () => {
57
+ subject();
58
+ expect(mittOn).toHaveBeenCalledWith(type, handler);
59
+ });
60
+ });
61
+
62
+ describe("when job's 'off' method is called", () => {
63
+ const subject = () => eventBus.job.off(type, handler);
64
+
65
+ test("calls the private mitt instance's 'off' method", () => {
66
+ subject();
67
+ expect(mittOff).toHaveBeenCalledWith(type, handler);
68
+ });
69
+ });
70
+
71
+ describe("when job's 'emit' method is called", () => {
72
+ const subject = () => eventBus.job.emit(type);
73
+
74
+ describe("when emit's 'emit' resolves", () => {
75
+ beforeEach(() => {
76
+ mittEmit.mockImplementation((_: any, obj: any) => {
77
+ return obj.resolve();
78
+ });
79
+ });
80
+
81
+ test('the promise resolves', async () => {
82
+ await expect(subject()).resolves.toBeUndefined();
83
+ });
84
+ });
85
+
86
+ describe("when emit's 'emit' rejects", () => {
87
+ const setup = (reason?: any) =>
88
+ mittEmit.mockImplementation((_: any, obj: any) => {
89
+ return obj.reject(reason);
90
+ });
91
+
92
+ describe('with a string reason', () => {
93
+ const reason = 'reason';
94
+
95
+ beforeEach(() => {
96
+ setup(reason);
97
+ });
98
+
99
+ test('the promise rejects with the reason', async () => {
100
+ await expect(subject()).rejects.toBe(reason);
101
+ });
102
+ });
103
+
104
+ describe('with an error', () => {
105
+ const error = Error('reason');
106
+
107
+ beforeEach(() => {
108
+ setup(error);
109
+ });
110
+
111
+ test('the promise rejects with the error', async () => {
112
+ await expect(subject()).rejects.toBe(error);
113
+ });
114
+ });
115
+
116
+ describe('with no argument', () => {
117
+ beforeEach(() => {
118
+ setup();
119
+ });
120
+
121
+ test('the promise rejects', async () => {
122
+ await expect(subject()).rejects.toBeUndefined();
123
+ });
124
+ });
125
+ });
126
+ });
127
+ });
128
+
129
+ describe(`[web-components] ${useEventBus.name}`, () => {
130
+ function subject() {
131
+ return renderHook(() => useEventBus());
132
+ }
133
+
134
+ test('provides an event bus instance', () => {
135
+ expect(subject().result.current).toBeInstanceOf(EventBus);
136
+ });
137
+
138
+ test('when the component using useEventBus is re-rendered, useEventBus provides the same event bus instance', () => {
139
+ const { result, rerender } = subject();
140
+
141
+ rerender();
142
+
143
+ const [firstInstance, secondInstance] = result.all;
144
+ expect(firstInstance).toBe(secondInstance);
145
+ });
146
+ });
@@ -0,0 +1,20 @@
1
+ describe('[web-components] globals', () => {
2
+ function itExportsGlobal(globalName: string) {
3
+ test(`exports the global ${globalName}`, () => {
4
+ jest.isolateModules(() => {
5
+ const globalValue = 'foo';
6
+ Object.assign(global, { [globalName]: globalValue });
7
+ const globalsExport = require('../globals')[globalName];
8
+ expect(globalsExport).toBe(globalValue);
9
+ });
10
+ });
11
+ }
12
+
13
+ describe('EXPOSED_DEPENDENCIES', () => {
14
+ itExportsGlobal('EXPOSED_DEPENDENCIES');
15
+ });
16
+
17
+ describe('WEB_COMPONENT_NAME', () => {
18
+ itExportsGlobal('WEB_COMPONENT_NAME');
19
+ });
20
+ });
@@ -0,0 +1,111 @@
1
+ import { createMemoryHistory, MemoryHistory } from 'history';
2
+ import { HistoryManager } from '../history-manager';
3
+
4
+ describe(`[web-components] ${HistoryManager.name}`, () => {
5
+ let historyManager: HistoryManager;
6
+ let history: MemoryHistory;
7
+
8
+ const url = '/foo';
9
+
10
+ beforeEach(() => {
11
+ jest.useFakeTimers();
12
+ history = createMemoryHistory();
13
+ historyManager = new HistoryManager();
14
+ historyManager.register(history);
15
+ });
16
+
17
+ describe('when user navigates to a new location', () => {
18
+ beforeEach(() => {
19
+ history.push(url);
20
+ });
21
+
22
+ test('navigates to a new location', () => {
23
+ expect(history.location.pathname).toEqual(url);
24
+ });
25
+ });
26
+
27
+ describe('when a second history instance is registered', () => {
28
+ let history2: MemoryHistory;
29
+
30
+ beforeEach(() => {
31
+ history2 = createMemoryHistory();
32
+ historyManager.register(history2);
33
+ });
34
+
35
+ describe('when user navigates to a new location', () => {
36
+ beforeEach(() => {
37
+ history.push(url);
38
+ });
39
+
40
+ test('navigates to a new location in all registered histories', () => {
41
+ expect(history.location.pathname).toEqual(url);
42
+ expect(history2.location.pathname).toEqual(url);
43
+ });
44
+ });
45
+
46
+ describe('when second history is unregistered', () => {
47
+ beforeEach(() => {
48
+ historyManager.unregister(history2);
49
+ });
50
+
51
+ describe('when user navigates to a new location', () => {
52
+ beforeEach(() => {
53
+ history.push(url);
54
+ });
55
+
56
+ test('navigates the first history, but does not navigate second history', () => {
57
+ expect(history.location.pathname).toEqual(url);
58
+ expect(history2.location.pathname).not.toEqual(url);
59
+ });
60
+ });
61
+ });
62
+
63
+ describe('when a redirect is setup to go to a deeper page', () => {
64
+ const urlRedirect = '/foo/bar';
65
+
66
+ beforeEach(() => {
67
+ history2.listen(location => {
68
+ if (location.pathname === url) {
69
+ history2.replace(urlRedirect);
70
+ }
71
+ });
72
+ });
73
+
74
+ describe('when user navigates to a new location', () => {
75
+ beforeEach(() => {
76
+ history.push(url);
77
+ jest.runOnlyPendingTimers();
78
+ });
79
+
80
+ test('redirects to deeper page', () => {
81
+ expect(history.location.pathname).toEqual(urlRedirect);
82
+ expect(history2.location.pathname).toEqual(urlRedirect);
83
+ });
84
+ });
85
+ });
86
+
87
+ describe('when a redirect is setup to go to a page that is not deeper', () => {
88
+ const urlRedirect = '/bar';
89
+
90
+ beforeEach(() => {
91
+ history2.listen(location => {
92
+ if (location.pathname === url) {
93
+ history2.replace(urlRedirect);
94
+ }
95
+ });
96
+ });
97
+
98
+ describe('when user navigates to a new location', () => {
99
+ beforeEach(() => {
100
+ history.push(url);
101
+ jest.runOnlyPendingTimers();
102
+ });
103
+
104
+ test('does not redirect', () => {
105
+ expect(history.location.pathname).toEqual(url);
106
+ expect(history2.location.pathname).toEqual(url);
107
+ });
108
+ });
109
+ });
110
+ });
111
+ });