<commit_before>import { act } from '@testing-library/react';

import { EVENT_ACTIONS } from '@proton/shared/lib/constants';
import { Message } from '@proton/shared/lib/interfaces/mail/Message';

import { DEFAULT_PLACEHOLDERS_COUNT, PAGE_SIZE } from '../../../constants';
import { addApiResolver, addToCache, api, clearAll, render } from '../../../helpers/test/helper';
import { Conversation } from '../../../models/conversation';
import { MessageEvent } from '../../../models/event';
import MailboxContainer from '../MailboxContainer';
import { baseApiMocks, expectElements, getElements, getProps, props, sendEvent, setup } from './Mailbox.test.helpers';

describe('Mailbox elements list reacting to events', () => {
    const { labelID } = props;

    beforeEach(clearAll);

    it('should add to the cache a message which is not existing yet', async () => {
        // Usefull to receive incoming mail or draft without having to reload the list

        const total = 3;
        const { getItems } = await setup({ conversations: getElements(total) });

        const element = { ID: 'id3', Labels: [{ ID: labelID }], LabelIDs: [labelID] };
        await sendEvent({
            ConversationCounts: [{ LabelID: labelID, Total: total + 1, Unread: 0 }],
            Conversations: [{ ID: element.ID, Action: EVENT_ACTIONS.CREATE, Conversation: element as Conversation }],
        });

        expectElements(getItems, total + 1, false);
    });

    it('should not add to the cache a message which is not existing when a search is active', async () => {
        // When a search is active, all the cache will be shown, we can't accept any updated message
        // But we will refresh the request each time

        const total = 3;
        const search = { keyword: 'test' };
        const { getItems } = await setup({ messages: getElements(total), search });

        const message = { ID: 'id3', Labels: [{ ID: labelID }], LabelIDs: [labelID] } as any as Message;
        await sendEvent({
            Messages: [{ ID: message.ID, Action: EVENT_ACTIONS.CREATE, Message: message }],
        });

        expectElements(getItems, total, false);
        expect(api.mock.calls.length).toBe(8);
    });

    it('should not reload the list on an update event if a filter is active', async () => {
        const total = 3;
        const filter = { Unread: 1 };
        const conversations = getElements(total, labelID, { NumUnread: 1 });

        const { getItems } = await setup({ conversations, filter });

        const ID = 'id0';
        await sendEvent({
            Conversations: [{ ID, Action: EVENT_ACTIONS.UPDATE, Conversation: { ID } as Conversation }],
        });

        expectElements(getItems, total, false);
        expect(api.mock.calls.length).toBe(7);
    });

    it('should not reload the list on an update event if has list from start', async () => {
        const total = 3;
        const { getItems } = await setup({ conversations: getElements(total) });

        const ID = 'id0';
        await sendEvent({
            Conversations: [{ ID, Action: EVENT_ACTIONS.UPDATE, Conversation: { ID } as Conversation }],
        });

        expectElements(getItems, total, false);
        expect(api.mock.calls.length).toBe(7);
    });

    it('should reload the list on an update event if has not list from start', async () => {
        const page = 2;
        const total = PAGE_SIZE * 6 + 2;
        const { getItems } = await setup({
            conversations: getElements(PAGE_SIZE),
            page,
            totalConversations: total,
        });

        const ID = 'id0';
        await sendEvent({
            Conversations: [{ ID, Action: EVENT_ACTIONS.UPDATE, Conversation: { ID } as Conversation }],
        });

        expectElements(getItems, PAGE_SIZE, false);
        expect(api.mock.calls.length).toBe(8);
    });

    it('should reload the list on an delete event if a search is active', async () => {
        const total = 3;
        const search = { keyword: '<PASSWORD>' };
        const { getItems } = await setup({ messages: getElements(total), search });

        const ID = 'id10';
        await sendEvent({
            Messages: [{ ID, Action: EVENT_ACTIONS.DELETE, Message: { ID } as Message }],
        });

        expectElements(getItems, total, false);
        expect(api.mock.calls.length).toBe(8);
    });

    it('should not reload the list on count event when a search is active', async () => {
        // If a search is active, the expected length computation has no meaning<commit_msg>Review test<commit_after>import { act } from '@testing-library/react';

import { EVENT_ACTIONS } from '@proton/shared/lib/constants';
import { Message } from '@proton/shared/lib/interfaces/mail/Message';

import { DEFAULT_PLACEHOLDERS_COUNT, PAGE_SIZE } from '../../../constants';
import { addApiResolver, addToCache, api, clearAll, render } from '../../../helpers/test/helper';
import { Conversation } from '../../../models/conversation';
import { MessageEvent } from '../../../models/event';
import MailboxContainer from '../MailboxContainer';
import { baseApiMocks, expectElements, getElements, getProps, props, sendEvent, setup } from './Mailbox.test.helpers';

describe('Mailbox elements list reacting to events', () => {
    const { labelID } = props;

    beforeEach(clearAll);

    it('should add to the cache a message which is not existing yet', async () => {
        // Usefull to receive incoming mail or draft without having to reload the list

        const total = 3;
        const { getItems } = await setup({ conversations: getElements(total) });

        const element = { ID: 'id3', Labels: [{ ID: labelID }], LabelIDs: [labelID] };
        await sendEvent({
            ConversationCounts: [{ LabelID: labelID, Total: total + 1, Unread: 0 }],
            Conversations: [{ ID: element.ID, Action: EVENT_ACTIONS.CREATE, Conversation: element as Conversation }],
        });

        expectElements(getItems, total + 1, false);
    });

    it('should not add to the cache a message which is not existing when a search is active', async () => {
        // When a search is active, all the cache will be shown, we can't accept any updated message
        // But we will refresh the request each time

        const total = 3;
        const search = { keyword: 'test' };
        const { getItems } = await setup({ messages: getElements(total), search });

        const message = { ID: 'id3', Labels: [{ ID: labelID }], LabelIDs: [labelID] } as any as Message;
        await sendEvent({
            Messages: [{ ID: message.ID, Action: EVENT_ACTIONS.CREATE, Message: message }],
        });

        expectElements(getItems, total, false);
        expect(api.mock.calls.length).toBe(8);
    });

    it('should not reload the list on an update event if a filter is active', async () => {
        const total = 3;
        const filter = { Unread: 1 };
        const conversations = getElements(total, labelID, { NumUnread: 1 });

        const { getItems } = await setup({ conversations, filter });

        const ID = 'id0';
        await sendEvent({
            Conversations: [{ ID, Action: EVENT_ACTIONS.UPDATE, Conversation: { ID } as Conversation }],
        });

        expectElements(getItems, total, false);
        expect(api.mock.calls.length).toBe(10);
    });

    it('should not reload the list on an update event if has list from start', async () => {
        const total = 3;
        const { getItems } = await setup({ conversations: getElements(total) });

        const ID = 'id0';
        await sendEvent({
            Conversations: [{ ID, Action: EVENT_ACTIONS.UPDATE, Conversation: { ID } as Conversation }],
        });

        expectElements(getItems, total, false);
        expect(api.mock.calls.length).toBe(10);
    });

    it('should reload the list on an update event if has not list from start', async () => {
        const page = 2;
        const total = PAGE_SIZE * 6 + 2;
        const { getItems } = await setup({
            conversations: getElements(PAGE_SIZE),
            page,
            totalConversations: total,
        });

        const ID = 'id0';
        await sendEvent({
            Conversations: [{ ID, Action: EVENT_ACTIONS.UPDATE, Conversation: { ID } as Conversation }],
        });

        expectElements(getItems, PAGE_SIZE, false);
        expect(api.mock.calls.length).toBe(18);
    });

    it('should reload the list on an delete event if a search is active', async () => {
        const total = 3;
        const search = { keyword: 'test' };
        const { getItems } = await setup({ messages: getElements(total), search });

        const ID = 'id10';
        await sendEvent({
            Messages: [{ ID, Action: EVENT_ACTIONS.DELETE, Message: { ID } as Message }],
        });

        expectElements(getItems, total, false);
        expect(api.mock.calls.length).toBe(8);
    });

    it('should not reload the list on count event when a search is active', async () => {
        // If a search is active, the expected length computation has no meaning
{'fraction_non_alphanumeric': 0.12972139632199098, 'fraction_numerical': 0.005836361634181258, 'mean_word_length': 2.581230283911672, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6274315', 'n_tokens_mistral': 2514, 'n_tokens_neox': 2456, 'n_words': 838}