<commit_before>#
# The `Input` unit tests
#
# Copyright (C) 2011 <NAME>
#
{describe, assert, server, load} = require('../test_helper')

server.respond "/input.html": """
<html>
  <head>
    <script src="/core.js"></script>
    <script src="/dom.js"></script>
  </head>
  <body>
    <form id="test-form" action="/some.url">
      <input type="text" name="name" id="input-name"/>

      <select id="multi-select" multiple="true">
        <option value="one">One</option>
        <option value="two" selected="true">Two</option>
        <option value="three" selected="true">Three</option>
        <option value="four">Four</option>
      </select>
    </form>
  </body>
</html>
"""

Input = ->
  load "/input.html", this, ->
    this.Input

test_input = ->
  load "/input.html", this, ->
    new this.Input(this.document.getElementById('input-name'))

test_multiselect = ->
  load "/input.html", this, ->
    new this.Input(this.document.getElementById('multi-select'))


describe "Input", module,
  "constructor":
    topic: Input

    "should create new input fields on raw elements": (Input)->
      raw   = this.document.getElementById('input-name')
      input = new Input(raw)

      assert.instanceOf input,   Input
      assert.same       input._, raw

    "should create new inputs by a tag name and options": (Input)->
      input = new Input('textarea', name: 'boo')

      assert.instanceOf input,           Input
      assert.equal      input._.tagName, 'TEXTAREA'
      assert.equal      input._.name,    'boo'

    "should be dynamically used with the 'Element' constructor": (Input)->
      input = new this.Element('input', type: 'radio', name: 'boo')

      assert.instanceOf input,           Input
      assert.equal      input._.tagName, 'INPUT'
      assert.equal      input._.type,    'radio'
      assert.equal      input._.name,    'boo'

    "should create input[type=text] by default": (Input)->
      input = new Input(name: 'test')

      assert.instanceOf input,           Input
      assert.equal      input._.tagName, 'INPUT'
      assert.equal      input._.type,    'text'
      assert.equal      input._.name,    'test'

    "should allow to specify the input field 'type'": (Input)->
      input = new Input(type: 'password', name: 'test')

      assert.instanceOf input,           Input
      assert.equal      input._.tagName, 'INPUT'
      assert.equal      input._.type,    'password'
      assert.equal      input._.name,    'test'

    "should create textareas with the {type: 'textarea'} option": (Input)->
      input = new Input(type: 'textarea', name: 'test')

      assert.instanceOf input,           Input
      assert.equal      input._.tagName, 'TEXTAREA'
      assert.equal      input._.name,    'test'

    "should create select element with the {type: 'select'} option": (Input)->
      input = new Input(type: 'select', name: 'test')

      assert.instanceOf input,           Input
      assert.equal      input._.tagName, 'SELECT'
      assert.equal      input._.name,    'test'

  "#form":
    topic: test_input

    "should return reference to it's form object": (input)->
      form = input.form()

      assert.instanceOf form,   this.Form
      assert.same       form._, this.document.getElementById('test-form')

  '#value()':
    "with a plain input field":
      topic: test_input

      "should return the value of the field": (input)->
        assert.equal input.value(), input._.value

    "with a multi-select field":
      topic: test_multiselect

      "should return an array of selected values": (input)->
        assert.deepEqual input.value(), ['two', 'three']

  '#value("data")':
    topic: Input

    "with a plain input field":
      topic: (Input)-> new Input(value: 'old value')

      "should assign the new value for the field": (input)->
        input.value('new value')
        assert.equal input._.value, 'new value'

      "should return the input field itself back to the code": (input)->
        assert.same input.value('another value'), input

    "with multi-select":
      topic: (Input)->
        input = new Input(type: 'select', multiple: true)
        input.html """
          <option value="one">One</option>
          <option value="two" selected="true">Two</option>
          <option value="three" selected="true">Three</option>
          <option value="four">Four</option>
        """

      "should assign a new value as an array": (input)->
        input.value(['one', 'four'])

        assert.deepEqual input.value(), ['one', 'four']

      "should assign a new value as a string": (input)->
        input.value('two')

        assert.deepEqual input.value(), ['two']

  '#focus()':
    topic: test_input

    "should call the raw 'focus' method": (input)->
      called = false
      input._.focus = -> called = true

      input.focus()

      assert.isTrue called

    "should set the 'focused' property of the wrapper": (input)->
      delete(input.focused)

      input.focus()

      assert.isTrue input.focused

    "should return the input field itself back to the code": (input)->
      assert.same input.focus(), input

  '#blur()':
    topic: test_input

    "should call the raw 'blur' method": (input)->
      called = false
      input._.blur = -> called = true

      input.blur()

      assert.isTrue called

    "should set the 'focused' property of the wrapper to 'false'": (input)->
      delete(input.focused)

      input.blur()

      assert.isFalse input.focused

    "should return the input field itself back to the code": (input)->
      assert.same input.blur(), input

  '#select()':
    topic: test_input

    "should call the raw 'select' method": (input)->
      called = false
      input._.select = -> called = true

      input.select()

      assert.isTrue called

    "should set the 'focused' property of the wrapper": (input)->
      delete(input.focused)

      input.select()

      assert.isTrue input.focused

    "should return the input field itself back to the code": (input)->
      assert.same input.select(), input


  '#disable()':
    topic: test_input

    "should set the 'disabled' property to 'true'": (input)->
      input._.disabled = false
      input.disable()

      assert.isTrue input._.disabled

    "should emit the 'disable' event": (input)->
      emitted = false
      input.on('disable', -> emitted = true)
      input.disable()

      assert.isTrue emitted

    "should return the field itself back to the code": (input)->
      assert.same input.disable(), input


  '#enable()':
    topic: test_input

    "should set the 'disabled' property to 'false'": (input)->
      input._.disabled = true
      input.enable()

      assert.isFalse input._.disabled

    "should emit the 'enable' event": (input)->
      emitted = false
      input.on('enable', -> emitted = true)
      input.enable()

      assert.isTrue emitted

    "should return the field itself back to the code": (input)->
      assert.same input.enable(), input

  '#disabled':
    topic: Input

    "\b()":
      topic: (Input)-> new Input(name: 'test')

      "should return 'true' when a field is disabled": (input)->
        input._.disabled = true
        assert.isTrue input.disabled()

      "should return 'false' when a field is not disabled": (input)->
        input._.disabled = false
        assert.isFalse input.disabled()

     "\b(value)":
       topic: (Input)-> new Input(name: 'test')

       "should call 'disable' when the value is 'true'": (input)->
         called = false
         input.disable = -> called = true; return @
         input.disabled(true)

         assert.isTrue called

       "should call 'enable' when the value is 'false'": (input)->
         called = false
         input.enable = -> called = true; return @
         input.disabled(false)

         assert.isTrue called

       "should return the field itself back to the code": (input)->
         assert.same input.disabled(true), input

  '#checked':
    topic: Input

    "\b()":
      topic: (Input)-> new Input(type: 'checkbox')

      "should return 'true' when the field is checked": (input)->
        input._.checked = true
        assert.isTrue input.checked()

      "should return 'false' when the field is not checked": (input)->
        input._.checked = false
        assert.isFalse input.checked()

    "\b(value)":
      topic: (Input)-> new Input(type: 'checkbox')

      "should make the field checked when called with 'true'": (input)->
        input._.checked = false
        input.checked(true)
        assert.isTrue input._.checked

      "should make the field unchecked when called with 'false'": (input)->
        input._.checked = true
        input.checked(false)
        assert.isFalse input._.checked

      "should return the input field itself back to the code": (input)->
        assert.same input.checked(true), input

  'NodeList extension':
    topic: Input,

    "'should add the 'value()' method": (Input)->
      input = new Input(name: 'boo', value: 'hoo')
      search = new this.NodeList([input])

      assert.isTrue 'value' of search
      assert.same   search.value(), input.value()<commit_msg>STL/dom moving the input_test from vows to mocha<commit_after>#
