<commit_before>'use strict';

var React = require('react');
var TestUtils = require('react/lib/ReactTestUtils');
var LinkedStateMixin = require('react/lib/LinkedStateMixin');
var MaskedInput = require('../src/MaskedInput');
var EventUtils = require('./EventUtils');
var chai = require('chai');
var expect = chai.expect;
var sinon = require('sinon');
chai.use(require('sinon-chai'));

describe('MaskedInput', function() {
  var container;
  var component;
  var domNode;
  var getInput;
  var props = {};

  // FIXME:
  // - undo?

  // TODO:
  // - When to show the placeholder
  // - Should the cursor be set to the first non-mask char?

  function getInputValue() {
    return TestUtils.findRenderedDOMComponentWithTag(getInput(), 'input').getDOMNode().value;
  }

  function cursorPosShouldEql(pos) {
    expect(getInput()._getSelection()).to.eql({start: pos, end: pos});
  }

  function simulateFocus(cb) {
    TestUtils.Simulate.focus(domNode);
    setTimeout(cb, 0);
  }

  var simulateKeyPress = key => EventUtils.simulateKeyPress(domNode, key);
  var simulateKeyDown = key => EventUtils.simulateKeyDown(domNode, key);
  var simulatePaste = content => EventUtils.simulateChange(domNode, content);
  var simulateTyping = content => EventUtils.simulateTyping(domNode, content);

  function setupTests(additionalTests) {
    context("when the mask is '99/99/9999'", function() {
      before(function() {
        props.mask = '99/99/9999';
      });

      additionalTests();

      describe('typing a key', function() {
        before(function() {
          props.onChange = sinon.spy();
          props.onComplete = sinon.spy();
        });

        after(function() {
          delete props.onChange;
          delete props.onComplete;
        });

        beforeEach(function() {
          props.onChange.reset();
        });

        afterEach(function() {
          props.onChange.reset();
          props.onComplete.reset();
        });

        context('when the character matches the mask', function() {
          beforeEach(function() {
            simulateKeyPress('2');
          });

          it('adds the character to the value', function() {
            expect(getInputValue()[0]).to.equal('2');
          });

          it('moves the cursor to the correct position', function() {
            cursorPosShouldEql(1);
          });

          it('correctly shifts the mask characters', function() {
            expect(getInputValue()).to.equal('2_/__/____');
          });

          it('calls the onChange callback', function() {
            expect(props.onChange).to.have.been.calledOnce;
            expect(props.onChange).to.have.been.calledWithExactly({
              target: {
                value: '2_/__/____'
              }
            });
          });

          it("doesn't call the onComplete callback", function() {
            expect(props.onComplete).to.have.not.been.called;
          });

          context('when the next character is a mask character', function() {
            // TODO: be?
            it('moves the cursor past the mask character', function() {
              simulateKeyPress('3');
              expect(getInputValue()).to.equal('23/__/____');
              cursorPosShouldEql(3);
            });
          });

          context('when the cursor is in the middle of the value', function() {
            beforeEach(function() {
              simulateTyping('34');
              getInput()._setSelection(1);
              simulateKeyPress('5');
            });

            it('adds the character to the value', function() {
              expect(getInputValue()[1]).to.equal('5');
            });

            it('moves the cursor to the correct position', function() {
              cursorPosShouldEql(3);
            });
          });

          context('when input text is selected', function() {
            beforeEach(function() {<commit_msg>:art: Cleanup<commit_after>'use strict';

var React = require('react');
var TestUtils = require('react/lib/ReactTestUtils');
var LinkedStateMixin = require('react/lib/LinkedStateMixin');
var MaskedInput = require('../src/MaskedInput');
var EventUtils = require('./EventUtils');
var chai = require('chai');
var expect = chai.expect;
var sinon = require('sinon');
chai.use(require('sinon-chai'));

describe('MaskedInput', function() {
  var container;
  var component;
  var domNode;
  var getInput;
  var props = {};

  // FIXME:
  // - undo?

  function getInputValue() {
    return TestUtils.findRenderedDOMComponentWithTag(getInput(), 'input').getDOMNode().value;
  }

  function cursorPosShouldEql(pos) {
    expect(getInput()._getSelection()).to.eql({start: pos, end: pos});
  }

  function simulateFocus(cb) {
    TestUtils.Simulate.focus(domNode);
    setTimeout(cb, 0);
  }

  var simulateKeyPress = key => EventUtils.simulateKeyPress(domNode, key);
  var simulateKeyDown = key => EventUtils.simulateKeyDown(domNode, key);
  var simulatePaste = content => EventUtils.simulateChange(domNode, content);
  var simulateTyping = content => EventUtils.simulateTyping(domNode, content);

  function setupTests(additionalTests) {
    context("when the mask is '99/99/9999'", function() {
      before(function() {
        props.mask = '99/99/9999';
      });

      additionalTests();

      describe('typing a key', function() {
        before(function() {
          props.onChange = sinon.spy();
          props.onComplete = sinon.spy();
        });

        after(function() {
          delete props.onChange;
          delete props.onComplete;
        });

        beforeEach(function() {
          props.onChange.reset();
        });

        afterEach(function() {
          props.onChange.reset();
          props.onComplete.reset();
        });

        context('when the character matches the mask', function() {
          beforeEach(function() {
            simulateKeyPress('2');
          });

          it('adds the character to the value', function() {
            expect(getInputValue()[0]).to.equal('2');
          });

          it('moves the cursor to the correct position', function() {
            cursorPosShouldEql(1);
          });

          it('correctly shifts the mask characters', function() {
            expect(getInputValue()).to.equal('2_/__/____');
          });

          it('calls the onChange callback', function() {
            expect(props.onChange).to.have.been.calledOnce;
            expect(props.onChange).to.have.been.calledWithExactly({
              target: {
                value: '2_/__/____'
              }
            });
          });

          it("doesn't call the onComplete callback", function() {
            expect(props.onComplete).to.have.not.been.called;
          });

          context('when the next character is a mask character', function() {
            beforeEach(function() {
              simulateKeyPress('3');
            });

            it('moves the cursor past the mask character', function() {
              expect(getInputValue()).to.equal('23/__/____');
              cursorPosShouldEql(3);
            });
          });

          context('when the cursor is in the middle of the value', function() {
            beforeEach(function() {
              simulateTyping('34');
              getInput()._setSelection(1);
              simulateKeyPress('5');
            });

            it('adds the character to the value', function() {
              expect(getInputValue()[1]).to.equal('5');
            });

            it('moves the cursor to the correct position', function() {
              cursorPosShouldEql(3);
            });
          });

          context('when input text is selected', function() {
            beforeEach(function() {
{'fraction_non_alphanumeric': 0.14004659590991458, 'fraction_numerical': 0.008801449650530676, 'mean_word_length': 2.2357621440536013, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6478045', 'n_tokens_mistral': 2177, 'n_tokens_neox': 2086, 'n_words': 454}