import selectors from '../../helpers/selectors.js';
import getBrowser from '../../helpers/puppeteer';

describe('Account Role create and basic data path', () => {
    let browser;
    let page;

    beforeAll(async() => {
        browser = await getBrowser();
        page = browser.page;
        await page.loginAndModule('it', 'account');
        await page.accessToSection('account.role');
    });

    afterAll(async() => {
        await browser.close();
    });

    it('should open the new account role form by clicking the add button', async() => {
        await page.waitToClick(selectors.accountRoleIndex.addRole);
        await page.waitForState('account.role.create');
    });

    it('should fill the form and then save it by clicking the create role button', async() => {
        await page.write(selectors.accountRoleIndex.newName, 'boringRole');
        await page.write(selectors.accountRoleIndex.newDescription, 'Boring description');
        await page.waitToClick(selectors.accountRoleIndex.createRoleButton);
        const message = await page.waitForSnackbar();

        expect(message.text).toContain('Data saved!');
    });

    it('should redirect the user to the created role basic data section', async() => {
        await page.waitForState('account.role.card.basicData');
    });

    it('should edit the role basic data', async() => {
        await page.overwrite(selectors.accountRoleBasicData.name, 'psyker');
        await page.overwrite(selectors.accountRoleBasicData.description, 'A role just for psykers');
        await page.waitToClick(selectors.accountRoleBasicData.save);
        const message = await page.waitForSnackbar();

        expect(message.text).toContain('Data saved!');
    });

    it('should reload the role basicData section and check the name was edited successfully', async() => {
        await page.reloadSection('account.role.card.basicData');
        const result = await page.waitToGetProperty(selectors.accountRoleBasicData.name, 'value');

        expect(result).toEqual('psyker');
    });

    it('should check the role description was edited successfully', async() => {
        const result = await page.waitToGetProperty(selectors.accountRoleBasicData.description, 'value');

        expect(result).toContain('psykers');
    });

    it('should navigate to the subroles section', async() => {
        await page.accessToSection('account.role.card.subroles');
    });

    it('should asign a subrole', async() => {
        await page.waitToClick(selectors.accountSubroles.addSubrole);
        await page.autocompleteSearch(selectors.accountSubroles.role, 'teamManager');
        await page.waitToClick(selectors.accountSubroles.save);
        const message = await page.waitForSnackbar();

        expect(message.text).toContain('Role added!');
    });

    it('should reload the subroles section and check a role was added', async() => {
        await page.reloadSection('account.role.card.subroles');
        const subrolesCount = await page.countElement(selectors.accountSubroles.anyResult);

        expect(subrolesCount).toEqual(1);
    });

    it('should access the employee roles inheritance then check the roles listed are the expected ones', async() => {
        await page.accessToSearchResult('employee');
        await page.accessToSection('account.role.card.inherited');
        const rolesCount = await page.countElement(selectors.accountRoleInheritance.anyResult);

        expect(rolesCount).toEqual(7);
    });
});