|
1 | 1 | import React from 'react'; |
2 | | -import { render } from '@testing-library/react'; |
3 | | -import { Provider } from 'react-redux'; |
4 | | -import configureStore from 'redux-mock-store'; |
5 | | -import thunk from 'redux-thunk'; |
| 2 | +import { fireEvent } from '@testing-library/react'; |
| 3 | +import { createMockNotificationEthSent } from '@metamask/notification-services-controller/notification-services/mocks'; |
| 4 | +import { processNotification } from '@metamask/notification-services-controller/notification-services'; |
| 5 | + |
| 6 | +import * as UseNotificationModule from '../../hooks/metamask-notifications/useNotifications'; |
| 7 | +import { renderWithProvider } from '../../../test/lib/render-helpers'; |
| 8 | +import configureStore from '../../store/store'; |
| 9 | +import mockState from '../../../test/data/mock-state.json'; |
6 | 10 | import { NotificationsListReadAllButton } from './notifications-list-read-all-button'; |
7 | 11 |
|
8 | | -const mockStore = configureStore([thunk]); |
9 | | -const store = mockStore({ |
10 | | - metamask: { |
11 | | - metamaskNotificationsList: [], |
12 | | - }, |
13 | | -}); |
| 12 | +const mockNotification = (isRead: boolean) => { |
| 13 | + const n = processNotification(createMockNotificationEthSent()); |
| 14 | + n.isRead = isRead; |
| 15 | + return n; |
| 16 | +}; |
14 | 17 |
|
15 | 18 | describe('NotificationsListReadAllButton', () => { |
| 19 | + const store = configureStore(mockState); |
| 20 | + |
16 | 21 | it('renders correctly and handles click', () => { |
17 | | - const { getByTestId } = render( |
18 | | - <Provider store={store}> |
19 | | - <NotificationsListReadAllButton notifications={[]} /> |
20 | | - </Provider>, |
| 22 | + const { getByTestId } = renderWithProvider( |
| 23 | + <NotificationsListReadAllButton notifications={[]} />, |
| 24 | + store, |
21 | 25 | ); |
22 | 26 |
|
23 | 27 | const button = getByTestId('notifications-list-read-all-button'); |
24 | 28 | expect(button).toBeInTheDocument(); |
25 | 29 | }); |
| 30 | + |
| 31 | + it('presses and marks all unread notifications as read', async () => { |
| 32 | + const mockMarkAsReadCallback = jest.fn(); |
| 33 | + jest |
| 34 | + .spyOn(UseNotificationModule, 'useMarkNotificationAsRead') |
| 35 | + .mockReturnValue({ markNotificationAsRead: mockMarkAsReadCallback }); |
| 36 | + |
| 37 | + const notificationList = [ |
| 38 | + mockNotification(true), |
| 39 | + mockNotification(false), |
| 40 | + mockNotification(false), |
| 41 | + mockNotification(false), |
| 42 | + mockNotification(true), |
| 43 | + ]; |
| 44 | + |
| 45 | + const { getByTestId } = renderWithProvider( |
| 46 | + <NotificationsListReadAllButton notifications={notificationList} />, |
| 47 | + store, |
| 48 | + ); |
| 49 | + |
| 50 | + const button = getByTestId('notifications-list-read-all-button'); |
| 51 | + fireEvent.click(button); |
| 52 | + |
| 53 | + expect(mockMarkAsReadCallback).toHaveBeenCalled(); |
| 54 | + const mockCall = mockMarkAsReadCallback.mock.lastCall[0]; |
| 55 | + expect(mockCall.length).toBe(3); // 3 unread notifications sent |
| 56 | + }); |
26 | 57 | }); |
0 commit comments