import { TestBed } from '@angular/core/testing';
import {
    ISearchHistoryItem,
    SearchHistoryService,
} from '@shared/services/history/search-history.service';

describe('SearchHistoryService', () => {
    let service: SearchHistoryService;

    const firstItem: ISearchHistoryItem = {
        type: 'flight-number',
        url: '/SU0001-20220528',
        params: {
            carrier: 'SU',
            flightNumber: '0001',
            date: new Date(2022, 4, 28),
        },
    };
    const secondItem: ISearchHistoryItem = {
        type: 'flight-number',
        url: '/SU0002-20220528',
        params: {
            carrier: 'SU',
            flightNumber: '0002',
            date: new Date(2022, 4, 28),
        },
    };

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [SearchHistoryService],
        });

        service = TestBed.inject(SearchHistoryService);
    });

    it('should add item to empty array', () => {
        expect(service.items).toEqual([]);

        service.add(firstItem);

        expect(service.items).toEqual([firstItem]);
    });

    it('should not add item if previous item has the same url', () => {
        service.add(firstItem);
        expect(service.items).toEqual([firstItem]);
        service.add(firstItem);
        expect(service.items).toEqual([firstItem]);
    });

    it('should add items to the start if they have different urls', () => {
        service.add(firstItem);
        expect(service.items).toEqual([firstItem]);
        service.add(secondItem);
        expect(service.items).toEqual([secondItem, firstItem]);
    });
});
