|
| 1 | +import { screen } from '@testing-library/react' |
| 2 | +import userEvent from '@testing-library/user-event' |
| 3 | +import { Ticker } from '../../api/Ticker' |
| 4 | +import { renderWithProviders, setMockToken, userToken } from '../../tests/utils' |
| 5 | +import MatrixCard from './MatrixCard' |
| 6 | + |
| 7 | +describe('MatrixCard', () => { |
| 8 | + beforeEach(() => { |
| 9 | + setMockToken(userToken) |
| 10 | + fetchMock.resetMocks() |
| 11 | + }) |
| 12 | + |
| 13 | + const ticker = ({ active, connected, roomName = '' }: { active: boolean; connected: boolean; roomName?: string }) => { |
| 14 | + return { |
| 15 | + id: 1, |
| 16 | + matrix: { |
| 17 | + active: active, |
| 18 | + connected: connected, |
| 19 | + roomName: roomName, |
| 20 | + }, |
| 21 | + } as Ticker |
| 22 | + } |
| 23 | + |
| 24 | + const component = ({ ticker }: { ticker: Ticker }) => { |
| 25 | + return <MatrixCard ticker={ticker} /> |
| 26 | + } |
| 27 | + |
| 28 | + it('should render the component', () => { |
| 29 | + renderWithProviders(component({ ticker: ticker({ active: false, connected: false }) })) |
| 30 | + |
| 31 | + expect(screen.getByText('Matrix')).toBeInTheDocument() |
| 32 | + expect(screen.getByText('You are not connected with Matrix.')).toBeInTheDocument() |
| 33 | + expect(screen.getByRole('button', { name: 'Add' })).toBeInTheDocument() |
| 34 | + }) |
| 35 | + |
| 36 | + it('should render the component when connected and active', async () => { |
| 37 | + renderWithProviders(component({ ticker: ticker({ active: true, connected: true, roomName: '#room:matrix.org' }) })) |
| 38 | + |
| 39 | + expect(screen.getByText('Matrix')).toBeInTheDocument() |
| 40 | + expect(screen.getByText('You are connected with Matrix.')).toBeInTheDocument() |
| 41 | + expect(screen.getByText('Your Room:')).toBeInTheDocument() |
| 42 | + expect(screen.getByRole('link', { name: '#room:matrix.org' })).toBeInTheDocument() |
| 43 | + expect(screen.getByRole('button', { name: 'Delete' })).toBeInTheDocument() |
| 44 | + expect(screen.getByRole('button', { name: 'Disable' })).toBeInTheDocument() |
| 45 | + |
| 46 | + fetchMock.mockResponseOnce(JSON.stringify({ status: 'success' })) |
| 47 | + |
| 48 | + await userEvent.click(screen.getByRole('button', { name: 'Disable' })) |
| 49 | + |
| 50 | + expect(fetchMock).toHaveBeenCalledTimes(1) |
| 51 | + expect(fetchMock).toHaveBeenCalledWith('http://localhost:8080/v1/admin/tickers/1/matrix', { |
| 52 | + body: JSON.stringify({ active: false }), |
| 53 | + headers: { |
| 54 | + Accept: 'application/json', |
| 55 | + 'Content-Type': 'application/json', |
| 56 | + Authorization: 'Bearer ' + userToken, |
| 57 | + }, |
| 58 | + method: 'put', |
| 59 | + }) |
| 60 | + }) |
| 61 | + |
| 62 | + it('should handle add button click', async () => { |
| 63 | + renderWithProviders(component({ ticker: ticker({ active: false, connected: false }) })) |
| 64 | + |
| 65 | + fetchMock.mockResponseOnce(JSON.stringify({ status: 'success' })) |
| 66 | + |
| 67 | + await userEvent.click(screen.getByRole('button', { name: 'Add' })) |
| 68 | + |
| 69 | + expect(fetchMock).toHaveBeenCalledTimes(1) |
| 70 | + expect(fetchMock).toHaveBeenCalledWith('http://localhost:8080/v1/admin/tickers/1/matrix', { |
| 71 | + body: JSON.stringify({ active: true }), |
| 72 | + headers: { |
| 73 | + Accept: 'application/json', |
| 74 | + 'Content-Type': 'application/json', |
| 75 | + Authorization: 'Bearer ' + userToken, |
| 76 | + }, |
| 77 | + method: 'put', |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + it('should handle delete with dialog', async () => { |
| 82 | + renderWithProviders(component({ ticker: ticker({ active: true, connected: true, roomName: '#room:matrix.org' }) })) |
| 83 | + |
| 84 | + await userEvent.click(screen.getByRole('button', { name: 'Delete' })) |
| 85 | + |
| 86 | + expect(screen.getByRole('dialog')).toBeInTheDocument() |
| 87 | + expect(screen.getByText('Delete Matrix integration')).toBeInTheDocument() |
| 88 | + |
| 89 | + fetchMock.mockResponseOnce(JSON.stringify({ status: 'success' })) |
| 90 | + |
| 91 | + await userEvent.click(screen.getByTestId('dialog-delete')) |
| 92 | + |
| 93 | + expect(fetchMock).toHaveBeenCalledTimes(1) |
| 94 | + expect(fetchMock).toHaveBeenCalledWith('http://localhost:8080/v1/admin/tickers/1/matrix', { |
| 95 | + headers: { |
| 96 | + Accept: 'application/json', |
| 97 | + 'Content-Type': 'application/json', |
| 98 | + Authorization: 'Bearer ' + userToken, |
| 99 | + }, |
| 100 | + method: 'delete', |
| 101 | + }) |
| 102 | + }) |
| 103 | + |
| 104 | + it('should handle enable when inactive', async () => { |
| 105 | + renderWithProviders(component({ ticker: ticker({ active: false, connected: true, roomName: '#room:matrix.org' }) })) |
| 106 | + |
| 107 | + expect(screen.getByRole('button', { name: 'Enable' })).toBeInTheDocument() |
| 108 | + |
| 109 | + fetchMock.mockResponseOnce(JSON.stringify({ status: 'success' })) |
| 110 | + |
| 111 | + await userEvent.click(screen.getByRole('button', { name: 'Enable' })) |
| 112 | + |
| 113 | + expect(fetchMock).toHaveBeenCalledTimes(1) |
| 114 | + expect(fetchMock).toHaveBeenCalledWith('http://localhost:8080/v1/admin/tickers/1/matrix', { |
| 115 | + body: JSON.stringify({ active: true }), |
| 116 | + headers: { |
| 117 | + Accept: 'application/json', |
| 118 | + 'Content-Type': 'application/json', |
| 119 | + Authorization: 'Bearer ' + userToken, |
| 120 | + }, |
| 121 | + method: 'put', |
| 122 | + }) |
| 123 | + }) |
| 124 | + |
| 125 | + it('should fail when response fails', async () => { |
| 126 | + renderWithProviders(component({ ticker: ticker({ active: true, connected: true, roomName: '#room:matrix.org' }) })) |
| 127 | + |
| 128 | + fetchMock.mockResponseOnce(JSON.stringify({ status: 'error' })) |
| 129 | + |
| 130 | + await userEvent.click(screen.getByRole('button', { name: 'Disable' })) |
| 131 | + |
| 132 | + expect(fetchMock).toHaveBeenCalledTimes(1) |
| 133 | + }) |
| 134 | + |
| 135 | + it('should fail when request fails', async () => { |
| 136 | + renderWithProviders(component({ ticker: ticker({ active: true, connected: true, roomName: '#room:matrix.org' }) })) |
| 137 | + |
| 138 | + fetchMock.mockReject() |
| 139 | + |
| 140 | + await userEvent.click(screen.getByRole('button', { name: 'Disable' })) |
| 141 | + |
| 142 | + expect(fetchMock).toHaveBeenCalledTimes(1) |
| 143 | + }) |
| 144 | +}) |
0 commit comments