/// <reference types="cypress" />
describe('VnSearchBar', () => {
    const employeeId = ' #1';
    const salesPersonId = ' #18';
    const idGap = '.q-item > .q-item__label';
    const cardList = '.vn-card-list';

    let url;
    beforeEach(() => {
        cy.login('developer');
        cy.visit('#/customer/list');
        cy.url().then((currentUrl) => (url = currentUrl));
    });

    it('should redirect to customer summary page', () => {
        searchAndCheck('1', employeeId);
        searchAndCheck('salesPerson', salesPersonId);
    });

    it('should stay on the list page if there are several results or none', () => {
        cy.writeSearchbar('salesA{enter}');
        checkCardListAndUrl(2);

        cy.clearSearchbar();

        cy.writeSearchbar('0{enter}');
        checkCardListAndUrl(0);
    });

    const searchAndCheck = (searchTerm, expectedText) => {
        cy.clearSearchbar();
        cy.writeSearchbar(`${searchTerm}{enter}`);
        cy.get(idGap).should('have.text', expectedText);
    };

    const checkCardListAndUrl = (expectedLength) => {
        cy.get(cardList).then(($cardList) => {
            expect($cardList.find('.q-card').length).to.equal(expectedLength);
            cy.url().then((currentUrl) => expect(currentUrl).to.contain(url));
        });
    };
});
