-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy patherror-utils.test.js
More file actions
70 lines (60 loc) · 1.92 KB
/
error-utils.test.js
File metadata and controls
70 lines (60 loc) · 1.92 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
/*
* Copyright (c) 2023, 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 React from 'react'
import {renderHook} from '@testing-library/react'
import {IntlProvider} from 'react-intl'
import {useErrorHandler} from './error-utils'
import PropTypes from 'prop-types'
// Mock the toast hook
const mockToast = jest.fn()
jest.mock('../hooks/use-toast', () => ({
__esModule: true,
default: () => mockToast
}))
// Mock the constants
jest.mock('../constants', () => ({
API_ERROR_MESSAGE: {
id: 'api.error.message',
defaultMessage: 'Something went wrong. Please try again.'
}
}))
// Create a simple wrapper component that provides IntlProvider
const TestWrapper = ({children}) => {
TestWrapper.propTypes = {
children: PropTypes.node.isRequired
}
return (
<IntlProvider locale="en-US" messages={{}}>
{children}
</IntlProvider>
)
}
describe('useErrorHandler', () => {
beforeEach(() => {
mockToast.mockClear()
})
it('returns a showError function', () => {
const {result} = renderHook(() => useErrorHandler(), {
wrapper: TestWrapper
})
expect(typeof result.current).toBe('function')
})
it('calls toast with correct parameters when showError is invoked', () => {
const {result} = renderHook(() => useErrorHandler(), {
wrapper: TestWrapper
})
const showError = result.current
// Call the showError function to cover line 20
showError()
// Verify that toast was called with the correct parameters
expect(mockToast).toHaveBeenCalledTimes(1)
expect(mockToast).toHaveBeenCalledWith({
title: 'Something went wrong. Please try again.',
type: 'error'
})
})
})