import { describe, expect, it, beforeEach, beforeAll } from 'vitest';
import { createWrapper } from 'app/test/vitest/helper';

import { useStateQueryStore } from 'src/stores/useStateQueryStore';

describe('useStateQueryStore', () => {
    beforeAll(() => {
        createWrapper({}, {});
    });

    const stateQueryStore = useStateQueryStore();
    const { add, isLoading, remove, reset } = useStateQueryStore();
    const firstQuery = { url: 'myQuery' };

    function getQueries() {
        return stateQueryStore.queries;
    }

    beforeEach(() => {
        reset();
        expect(getQueries().size).toBeFalsy();
    });

    it('should add two queries', async () => {
        expect(getQueries().size).toBeFalsy();
        add(firstQuery);

        expect(getQueries().size).toBeTruthy();
        expect(getQueries().has(firstQuery)).toBeTruthy();

        add();
        expect(getQueries().size).toBe(2);
    });

    it('should add and remove loading state', async () => {
        expect(isLoading().value).toBeFalsy();
        add(firstQuery);
        expect(isLoading().value).toBeTruthy();
        remove(firstQuery);
        expect(isLoading().value).toBeFalsy();
    });

    it('should add and remove query', async () => {
        const secondQuery = { ...firstQuery };
        const thirdQuery = { ...firstQuery };

        add(firstQuery);
        add(secondQuery);

        const beforeCount = getQueries().size;
        add(thirdQuery);
        expect(getQueries().has(thirdQuery)).toBeTruthy();

        remove(thirdQuery);
        expect(getQueries().has(thirdQuery)).toBeFalsy();
        expect(getQueries().size).toBe(beforeCount);
    });
});