|
1 | 1 | import React from 'react'; |
2 | | -import {render, fireEvent, screen} from '@testing-library/react'; |
| 2 | +import {render, fireEvent, screen, waitFor} from '@testing-library/react'; |
3 | 3 | import '@testing-library/jest-dom'; |
4 | 4 | import OutdialCallComponent from '../../../../src/components/task/OutdialCall/outdial-call'; |
| 5 | +import store from '@webex/cc-store'; |
5 | 6 |
|
6 | | -// This test suite is skipped because we have removed the :broken from the command |
7 | | -// line in the package.json scripts to run these tests in pipeline |
8 | | -describe.skip('OutdialCallComponent', () => { |
| 7 | +describe('OutdialCallComponent', () => { |
9 | 8 | const mockStartOutdial = jest.fn(); |
| 9 | + const KEY_LIST = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '*', '0', '#']; |
| 10 | + let customEvent; |
| 11 | + |
| 12 | + // Prevent warning 'CC-Widgets: UI Metrics: No logger found' |
| 13 | + store.store.logger = { |
| 14 | + log: jest.fn(), |
| 15 | + info: jest.fn(), |
| 16 | + warn: jest.fn(), |
| 17 | + error: jest.fn(), |
| 18 | + trace: jest.fn(), |
| 19 | + }; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + jest.clearAllMocks(); |
| 23 | + }); |
10 | 24 |
|
11 | 25 | const props = { |
12 | 26 | startOutdial: mockStartOutdial, |
| 27 | + outdialANIEntries: [ |
| 28 | + {name: 'name 1', number: '1'}, |
| 29 | + {name: 'name 2', number: '2'}, |
| 30 | + ], |
13 | 31 | }; |
14 | 32 |
|
15 | 33 | beforeEach(() => { |
| 34 | + // Create a custom event that mimics what the mdc-input component would fire |
| 35 | + customEvent = new Event('change', {bubbles: true}); |
16 | 36 | mockStartOutdial.mockClear(); |
17 | 37 | }); |
18 | 38 |
|
19 | 39 | it('renders the component correctly', () => { |
20 | 40 | render(<OutdialCallComponent {...props} />); |
21 | | - expect(screen.getByPlaceholderText('Enter number to dial')).toBeInTheDocument(); |
22 | | - expect(screen.getByText('Outdial Call')).toBeInTheDocument(); |
| 41 | + expect(screen.getByTestId('outdial-number-input')).toBeInTheDocument(); |
| 42 | + KEY_LIST.forEach((key) => { |
| 43 | + expect(screen.getByText(key)).toBeInTheDocument(); |
| 44 | + }); |
| 45 | + expect(screen.getByTestId('outdial-ani-option-select')).toBeInTheDocument(); |
| 46 | + expect(screen.getByTestId('outdial-call-button')).toBeInTheDocument(); |
23 | 47 | }); |
24 | 48 |
|
25 | | - it('updates input value when typing directly', () => { |
| 49 | + it('updates input value when typing directly', async () => { |
26 | 50 | render(<OutdialCallComponent {...props} />); |
27 | | - const input = screen.getByPlaceholderText('Enter number to dial'); |
28 | | - fireEvent.change(input, {target: {value: '123'}}); |
29 | | - expect(input).toHaveValue('123'); |
| 51 | + const input = await screen.findByTestId('outdial-number-input'); |
| 52 | + |
| 53 | + Object.defineProperty(customEvent, 'target', { |
| 54 | + writable: false, |
| 55 | + value: {value: '123'}, |
| 56 | + }); |
| 57 | + fireEvent(input, customEvent); |
| 58 | + |
| 59 | + await waitFor(() => { |
| 60 | + expect(input).toHaveAttribute('value', '123'); |
| 61 | + }); |
30 | 62 | }); |
31 | 63 |
|
32 | 64 | it('updates input value when clicking keypad buttons', () => { |
33 | 65 | render(<OutdialCallComponent {...props} />); |
34 | 66 | fireEvent.click(screen.getByText('1')); |
35 | 67 | fireEvent.click(screen.getByText('2')); |
36 | 68 | fireEvent.click(screen.getByText('3')); |
37 | | - expect(screen.getByPlaceholderText('Enter number to dial')).toHaveValue('123'); |
| 69 | + expect(screen.getByTestId('outdial-number-input')).toHaveValue('123'); |
38 | 70 | }); |
39 | 71 |
|
40 | 72 | it('calls startOutdial with correct payload when clicking call button', () => { |
41 | 73 | render(<OutdialCallComponent {...props} />); |
42 | | - const input = screen.getByPlaceholderText('Enter number to dial'); |
43 | | - fireEvent.change(input, {target: {value: '123'}}); |
| 74 | + const input = screen.getByTestId('outdial-number-input'); |
| 75 | + Object.defineProperty(customEvent, 'target', { |
| 76 | + writable: false, |
| 77 | + value: {value: '123'}, |
| 78 | + }); |
| 79 | + fireEvent(input, customEvent); |
44 | 80 |
|
45 | | - const callButton = screen.getByRole('button'); |
| 81 | + const callButton = screen.getByTestId('outdial-call-button'); |
46 | 82 | fireEvent.click(callButton); |
47 | 83 |
|
48 | | - expect(mockStartOutdial).toHaveBeenCalledWith({ |
49 | | - entryPointId: 'test-entry-point', |
50 | | - destination: '123', |
51 | | - direction: 'OUTBOUND', |
52 | | - attributes: {}, |
53 | | - mediaType: 'telephony', |
54 | | - outboundType: 'OUTDIAL', |
55 | | - }); |
| 84 | + expect(mockStartOutdial).toHaveBeenCalledWith('123'); |
56 | 85 | }); |
57 | 86 |
|
58 | 87 | it('allows special characters (* # +) from keypad', () => { |
59 | 88 | render(<OutdialCallComponent {...props} />); |
60 | 89 | fireEvent.click(screen.getByText('*')); |
61 | 90 | fireEvent.click(screen.getByText('#')); |
62 | | - expect(screen.getByPlaceholderText('Enter number to dial')).toHaveValue('*#'); |
| 91 | + expect(screen.getByTestId('outdial-number-input')).toHaveValue('*#'); |
63 | 92 | }); |
64 | 93 |
|
65 | | - it('does not allow invalid characters', () => { |
| 94 | + it('shows error help text when invalid characters are entered', async () => { |
66 | 95 | render(<OutdialCallComponent {...props} />); |
67 | | - const input = screen.getByPlaceholderText('Enter number to dial'); |
68 | | - fireEvent.change(input, {target: {value: 'abc'}}); |
69 | | - expect(input).toHaveValue(''); |
| 96 | + const input = await screen.findByTestId('outdial-number-input'); |
| 97 | + Object.defineProperty(customEvent, 'target', { |
| 98 | + writable: false, |
| 99 | + value: {value: 'abc'}, |
| 100 | + }); |
| 101 | + fireEvent(input, customEvent); |
| 102 | + await waitFor(() => expect(input).toHaveAttribute('help-text', 'Incorrect format.')); |
70 | 103 | }); |
71 | 104 |
|
72 | | - it('does not allow invalid characters when typing', () => { |
| 105 | + it('does not allow invalid characters when typing', async () => { |
73 | 106 | render(<OutdialCallComponent {...props} />); |
74 | | - const input = screen.getByPlaceholderText('Enter number to dial'); |
75 | | - fireEvent.change(input, {target: {value: '123abc'}}); |
76 | | - expect(input).toHaveValue('123'); |
| 107 | + const input = await screen.findByTestId('outdial-number-input'); |
| 108 | + Object.defineProperty(customEvent, 'target', { |
| 109 | + writable: false, |
| 110 | + value: {value: '123abc'}, |
| 111 | + }); |
| 112 | + fireEvent(input, customEvent); |
| 113 | + await waitFor(() => expect(input).toHaveAttribute('help-text', 'Incorrect format.')); |
77 | 114 | }); |
78 | 115 |
|
79 | | - it('does not allow empty input', () => { |
| 116 | + it('has no ANI entry options when the entry list is empty', async () => { |
| 117 | + render(<OutdialCallComponent startOutdial={mockStartOutdial} outdialANIEntries={[]} />); |
| 118 | + const select = await screen.findByTestId('outdial-ani-option-select'); |
| 119 | + fireEvent.click(select); |
| 120 | + expect(screen.queryByText('name 1')).not.toBeInTheDocument(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('sets selected ani when an option is selected', async () => { |
80 | 124 | render(<OutdialCallComponent {...props} />); |
81 | | - const callButton = screen.getByRole('button'); |
82 | | - fireEvent.click(callButton); |
83 | | - expect(mockStartOutdial).not.toHaveBeenCalled(); |
| 125 | + const select = await screen.findByTestId('outdial-ani-option-select'); |
| 126 | + fireEvent.click(select); |
| 127 | + const option = await screen.findByText('name 1'); |
| 128 | + expect(option).toBeInTheDocument(); |
| 129 | + fireEvent.click(option); |
| 130 | + await waitFor(() => { |
| 131 | + expect(option).toHaveAttribute('aria-selected', 'true'); |
| 132 | + }); |
84 | 133 | }); |
85 | 134 |
|
86 | | - it('should remove whitespace and only keep numbers', () => { |
| 135 | + it('does not allow empty input', async () => { |
87 | 136 | render(<OutdialCallComponent {...props} />); |
88 | | - const input = screen.getByPlaceholderText('Enter number to dial'); |
89 | | - fireEvent.change(input, {target: {value: ' 1 2 3 4 '}}); |
90 | | - expect(input).toHaveValue('1234'); |
| 137 | + const callButton = await screen.findByTestId('outdial-call-button'); |
| 138 | + expect(callButton).toBeDisabled(); |
91 | 139 | }); |
92 | 140 | }); |
0 commit comments