import React from 'react';
import { WedaApp } from './setup';

import '@testing-library/jest-dom/extend-expect';

import { ErrorBoundary } from 'react-error-boundary';

import { render, screen, fireEvent, waitFor, act } from '@testing-library/react';

import { defaultComponents } from './sample/defaultComponents';
import { loadDependiences } from './mocks/loaddependiences';

import { componentError, componentLibError, lifecycleError } from './sample/error.cals';

describe('组件问题', () => {
  it('找不到组件', async () => {
    const { debug, baseElement } = render(
      <WedaApp cals={componentError} dependencies={defaultComponents} loadDependiences={loadDependiences} />,
    );
    // 提示 “组件gsd-h5-react:Text2未找到”
    expect(await screen.findByText('gsd-h5-react:Text2')).toBeVisible();
  });

  it('找不到组件库', async () => {
    const { debug, baseElement } = render(
      <WedaApp cals={componentLibError} dependencies={defaultComponents} loadDependiences={loadDependiences} />,
    );
    // 提示 “组件xxxxx:Text未找到”
    expect(await screen.findByText('xxxxx:Text')).toBeVisible();
  });
});

describe.skip('用户代码问题', () => {
  // TODO
  // 待想办法进行测试
  it('生命周期代码异常', async () => {
    const fallbackRender = jest.fn(() => null);
    const { debug, baseElement } = render(
      <ErrorBoundary fallbackRender={fallbackRender}>
        <WedaApp cals={lifecycleError} dependencies={defaultComponents} loadDependiences={loadDependiences} />
      </ErrorBoundary>,
    );

    await waitFor(() => expect(fallbackRender).toHaveBeenCalled());

    // 事件/生命周期异常不应该阻止渲染
    expect(await screen.findByText('生命周期代码异常')).toBeVisible();

    // debug();

    console.log(fallbackRender.mock.calls);
  });
});