# The `Input` unit tests
#
# Copyright (C) 2011-2012 <NAME>
#
{Browser} = require('../test_helper')

Browser.respond "/input.html": """
<html>
  <head>
    <script src="/core.js"></script>
    <script src="/dom.js"></script>
  </head>
  <body>
    <form id="test-form" action="/some.url">
      <input type="text" name="name" id="input-name"/>

      <select id="multi-select" multiple="true">
        <option value="one">One</option>
        <option value="two" selected="true">Two</option>
        <option value="three" selected="true">Three</option>
        <option value="four">Four</option>
      </select>
    </form>
  </body>
</html>
"""

describe "Input", ->
  get_Input = (callback)->
    (done)->
      Browser.open "/input.html", ($, window)->
        callback($.Input, $, window, window.document)
        done()

  get_input = (callback)->
    (done)->
      Browser.open "/input.html", ($, window)->
        callback(new $.Input(window.document.getElementById('input-name')), $, window, window.document)
        done()

  get_multiselect = (callback)->
    (done)->
      Browser.open "/input.html", ($, window)->
        callback(new $.Input(window.document.getElementById('multi-select')), $, window, window.document)
        done()

  describe "constructor", ->

    it "should create new input fields on raw elements", get_Input (Input, $, window, document)->
      raw   = document.getElementById('input-name')
      input = new Input(raw)

      input.should.be.instanceOf Input
      input._.should.equal       raw

    it "should create new inputs by a tag name and options", get_Input (Input)->
      input = new Input('textarea', name: 'boo')

      input.should.be.instanceOf Input
      input._.tagName.should.eql 'TEXTAREA'
      input._.name.should.eql    'boo'

    it "should be dynamically used with the 'Element' constructor", get_Input (Input, $)->
      input = new $.Element('input', type: 'radio', name: 'boo')

      input.should.be.instanceOf Input
      input._.tagName.should.eql 'INPUT'
      input._.type.should.eql    'radio'
      input._.name.should.eql    'boo'

    it "should create input[type=text] by default", get_Input (Input)->
      input = new Input(name: 'test')

      input.should.be.instanceOf Input
      input._.tagName.should.eql 'INPUT'
      input._.type.should.eql    'text'
      input._.name.should.eql    'test'

    it "should allow to specify the input field 'type'", get_Input (Input)->
      input = new Input(type: 'password', name: 'test')

      input.should.be.instanceOf Input
      input._.tagName.should.eql 'INPUT'
      input._.type.should.eql    'password'
      input._.name.should.eql    'test'

    it "should create textareas with the {type: 'textarea'} option", get_Input (Input)->
      input = new Input(type: 'textarea', name: 'test')

      input.should.be.instanceOf Input
      input._.tagName.should.eql 'TEXTAREA'
      input._.name.should.eql    'test'

    it "should create select element with the {type: 'select'} option", get_Input (Input)->
      input = new Input(type: 'select', name: 'test')

      input.should.be.instanceOf Input
      input._.tagName.should.eql 'SELECT'
      input._.name.should.eql    'test'


  describe "\b#form()", ->

    it "should return reference to it's form object", get_input (input, $, window, document)->
      form = input.form()

      form.should.be.instanceOf $.Form
      form._.should.be.same     document.getElementById('test-form')


  describe '\b#value()', ->
    describe "with a plain input field", ->

      it "should return the value of the field", get_input (input)->
        input.value().should.eql input._.value

    describe "with a multi-select field", ->
      it "should return an array of selected values", get_multiselect (input)->
        input.value().should.eql ['two', 'three']

  describe '\b#value("data")', ->

    describe "with a plain input field", ->

      it "should assign the new value for the field", get_input (input)->
        input._.value = 'old value'
        input.value('new value')
        input._.value.should.eql 'new value'

      it "should return the input field itself back to the code", get_input (input)->
        input.value('another value').should.equal input

    describe "with multi-select", ->
      get = (callback)->
        (done)->
          Browser.open "/input.html", ($, window)->
            input = new $.Input(type: 'select', multiple: true)
            input.html """
              <option value="one">One</option>
              <option value="two" selected="true">Two</option>
              <option value="three" selected="true">Three</option>
              <option value="four">Four</option>
            """
            callback(input, $, window, window.document)
            done()

      it "should assign a new value as an array", get (input)->
        input.value(['one', 'four'])

        input.value().should.eql ['one', 'four']

      it "should assign a new value as a string", get (input)->
        input.value('two')

        input.value().should.eql ['two']

  describe '\b#focus()', ->

    it "should call the raw 'focus' method", get_input (input)->
      called = false
      input._.focus = -> called = true

      input.focus()

      called.should.be.true

    it "should set the 'focused' property of the wrapper", get_input (input)->
      delete(input.focused)

      input.focus()

      input.focused.should.be.true

    it "should return the input field itself back to the code", get_input (input)->
      input.focus().should.equal input

  describe '\b#blur()', ->

    it "should call the raw 'blur' method", get_input (input)->
      called = false
      input._.blur = -> called = true

      input.blur()

      called.should.be.true

    it "should set the 'focused' property of the wrapper to 'false'", get_input (input)->
      delete(input.focused)

      input.blur()

      input.focused.should.be.false

    it "should return the input field itself back to the code", get_input (input)->
      input.blur().should.equal input

  describe '#select()', ->

    it "should call the raw 'select' method", get_input (input)->
      called = false
      input._.select = -> called = true

      input.select()

      called.should.be.true

    it "should set the 'focused' property of the wrapper", get_input (input)->
      delete(input.focused)

      input.select()

      input.focused.should.be.true

    it "should return the input field itself back to the code", get_input (input)->
      input.select().should.equal input


  describe '\b#disable()', ->

    it "should set the 'disabled' property to 'true'", get_input (input)->
      input._.disabled = false
      input.disable()

      input._.disabled.should.be.true

    it "should emit the 'disable' event", get_input (input)->
      emitted = false
      input.on('disable', -> emitted = true)
      input.disable()

      emitted.should.be.true

    it "should return the field itself back to the code", get_input (input)->
      input.disable().should.equal input


  describe '\b#enable()', ->

    it "should set the 'disabled' property to 'false'", get_input (input)->
      input._.disabled = true
      input.enable()

      input._.disabled.should.be.false

    it "should emit the 'enable' event", get_input (input)->
      emitted = false
      input.on('enable', -> emitted = true)
      input.enable()

      emitted.should.be.true

    it "should return the field itself back to the code", get_input (input)->
      input.enable().should.equal input

  describe '\b#disabled', ->

    describe "\b()", ->

      it "should return 'true' when a field is disabled", get_input (input)->
        input._.disabled = true
        input.disabled().should.be.true

      it "should return 'false' when a field is not disabled", get_input (input)->
        input._.disabled = false
        input.disabled().should.be.false

     describe "\b(value)", ->

       it "should call 'disable' when the value is 'true'", get_input (input)->
         called = false
         input.disable = -> called = true; return @
         input.disabled(true)

         called.should.be.true

       it "should call 'enable' when the value is 'false'", get_input (input)->
         called = false
         input.enable = -> called = true; return @
         input.disabled(false)

         called.should.be.true

       it "should return the field itself back to the code", get_input (input)->
         input.disabled(true).should.equal input

  describe '\b#checked', ->

    describe "\b()", ->

      it "should return 'true' when the field is checked", get_input (input)->
        input._.checked = true
        input.checked().should.be.true

      it "should return 'false' when the field is not checked", get_input (input)->
        input._.checked = false
        input.checked().should.be.false

    describe "\b(value)", ->

      it "should make the field checked when called with 'true'", get_input (input)->
        input._.checked = false
        input.checked(true)
        input._.checked.should.be.true

      it "should make the field unchecked when called with 'false'", get_input (input)->
        input._.checked = true
        input.checked(false)
        input._.checked.should.be.false

      it "should return the input field itself back to the code", get_input (input)->
        input.checked(true).should.equal input

  describe 'NodeList extension', ->

    it "should add the 'value()' method", get_Input (Input, $)->
      input = new Input(name: 'boo', value: 'hoo')
      search = new $.NodeList([input])

      search.value.should.be.a 'function'
      search.value().should.equal input.value()
{'fraction_non_alphanumeric': 0.14265357821405952, 'fraction_numerical': 0.0006333122229259025, 'mean_word_length': 2.456585187887632, 'pattern_counts': {'":': 52, '<': 67, '<?xml version=': 0, '>': 206, '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': '5432280', 'n_tokens_mistral': 5598, 'n_tokens_neox': 5360, 'n_words': 1655}