// Libs
// -----------------------------------------------------------------------------
import { screen, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom';
import { rest } from 'msw';
import { setupServer } from 'msw/node';

// App
// -----------------------------------------------------------------------------
import { testRenderWithProviders, handleLocaleFileRequest } from 'testingUtils';
import { LicensePage } from './LicensePage';
import { TypeLicense } from 'types/default';

// Test data
// -----------------------------------------------------------------------------

const license: TypeLicense = {
  license: {
    customer: 'test_customer',
    product: 'test_product',
    type: 'test_type',
    evaluationStart: '2022-01-01T00:00:00.000Z',
    evaluationEnd: '2022-12-31T00:00:00.000Z',
    features: [],
    options: {},
  },
  features: [
    {
      name: 'test_feature_name',
      description: 'test_feature_description',
    },
  ],
  options: [
    {
      name: 'test_option_name',
      value: 1337,
      description: 'test_option_description',
    },
  ],
};

// Setup MSW server
// -----------------------------------------------------------------------------

const server = setupServer(
  handleLocaleFileRequest,
  rest.get('/en/api/license/', (req, res, ctx) => {
    return res(ctx.json(license));
  }),
);

beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

// Test cases
// -----------------------------------------------------------------------------

test('License page: full', async () => {
  testRenderWithProviders(<LicensePage />);

  await waitFor(() => {
    expect(screen.getByText('License')).toBeInTheDocument();
    expect(screen.getByText('Current license')).toBeInTheDocument();
    expect(screen.getByText('Activate new')).toBeInTheDocument();

    expect(screen.getByText('License info')).toBeInTheDocument();
    expect(screen.getByText('License features')).toBeInTheDocument();
    expect(screen.getByText('License options')).toBeInTheDocument();

    expect(screen.getByText('test_customer')).toBeInTheDocument();
    expect(screen.getByText('test_product')).toBeInTheDocument();
    expect(screen.getByText('test_type')).toBeInTheDocument();
    expect(screen.getByText('01.01.2022')).toBeInTheDocument();
    expect(screen.getByText('31.12.2022')).toBeInTheDocument();
    expect(screen.getByText('test_feature_name')).toBeInTheDocument();
    expect(screen.getByText('test_feature_description')).toBeInTheDocument();
    expect(screen.getByText('test_option_name')).toBeInTheDocument();
    expect(screen.getByText('1337')).toBeInTheDocument();
    expect(screen.getByText('test_option_description')).toBeInTheDocument();
  });
});

test('License page: empty features and options', async () => {
  server.use(
    rest.get('/en/api/license/', (req, res, ctx) => {
      return res(ctx.json({ ...license, features: [], options: [] }));
    }),
  );

  testRenderWithProviders(<LicensePage />);

  await waitFor(() => {
    expect(screen.getByText('No options defined')).toBeInTheDocument();
    expect(screen.getByText('No features defined')).toBeInTheDocument();
  });
});
