<commit_before>/*global describe, it, expect*/
'use strict';

describe('types', function() {

  var React = require('react/addons');
  var TestUtils = React.addons.TestUtils;

  var mounted = function (component) {
    var rendered = TestUtils.renderIntoDocument(component);
    return rendered;
  };

  var testWithFormatic = function (formatic) {

    var unmapType = function (type) {

      var typeMap = formatic.options.typeMap || {};
      var types = Object.keys(typeMap).filter(function (key) {
        return typeMap[key] === type;
      }).map(function (key) {
        return key;
      });

      if (types.length === 0) {
        types.push(type);
      }

      return types;
    };

    var testValueType = function (field, selector, setter) {

      var types = unmapType(field.type);

      types.forEach(function (type) {

        field.type = type;

        it('should change data for ' + field.type, function () {
          var form = formatic();
          form.fields([
            field
          ]);

          var component = mounted(form.component());

          form.on('update', function (props) {
            component.setProps(props);
          });
          form.set('name', 'Joe');

          var value;
          var node;

          if (typeof selector === 'function') {
            value = selector(component.getDOMNode());
          } else {
            if (selector[0] === '.') {
              node = component.getDOMNode().getElementsByClassName(selector)[0];
            } else {
              node = component.getDOMNode().getElementsByTagName(selector)[0];
            }
            value = node.value;
          }

          expect(value).toEqual('Joe');

          if (typeof setter === 'function') {
            setter(component.getDOMNode(), 'Mary');
          } else {
            node.value = 'Mary';
            TestUtils.Simulate.change(node);
          }

          expect(form.val()).toEqual({name: 'Mary'});
        });
      });
    };

    testValueType({
      type: 'text',
      key: 'name'
    }, 'input');

    testValueType({
      type: 'textarea',
      key: 'name'
    }, 'textarea');

    testValueType({
      type: 'select',
      key: 'name',
      choices: ['Joe', 'Mary']
    }, function (node) {
      return node.getElementsByClassName('zf-select-active')[0].innerHTML;
    }, function (node, value) {
      var arrowNode = node.getElementsByClassName('zf-select-arrow')[0];
      TestUtils.Simulate.click(arrowNode);
      var choiceNodes = node.getElementsByClassName('zf-select-choice');
      choiceNodes = Array.prototype.slice.call(choiceNodes, 0);
      var matchingNodes = choiceNodes.filter(function (node) {
        return node.innerHTML === value;
      });
      TestUtils.Simulate.click(matchingNodes[0]);
    });

    testValueType({
      type: 'password',
      key: 'name'
    }, 'input');
  };

  testWithFormatic(require('../')('react'));
  testWithFormatic(require('../')('zapier'));

});
<commit_msg>Fixed tests for dropdown refactor.<commit_after>/*global describe, it, expect*/
'use strict';

describe('types', function() {

  var React = require('react/addons');
  var TestUtils = React.addons.TestUtils;

  var mounted = function (component) {
    var rendered = TestUtils.renderIntoDocument(component);
    return rendered;
  };

  var testWithFormatic = function (formatic) {

    var unmapType = function (type) {

      var typeMap = formatic.options.typeMap || {};
      var types = Object.keys(typeMap).filter(function (key) {
        return typeMap[key] === type;
      }).map(function (key) {
        return key;
      });

      if (types.length === 0) {
        // If this type maps to itself, make sure that type doesn't map to
        // something else.
        if (!(type in typeMap)) {
          types.push(type);
        }
      }

      return types;
    };

    var testValueType = function (field, selector, setter) {

      var types = unmapType(field.type);

      types.forEach(function (type) {

        field.type = type;

        it('should change data for ' + field.type, function () {
          var form = formatic();
          form.fields([
            field
          ]);

          var component = mounted(form.component());

          form.on('update', function (props) {
            component.setProps(props);
          });
          form.set('name', 'Joe');

          var value;
          var node;

          if (typeof selector === 'function') {
            value = selector(component.getDOMNode());
          } else {
            if (selector[0] === '.') {
              node = component.getDOMNode().getElementsByClassName(selector)[0];
            } else {
              node = component.getDOMNode().getElementsByTagName(selector)[0];
            }
            value = node.value;
          }

          expect(value).toEqual('Joe');

          if (typeof setter === 'function') {
            setter(component.getDOMNode(), 'Mary');
          } else {
            node.value = 'Mary';
            TestUtils.Simulate.change(node);
          }

          expect(form.val()).toEqual({name: 'Mary'});
        });
      });
    };

    testValueType({
      type: 'text',
      key: 'name'
    }, 'input');

    testValueType({
      type: 'textarea',
      key: 'name'
    }, 'textarea');

    testValueType({
      type: 'select',
      key: 'name',
      choices: ['Joe', 'Mary']
    }, 'select');

    testValueType({
      type: 'dropdown',
      key: 'name',
      choices: ['Joe', 'Mary']
    }, function (node) {
      return node.getElementsByClassName('field-value')[0].innerHTML;
    }, function (node, value) {
      var arrowNode = node.getElementsByClassName('field-toggle')[0];
      TestUtils.Simulate.click(arrowNode);
      var choiceNodes = node.getElementsByClassName('field-choice');
      choiceNodes = Array.prototype.slice.call(choiceNodes, 0);
      var matchingNodes = choiceNodes.filter(function (node) {
        return node.innerHTML === value;
      });
      TestUtils.Simulate.click(matchingNodes[0]);
    });

    testValueType({
      type: 'password',
      key: 'name'
    }, 'input');
  };

  testWithFormatic(require('../')('react'));
  testWithFormatic(require('../')('zapier'));

});

{'fraction_non_alphanumeric': 0.1529110684580934, 'fraction_numerical': 0.0025591810620601407, 'mean_word_length': 2.120259481037924, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '186671', 'n_tokens_mistral': 1835, 'n_tokens_neox': 1764, 'n_words': 372}