1 import { render, screen, waitFor } from '@testing-library/react';
2 import userEvent from '@testing-library/user-event';
4 import { FeatureCode } from '@proton/features';
5 import { ALMOST_ALL_MAIL, VIEW_MODE } from '@proton/shared/lib/mail/mailSettings';
14 } from '@proton/testing';
16 import MessagesSection from './MessagesSection';
18 describe('MessagesSection', () => {
19 let mockedApi: jest.Mock;
20 let mockedCall: jest.Mock;
23 mockedApi = jest.fn();
24 mockedCall = jest.fn();
26 mockUseApi(mockedApi);
27 mockUseEventManager({ call: mockedCall });
29 mockUseNotifications();
31 mockUseFlag().mockImplementation((code) => {
32 return code === 'WebMailPageSizeSetting';
36 mockUseMailSettings();
39 describe('AlmostAllMail', () => {
40 describe('when AlmostAllMail is not enabled', () => {
41 it('should not render AlmostAllMail toggle', () => {
42 mockUseFeature().mockImplementation((code) => {
43 if (code === FeatureCode.AlmostAllMail) {
44 return { feature: { Value: false } } as any;
47 return { feature: { Value: true } } as any;
50 render(<MessagesSection />);
52 expect(screen.queryByText('Exclude Spam/Trash from All mail')).not.toBeInTheDocument();
56 describe('when AlmostAllMail is enabled', () => {
58 mockUseFeature({ feature: { Value: true } });
61 it('should render AlmostAllMail toggle', () => {
62 render(<MessagesSection />);
64 expect(screen.getByText('Exclude Spam/Trash from All mail')).toBeInTheDocument();
67 it('should toggle setting on click', async () => {
68 render(<MessagesSection />);
70 const setting = screen.getByText('Exclude Spam/Trash from All mail');
71 await userEvent.click(setting);
74 expect(mockedApi).toHaveBeenCalledTimes(1);
76 expect(mockedApi).toHaveBeenCalledWith({
77 data: { AlmostAllMail: ALMOST_ALL_MAIL.ENABLED },
79 url: 'mail/v4/settings/almost-all-mail',
82 await userEvent.click(setting);
85 expect(mockedApi).toHaveBeenCalledTimes(2);
87 expect(mockedApi).toHaveBeenLastCalledWith({
88 data: { AlmostAllMail: ALMOST_ALL_MAIL.DISABLED },
90 url: 'mail/v4/settings/almost-all-mail',
96 describe('PageSize', () => {
98 mockUseFeature({ feature: { Value: true } });
99 mockUseMailSettings([{ ViewMode: VIEW_MODE.SINGLE }]);
102 describe('when PageSize selection is disabled', () => {
104 mockUseFlag().mockImplementation((code) => {
105 return code !== 'WebMailPageSizeSetting';
109 it('should not display selector', () => {
110 render(<MessagesSection />);
111 expect(screen.queryByText(/Messages per page/)).not.toBeInTheDocument();
112 expect(screen.queryByText(/Conversations per page/)).not.toBeInTheDocument();
116 describe('when PageSize selection is enabled', () => {
117 it('should display correct label', () => {
118 render(<MessagesSection />);
119 expect(screen.getByText(/Messages per page/));
122 describe('when user is in grouped messages mode', () => {
124 mockUseMailSettings([{ ViewMode: VIEW_MODE.GROUP }]);
127 it('should display correct label', () => {
128 render(<MessagesSection />);
130 expect(screen.getByText(/Conversations per page/));
134 describe('when user clicks on an option', () => {
135 const mockedApi = jest.fn();
136 const mockedCall = jest.fn();
137 const mockedCreateNotification = jest.fn();
140 mockUseApi(mockedApi);
141 mockUseEventManager({ call: mockedCall });
142 mockUseNotifications({ createNotification: mockedCreateNotification });
145 it('should display correct label', async () => {
146 render(<MessagesSection />);
148 const select = screen.getByTestId('page-size-selector');
149 await userEvent.click(select);
151 const option2 = screen.getByRole('button', { name: /100/ });
152 await userEvent.click(option2);
154 await waitFor(() => {
155 expect(mockedApi).toHaveBeenCalledTimes(1);
158 expect(mockedApi).toHaveBeenCalledWith({
159 data: { PageSize: 100 },
161 url: 'mail/v4/settings/pagesize',
164 expect(mockedCall).toHaveBeenCalledTimes(1);
166 expect(mockedCreateNotification).toHaveBeenCalledTimes(1);
167 expect(mockedCreateNotification).toHaveBeenCalledWith({ text: 'Preference saved' });