<commit_before>import { RenderHookResult } from '@testing-library/react-hooks';
import { range } from 'proton-shared/lib/helpers/array';
import { queryConversations } from 'proton-shared/lib/api/conversations';

import { useElements } from './useElements';
import { Element } from '../models/element';
import { Page, Sort, Filter, SearchParameters } from '../models/tools';
import { renderHook, clearAll, addApiMock, api } from '../helpers/test/helper';
import { ConversationLabel } from '../models/conversation';

interface SetupArgs {
    elements?: Element[];
    total?: number;
    conversationMode?: boolean;
    inputLabelID?: string;
    page?: Page;
    sort?: Sort;
    filter?: Filter;
    search?: SearchParameters;
}

describe('useElements', () => {
    const labelID = 'labelID';
    const element1 = { ID: 'id1', Labels: [{ ID: labelID, ContextTime: 1 }], LabelIDs: [labelID], Size: 20 } as Element;
    const element2 = { ID: 'id2', Labels: [{ ID: labelID, ContextTime: 2 }], LabelIDs: [labelID], Size: 10 } as Element;
    const element3 = {
        ID: 'id3',
        Labels: [{ ID: 'otherLabelID', ContextTime: 3 }],
        LabelIDs: ['otherLabelID']
    } as Element;

    const getElements = (count: number, label = labelID): Element[] =>
        range(0, count).map((i) => ({
            ID: `id${i}`,
            Labels: [{ ID: label, ContextTime: i }] as ConversationLabel[],
            LabelIDs: [label]
        }));

    let renderHookResult: RenderHookResult<any, any> | null = null;

    const setup = async ({
        elements = [],
        conversationMode = true,
        inputLabelID = labelID,
        page = { page: 0, size: 50, limit: 50, total: elements.length },
        sort = { sort: 'Time', desc: true },
        filter = {},
        search = {}
    }: SetupArgs = {}) => {
        addApiMock('conversations', () => ({ Total: page.total, Conversations: elements }));

        if (renderHookResult === null) {
            renderHookResult = renderHook((props: any = {}) =>
                useElements({ conversationMode, labelID: inputLabelID, page, sort, filter, search, ...props })
            );
        } else {
            renderHookResult.rerender({ conversationMode, labelID: inputLabelID, page, sort, filter });
        }

        await renderHookResult.waitForNextUpdate();
        return renderHookResult;
    };

    afterEach(() => {
        renderHookResult = null;
        // [(useApi as jest.Mock, api)].forEach((mock) => mock.mockClear());
        clearAll();
    });

    describe('elements memo', () => {
        it.only('should order by label context time', async () => {
            const result = await setup({ elements: [element1, element2] });
            const [, elements] = result.result.current;
            expect(elements).toEqual([element2, element1]);
        });

        it('should filter message with the right label', async () => {
            const result = await setup({ elements: [element1, element2, element3] });
            const [, elements] = result.result.current;
            expect(elements.length).toBe(2);
        });

        it('should limit to the page size', async () => {
            const page: Page = { page: 0, size: 5, limit: 5, total: 15 };
            const result = await setup({ elements: getElements(page.total), page });
            const [, elements] = result.result.current;
            expect(elements.length).toBe(page.size);
        });

        it('should returns the current page', async () => {
            const page: Page = { page: 0, size: 5, limit: 5, total: 7 };
            const allElements = getElements(page.total);

            const result = await setup({ elements: allElements.slice(0, page.size), page });

            page.page = 1;
            await setup({ elements: allElements.slice(page.size), page });

            const [, elements] = result.result.current;
            expect(elements.length).toBe(page.total - page.size);
        });<commit_msg>Hotfix: remove forgotten it.only<commit_after>import { RenderHookResult } from '@testing-library/react-hooks';
import { range } from 'proton-shared/lib/helpers/array';
import { queryConversations } from 'proton-shared/lib/api/conversations';

import { useElements } from './useElements';
import { Element } from '../models/element';
import { Page, Sort, Filter, SearchParameters } from '../models/tools';
import { renderHook, clearAll, addApiMock, api } from '../helpers/test/helper';
import { ConversationLabel } from '../models/conversation';

interface SetupArgs {
    elements?: Element[];
    total?: number;
    conversationMode?: boolean;
    inputLabelID?: string;
    page?: Page;
    sort?: Sort;
    filter?: Filter;
    search?: SearchParameters;
}

describe('useElements', () => {
    const labelID = 'labelID';
    const element1 = { ID: 'id1', Labels: [{ ID: labelID, ContextTime: 1 }], LabelIDs: [labelID], Size: 20 } as Element;
    const element2 = { ID: 'id2', Labels: [{ ID: labelID, ContextTime: 2 }], LabelIDs: [labelID], Size: 10 } as Element;
    const element3 = {
        ID: 'id3',
        Labels: [{ ID: 'otherLabelID', ContextTime: 3 }],
        LabelIDs: ['otherLabelID']
    } as Element;

    const getElements = (count: number, label = labelID): Element[] =>
        range(0, count).map((i) => ({
            ID: `id${i}`,
            Labels: [{ ID: label, ContextTime: i }] as ConversationLabel[],
            LabelIDs: [label]
        }));

    let renderHookResult: RenderHookResult<any, any> | null = null;

    const setup = async ({
        elements = [],
        conversationMode = true,
        inputLabelID = labelID,
        page = { page: 0, size: 50, limit: 50, total: elements.length },
        sort = { sort: 'Time', desc: true },
        filter = {},
        search = {}
    }: SetupArgs = {}) => {
        addApiMock('conversations', () => ({ Total: page.total, Conversations: elements }));

        if (renderHookResult === null) {
            renderHookResult = renderHook((props: any = {}) =>
                useElements({ conversationMode, labelID: inputLabelID, page, sort, filter, search, ...props })
            );
        } else {
            renderHookResult.rerender({ conversationMode, labelID: inputLabelID, page, sort, filter });
        }

        await renderHookResult.waitForNextUpdate();
        return renderHookResult;
    };

    afterEach(() => {
        renderHookResult = null;
        // [(useApi as jest.Mock, api)].forEach((mock) => mock.mockClear());
        clearAll();
    });

    describe('elements memo', () => {
        it('should order by label context time', async () => {
            const result = await setup({ elements: [element1, element2] });
            const [, elements] = result.result.current;
            expect(elements).toEqual([element2, element1]);
        });

        it('should filter message with the right label', async () => {
            const result = await setup({ elements: [element1, element2, element3] });
            const [, elements] = result.result.current;
            expect(elements.length).toBe(2);
        });

        it('should limit to the page size', async () => {
            const page: Page = { page: 0, size: 5, limit: 5, total: 15 };
            const result = await setup({ elements: getElements(page.total), page });
            const [, elements] = result.result.current;
            expect(elements.length).toBe(page.size);
        });

        it('should returns the current page', async () => {
            const page: Page = { page: 0, size: 5, limit: 5, total: 7 };
            const allElements = getElements(page.total);

            const result = await setup({ elements: allElements.slice(0, page.size), page });

            page.page = 1;
            await setup({ elements: allElements.slice(page.size), page });

            const [, elements] = result.result.current;
            expect(elements.length).toBe(page.total - page.size);
        });
{'fraction_non_alphanumeric': 0.15136694256333083, 'fraction_numerical': 0.009530975670930524, 'mean_word_length': 2.5101232394366195, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '6883085', 'n_tokens_mistral': 2252, 'n_tokens_neox': 2199, 'n_words': 613}