{"size": 2169, "ext": "js", "max_stars_repo_path": "src/widgets/input/__tests__/InputTestUtils.js", "max_stars_repo_name": "cuicuiwang/lugia-web", "max_stars_repo_stars_event_min_datetime": "2019-08-08T13:13:35.000Z", "max_stars_repo_stars_event_max_datetime": "2020-12-14T01:17:41.000Z", "max_issues_repo_path": "src/widgets/input/__tests__/InputTestUtils.js", "max_issues_repo_name": "cuicuiwang/lugia-web", "max_issues_repo_issues_event_min_datetime": "2019-12-30T05:14:46.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-08T23:01:23.000Z", "max_forks_repo_path": "src/widgets/input/__tests__/InputTestUtils.js", "max_forks_repo_name": "cuicuiwang/lugia-web", "max_forks_repo_forks_event_min_datetime": "2019-08-09T02:39:54.000Z", "max_forks_repo_forks_event_max_datetime": "2021-06-09T08:49:23.000Z"}
{"max_stars_count": 52, "max_issues_count": 42, "max_forks_count": 26, "avg_line_length": 26.4512195122, "max_line_length": 82, "alphanum_fraction": 0.6892577225}
/**
 @flow
 */
import type { ReactWrapper } from 'enzyme';
import Enzyme, { mount } from 'enzyme';
import type { VerifyOrder } from '@lugia/jverify';
import * as React from 'react';
import Input from '../';
import 'jest-styled-components';
import chai from 'chai';
import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({ adapter: new Adapter() });

const {
  mockFunction,
  VerifyOrder: VerifyOrderFactory,
  VerifyOrderConfig,
} = require('@lugia/jverify');

const { expect: exp } = chai;

export function testPropsValue(value: string, expect: string) {
  const component = mount(<Input value={value} />);
  assertInputValue(component, expect);
}

export function assertInputValue(component: ReactWrapper, expect: string) {
  const inputDOM = getInputDOM(component);
  if (inputDOM) {
    exp(inputDOM.value).to.be.equal(expect);
    return;
  }
  throw new Error('input创建失败');
}

function getInputDOM(component, text): HTMLInputElement | null {
  const result = component.find('input').getDOMNode();
  if (result instanceof HTMLInputElement) {
    return result;
  }
  return null;
}

type KeyEventType = 'onKeyUp' | 'onKeyPress' | 'onKeyDown' | 'onFocus' | 'onBlur';

export function testKeyBoardEvent(
  order: VerifyOrder,
  keyEvent: KeyEventType,
  opt: { keyCode: any, Target: React.ComponentType<any> }
) {
  const mockFunc = mockFunction.create(VerifyOrderConfig.create(keyEvent, order));
  const { keyCode = 49, Target } = opt;
  const event = { keyCode };
  mockFunc.mock(({ keyCode }) => {
    exp(keyCode).to.be.equal(keyCode);
  });
  const props = {
    [keyEvent]: mockFunc.getFunction(),
  };

  const component = mount(<Target {...props} />);
  component.find('input').simulate(keyEvent.substr(2).toLowerCase(), event);
  order.verify(arg => {
    arg[keyEvent](VerifyOrderFactory.Object);
  });
}

/*
*   键盘事件为空的情况
*/
export function testFireNullKeyBoardEvent(
  keyEvent: KeyEventType,
  opt: { keyCode: any, Target: React.ComponentType<any> }
) {
  const { keyCode = 49, Target } = opt;
  const component = mount(<Target />);
  const event = { keyCode };

  component.find('input').simulate(keyEvent.substr(2).toLowerCase(), event);
}
