import { getUnitCreateData } from '@/manager/modules/unit/unit-factories';
import { test } from '@/manager/modules/unit/unit-fixtures';
import { getUnitTypeCreateData } from '@/manager/modules/unit-type/unit-type-factories';
import { expectSingleLocatorToHaveText } from '@/manager/shared/utils/expect-utils';
import { formatId } from '@/shared/utils/formatters';
import { expect } from '@/shared/utils/matchers';

test('navigates from the list to the details page', async ({ createUnit, createUnitType, unitListPage }) => {
  const unitType = await createUnitType(getUnitTypeCreateData());
  const unit = await createUnit(getUnitCreateData(unitType));
  const id = formatId(unit.id);

  await unitListPage.goto();

  const detailsPage = await test.step('navigate to unit details page', async () => {
    await unitListPage.searchTextField.fill(id);
    return unitListPage.goToUnitDetailsPage(0);
  });

  await test.step('verify unit details page', async () => {
    await expectSingleLocatorToHaveText(detailsPage.title, unit.name);
  });
});

test('navigates from the list to the create dialog', async ({ unitListPage }) => {
  await unitListPage.goto();

  const createDialog = await test.step('open unit create dialog from list', () => unitListPage.openUnitCreateDialog());

  await test.step('verify unit create dialog is open', async () => {
    await expect(createDialog.main).toBeVisible();
  });
});
