-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathshopper-agent-utils.test.js
More file actions
233 lines (188 loc) · 7.14 KB
/
shopper-agent-utils.test.js
File metadata and controls
233 lines (188 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* Copyright (c) 2025, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import {
launchChat,
openShopperAgent
} from '@salesforce/retail-react-app/app/utils/shopper-agent-utils'
describe('shopper-agent-utils', () => {
let originalWindow
let consoleErrorSpy
beforeEach(() => {
// Save original window
originalWindow = global.window
// Mock console methods
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {})
})
afterEach(() => {
// Restore console methods
consoleErrorSpy.mockRestore()
jest.clearAllMocks()
// Restore window after clearing mocks
global.window = originalWindow
})
describe('launchChat', () => {
test('should return early if not on client side', () => {
delete global.window
const result = launchChat()
expect(result).toBeUndefined()
})
test('should launch chat when embeddedservice_bootstrap is available', () => {
const mockLaunchChat = jest.fn()
global.window = {
embeddedservice_bootstrap: {
utilAPI: {
launchChat: mockLaunchChat
}
}
}
launchChat()
expect(mockLaunchChat).toHaveBeenCalledTimes(1)
})
test('should not launch chat when embeddedservice_bootstrap is missing', () => {
global.window = {}
expect(() => launchChat()).not.toThrow()
})
test('should not launch chat when utilAPI is missing', () => {
global.window = {
embeddedservice_bootstrap: {}
}
expect(() => launchChat()).not.toThrow()
})
test('should not launch chat when launchChat is not a function', () => {
global.window = {
embeddedservice_bootstrap: {
utilAPI: {
launchChat: 'not a function'
}
}
}
expect(() => launchChat()).not.toThrow()
})
test('should call showChatButton before launchChat when hideChatButtonOnLoad is true', () => {
const mockShowChatButton = jest.fn()
const mockLaunchChat = jest.fn()
global.window = {
embeddedservice_bootstrap: {
settings: {
hideChatButtonOnLoad: true
},
utilAPI: {
showChatButton: mockShowChatButton,
launchChat: mockLaunchChat
}
}
}
launchChat()
expect(mockShowChatButton).toHaveBeenCalledTimes(1)
expect(mockLaunchChat).toHaveBeenCalledTimes(1)
// showChatButton must be called before launchChat
expect(mockShowChatButton.mock.invocationCallOrder[0]).toBeLessThan(
mockLaunchChat.mock.invocationCallOrder[0]
)
})
test('should not call showChatButton when hideChatButtonOnLoad is false', () => {
const mockShowChatButton = jest.fn()
const mockLaunchChat = jest.fn()
global.window = {
embeddedservice_bootstrap: {
settings: {
hideChatButtonOnLoad: false
},
utilAPI: {
showChatButton: mockShowChatButton,
launchChat: mockLaunchChat
}
}
}
launchChat()
expect(mockShowChatButton).not.toHaveBeenCalled()
expect(mockLaunchChat).toHaveBeenCalledTimes(1)
})
test('should handle errors and log error', () => {
const mockLaunchChat = jest.fn(() => {
throw new Error('Launch error')
})
global.window = {
embeddedservice_bootstrap: {
utilAPI: {
launchChat: mockLaunchChat
}
}
}
launchChat()
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Shopper Agent: Error launching chat',
expect.any(Error)
)
})
})
describe('openShopperAgent', () => {
test('should return early if not on client side', () => {
delete global.window
const result = openShopperAgent()
expect(result).toBeUndefined()
})
test('should call launchChat', () => {
const mockLaunchChat = jest.fn()
global.window = {
embeddedservice_bootstrap: {
utilAPI: {
launchChat: mockLaunchChat
}
}
}
openShopperAgent()
expect(mockLaunchChat).toHaveBeenCalledTimes(1)
})
test('should handle errors from launchChat and log error', () => {
global.window = {
embeddedservice_bootstrap: {
utilAPI: {
launchChat: jest.fn(() => {
throw new Error('Launch error')
})
}
}
}
openShopperAgent()
// launchChat catches its own error and logs "Error launching chat"
// Since launchChat handles the error internally, openShopperAgent's try-catch
// doesn't catch it, so we only see the launchChat error log
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Shopper Agent: Error launching chat',
expect.any(Error)
)
})
test('should handle errors when launchChat throws and openShopperAgent catches it', () => {
// Simulate a scenario where launchChat itself throws an error
// that isn't caught internally (though in practice launchChat has try-catch)
const mockLaunchChat = jest.fn(() => {
throw new Error('Unexpected error')
})
global.window = {
embeddedservice_bootstrap: {
utilAPI: {
launchChat: mockLaunchChat
}
}
}
openShopperAgent()
// launchChat should have been called
expect(mockLaunchChat).toHaveBeenCalled()
// launchChat's internal try-catch should handle the error
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Shopper Agent: Error launching chat',
expect.any(Error)
)
})
test('should handle errors when embeddedservice_bootstrap is missing', () => {
global.window = {}
// Should not throw, launchChat handles missing bootstrap gracefully
expect(() => openShopperAgent()).not.toThrow()
})
})
})