-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathuse-shopper-agent.test.js
More file actions
59 lines (45 loc) · 1.87 KB
/
use-shopper-agent.test.js
File metadata and controls
59 lines (45 loc) · 1.87 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
/*
* 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 {renderHook, act} from '@testing-library/react'
import {useShopperAgent} from '@salesforce/retail-react-app/app/hooks/use-shopper-agent'
import {launchChat} from '@salesforce/retail-react-app/app/utils/shopper-agent-utils'
jest.mock('@salesforce/retail-react-app/app/utils/shopper-agent-utils', () => ({
launchChat: jest.fn()
}))
describe('useShopperAgent', () => {
beforeEach(() => {
jest.clearAllMocks()
})
test('should return an object with actions', () => {
const {result} = renderHook(() => useShopperAgent())
expect(result.current).toEqual({actions: expect.any(Object)})
expect(result.current.actions).toHaveProperty('open')
expect(typeof result.current.actions.open).toBe('function')
})
test('should call launchChat when actions.open is invoked', () => {
const {result} = renderHook(() => useShopperAgent())
act(() => {
result.current.actions.open()
})
expect(launchChat).toHaveBeenCalledTimes(1)
})
test('should call launchChat each time actions.open is invoked', () => {
const {result} = renderHook(() => useShopperAgent())
act(() => {
result.current.actions.open()
result.current.actions.open()
})
expect(launchChat).toHaveBeenCalledTimes(2)
})
test('should return a stable open callback reference across re-renders', () => {
const {result, rerender} = renderHook(() => useShopperAgent())
const firstOpen = result.current.actions.open
rerender()
const secondOpen = result.current.actions.open
expect(firstOpen).toBe(secondOpen)
})
})