import React from "dist/React";
import ReactTestUtils from "lib/ReactTestUtils";
var ReactDOM = window.ReactDOM || React;
function normalizeCodeLocInfo(str) {
    return str && str.replace(/\(at .+?:\d+\)/g, '(at **)');
  }

  function dispatchEventOnNode(node, type) {
    node.dispatchEvent(new Event(type, {bubbles: true, cancelable: true}));
  }

 var setUntrackedValue = Object.getOwnPropertyDescriptor(
    HTMLInputElement.prototype,
    'value',
  ).set;

describe("ReactDOMInput", function() {
    this.timeout(200000);

    it("should properly control a value even if no event listener exists", () => {
        const container = document.createElement("div");
        const stub = ReactDOM.render(<input type="text" value="lion" />, container);
    
        document.body.appendChild(container);
    
        const node = ReactDOM.findDOMNode(stub);
      
    
        setUntrackedValue.call(node, "giraffe");
    
        // This must use the native event dispatching. If we simulate, we will
        // bypass the lazy event attachment system so we won't actually test this.
        dispatchEventOnNode(node, "change");
    
        expect(node.value).toBe("lion");
    
        document.body.removeChild(container);
    });


    it('should control a value in reentrant events', () => {
        class ControlledInputs extends React.Component {
          state = {value: 'lion'};
          a = null;
          b = null;
          switchedFocus = false;
          change(newValue) {
            this.setState({value: newValue});
            // Calling focus here will blur the text box which causes a native
            // change event. Ideally we shouldn't have to fire this ourselves.
            // Don't remove unless you've verified the fix in #8240 is still covered.
            dispatchEventOnNode(this.a, 'change');
            this.b.focus();
          }
          blur(currentValue) {
            this.switchedFocus = true;
            // currentValue should be 'giraffe' here because we should not have
            // restored it on the target yet.
            this.setState({value: currentValue});
          }
          render() {
            return (
              <div>
                <input
                  type="text"
                  ref={n => (this.a = n)}
                  value={this.state.value}
                  onChange={e => this.change(e.target.value)}
                  onBlur={e => this.blur(e.target.value)}
                />
                <input type="text" ref={n => (this.b = n)} />
              </div>
            );
          }
        }
    
        const container = document.createElement('div');
        const instance = ReactDOM.render(<ControlledInputs />, container);
    
        // We need it to be in the body to test native event dispatching.
        document.body.appendChild(container);
    
        // Focus the field so we can later blur it.
        // Don't remove unless you've verified the fix in #8240 is still covered.
        instance.a.focus();
        setUntrackedValue.call(instance.a, 'giraffe');
        // This must use the native event dispatching. If we simulate, we will
        // bypass the lazy event attachment system so we won't actually test this.
        dispatchEventOnNode(instance.a, 'change');
        dispatchEventOnNode(instance.a, 'blur');
    
        expect(instance.a.value).toBe('giraffe');
        expect(instance.switchedFocus).toBe(true);
    
        document.body.removeChild(container);
      });
      it('should control values in reentrant events with different targets', () => {
        class ControlledInputs extends React.Component {
          state = {value: 'lion'};
          a = null;
          b = null;
          change(newValue) {
            // This click will change the checkbox's value to false. Then it will
            // invoke an inner change event. When we finally, flush, we need to
            // reset the checkbox's value to true since that is its controlled
            // value.
            this.b.click();
          }
          render() {
            return (
              <div>
                <input
                  type="text"
                  ref={n => (this.a = n)}
                  value="lion"
                  onChange={e => this.change(e.target.value)}
                />
                <input
                  type="checkbox"
                  ref={n => (this.b = n)}
                  checked={true}
                  onChange={() => {}}
                />
              </div>
            );
          }
        }
    
        const container = document.createElement('div');
        const instance = ReactDOM.render(<ControlledInputs />, container);
    
        // We need it to be in the body to test native event dispatching.
        document.body.appendChild(container);
    
        setUntrackedValue.call(instance.a, 'giraffe');
        // This must use the native event dispatching. If we simulate, we will
        // bypass the lazy event attachment system so we won't actually test this.
        dispatchEventOnNode(instance.a, 'input');
    
        expect(instance.a.value).toBe('lion');
        expect(instance.b.checked).toBe(true);
    
        document.body.removeChild(container);
      });
    

});
var jest = {
    fn: function(){
        return function(){

        }
    }
}

describe('switching text inputs between numeric and string numbers', () => {
    it('does change the number 2 to "2.0" with no change handler', () => {
      let stub = <input type="text" value={2} onChange={jest.fn()} />;
      stub = ReactTestUtils.renderIntoDocument(stub);
      const node = ReactDOM.findDOMNode(stub);

      node.value = '2.0';

      ReactTestUtils.Simulate.change(stub);

      expect(node.getAttribute('value')).toBe('2');
      expect(node.value).toBe('2');
    });

    it('does change the string "2" to "2.0" with no change handler', () => {
      let stub = <input type="text" value={'2'} onChange={jest.fn()} />;
      stub = ReactTestUtils.renderIntoDocument(stub);
      const node = ReactDOM.findDOMNode(stub);

      node.value = '2.0';

      ReactTestUtils.Simulate.change(stub);

      expect(node.getAttribute('value')).toBe('2');
      expect(node.value).toBe('2');
    });


    it('changes the number 2 to "2.0" using a change handler', () => {
        class Stub extends React.Component {
          state = {
            value: 2,
          };
          onChange = event => {
            this.setState({value: event.target.value});
          };
          render() {
            const {value} = this.state;
  
            return <input type="text" value={value} onChange={this.onChange} />;
          }
        }
  
        const stub = ReactTestUtils.renderIntoDocument(<Stub />);
        const node = ReactDOM.findDOMNode(stub);
  
        node.value = '2.0';
  
        ReactTestUtils.Simulate.change(node);
  
        expect(node.getAttribute('value')).toBe('2.0');
        expect(node.value).toBe('2.0');
      });
    


})