import React from 'react';
import { screen } from '@testing-library/react';
import { renderInAppContext } from '@theorchard/suite-testing';
import * as mockStores from 'src/__fixtures__/graphql/store-list-response.json';
import * as storesQueries from 'src/apollo/queries/stores';
import { ContractStores } from 'src/components/contract-detail/contract-stores';
import type { GetStoresQuery } from 'src/apollo/queries/__generated__/stores';

describe('<ContractStores>', () => {
    afterEach(jest.restoreAllMocks);

    beforeEach(() => {
        jest.spyOn(storesQueries, 'useStoreList').mockReturnValue({
            data: mockStores as GetStoresQuery,
            loading: false,
        });
    });

    const render = (props: any) =>
        renderInAppContext(<ContractStores {...props} />);
    const storeList = ['121 Music', 'Spotify'];

    it('renders Services section correctly when there are no excluded stores', () => {
        const props = {
            contractExclusion: null,
            storeList,
            storesLoading: false,
        };
        render(props);
        expect(screen.getByText('All')).toBeDefined();
        expect(screen.getByText('None')).toBeDefined();
    });

    it('renders Services section correctly when there are excluded stores', () => {
        const props = {
            contractExclusion: { exclusions: { stores: ['1'] } },
            storeList,
            storesLoading: false,
        };
        render(props);
        expect(screen.getByText('121 Music')).toBeDefined();
    });

    it('renders Services section when there are no excluded stores', () => {
        const props = {
            contractExclusion: { exclusions: { stores: [] } },
            storeList,
            storesLoading: false,
        };
        render(props);
        expect(screen.getByText('All')).toBeDefined();
        expect(screen.getByText('None')).toBeDefined();
    });
});
