<commit_before>var dom           = require('./dom')('<html><body></body></html>')
var ReactDatalist = require('../react-datalist')
var assert        = require('assert')
var _             = require('lodash')
var React         = require('react')
var nanodom       = require('nanodom')
var ReactAddons   = require('react/addons')

var ReactTestUtils = React.addons.TestUtils

/** VARIABLES **/

var filterSetter;
var setFilter    = function(fn) { filterSetter = fn }
var options      = ['apple','orange','pear','pineapple','melon']
var defaultProps = {options:options, list:'fruit', forcePoly:true, setFilter:setFilter}

/** HELPER FUNCTIONS **/

function render(props, callback) {
    return React.renderComponent(ReactDatalist(_.merge(defaultProps, props)), document.body, function() {
        if (typeof callback === 'function') setTimeout(callback)
    })
}
function setInputValue(datalist, value) {
     var __input   = nanodom('.react-datalist-input')[0]
     var _input    = ReactTestUtils.findRenderedDOMComponentWithTag(datalist, 'input')
     __input.value = value
    ReactTestUtils.Simulate.change(_input)
}
function clickOption(datalist, index) {
    var _options = ReactTestUtils.scryRenderedDOMComponentsWithClass(datalist, 'react-datalist-option')
    ReactTestUtils.Simulate.click(_options[index])
}

/** TESTS **/

describe('DATALIST', function() {

    afterEach(function(done) {
        React.unmountComponentAtNode(document.body)
        document.body.innerHTML = ""
        setTimeout(done)
    })

    it('Should render a container, an input and a datalist', function(done) {
        render({},function() {
            var __datalist = nanodom('.react-datalist')
            assert(__datalist.length == 1)
            var __container = nanodom('.react-datalist-container')
            assert(__container.length == 1)
            var __input = nanodom('.react-datalist-input')
            assert(__input.length == 1)
            done()            
        })
    })

    it('Should be able to filter options', function(done) {
        var _datalist = render({},function() {
            var filtered = _datalist.filterOptions(options, null)
            assert(filtered.length === options.length)
            filtered = _datalist.filterOptions(options)
            assert(filtered.length === options.length)
            filtered = _datalist.filterOptions(options, "p")
            assert(filtered.length === 3)
            filtered = _datalist.filterOptions(options, "lon")
            assert(filtered.length === 1)
            done()            
        })
    })

    it('Should support setting initial input value throught initialFilter property', function(done) {
        render({initialFilter : 'meh'}, function() {
            var __input = nanodom('.react-datalist-input')[0]
            assert(__input.value === 'meh')
            done()
        })
    })

    it('Should render a datalist with the passed options', function(done) {
        var _datalist = render({}, function() {
            setInputValue(_datalist, '')
            var domlist = nanodom('.react-datalist')
            assert(domlist.length == 1)
            assert(domlist[0].childNodes.length == options.length)
            assert(domlist[0].id == 'fruit')
            done()
        })
    })

    it('Filter options should change when the input value changes', function(done) {
        var _datalist = render({}, function() {
            var __datalist = nanodom('.react-datalist')[0]
            setInputValue(_datalist, '')
            assert(__datalist.childNodes.length == options.length)
            setInputValue(_datalist, 'melon')
            assert(__datalist.childNodes.length == 1)
            done()
        })
    })

    it('Should call its callback', function(done) {
        var onInputChange = function(event) {
            assert(true)
            done()
        }
        var _datalist = render({ onInputChange : onInputChange }, function() {
            setInputValue(_datalist, 'p')
        })
    })

    // it('Can navigate options with arrow keys', function() {
    //     setInputValue('')
    //     var __datalist = nanodom('.react-datalist')[0]
    //     var __input    = nanodom('.react-datalist-input')[0]
    //     var _input     = ReactTestUtils.findRenderedDOMComponentWithTag(_datalist, 'input')
    //     assert(__datalist.childNodes.length == options.length)
    //     // Down to first item
    //     ReactTestUtils.Simulate.keyDown(_input, {which: 40, type: "keydown"})
    //     var __option = nanodom('.react-datalist-option-selected')
    //     assert(__option.length == 1)
    //     assert(__option[0].innerHTML == options[0])
    //     // Down to second item
    //     ReactTestUtils.Simulate.keyDown(_input, {which: 40, type: "keydown"})
    //     var __option = nanodom('.react-datalist-option-selected')
    //     assert(__option.length == 1)
    //     assert(__option[0].innerHTML == options[1])
    //     // Up to first item
    //     ReactTestUtils.Simulate.keyDown(_input, {which: 38, type: "keydown"})
    //     var __option = nanodom('.react-datalist-option-selected')
    //     assert(__option.length == 1)
    //     assert(__option[0].innerHTML == options[0])
    // })

    // it('Can select an option by hitting enter', function(done) {
    //     var onOptionSelected = function(option) {
    //         assert(option == options[0])
    //         render(defaultOptions, function() {
    //             done()
    //         })
    //     }
    //     var opts = merge(defaultOptions, {onOptionSelected:onOptionSelected})
    //     render(opts, function() {
    //         setInputValue('')
    //         var __datalist = nanodom('.react-datalist')[0]
    //         var __input    = nanodom('.react-datalist-input')[0]
    //         var _input     = ReactTestUtils.findRenderedDOMComponentWithTag(_datalist, 'input')
    //         assert(__datalist.childNodes.length == options.length)
    //         // Down to an item
    //         ReactTestUtils.Simulate.keyDown(_input, {which: 40, type: "keydown"})
    //         var __option = nanodom('.react-datalist-option-selected')
    //         assert(__option.length == 1)
    //         // Enter to select it
    //         ReactTestUtils.Simulate.keyDown(_input, {which: 13, type: "keydown"})
    //     })
    // })

    // it('Can hide the options by pressing ESC', function(done) {
    //     setInputValue('p')
    //     var __datalist, __input, _input;
    //     _input     = ReactTestUtils.findRenderedDOMComponentWithTag(_datalist, 'input')
    //     ReactTestUtils.Simulate.change(_input)
    //     __datalist = nanodom('.react-datalist')[0]
    //     assert(__datalist.childNodes.length == 3)
    //     // ESC to hide the options
    //     ReactTestUtils.Simulate.keyUp(_input, {which: 27, type: "keyup"})
    //     __datalist = nanodom('.react-datalist')[0]
    //     __input    = nanodom('.react-datalist-input')[0]<commit_msg>More tests reworked<commit_after>var dom           = require('./dom')('<html><body></body></html>')
var ReactDatalist = require('../react-datalist')
var assert        = require('assert')
var _             = require('lodash')
var React         = require('react')
var nanodom       = require('nanodom')
var ReactAddons   = require('react/addons')

var ReactTestUtils = React.addons.TestUtils

/** VARIABLES **/

var filterSetter;
var setFilter    = function(fn) { filterSetter = fn }
var options      = ['apple','orange','pear','pineapple','melon']
var defaultProps = {options:options, list:'fruit', forcePoly:true, setFilter:setFilter}

/** HELPER FUNCTIONS **/

function render(props, callback) {
    return React.renderComponent(ReactDatalist(_({}).merge(defaultProps).merge(props).__wrapped__), document.body, function() {
        if (typeof callback === 'function') setTimeout(callback)
    })
}
function setInputValue(datalist, value) {
     var __input   = nanodom('.react-datalist-input')[0]
     var _input    = ReactTestUtils.findRenderedDOMComponentWithTag(datalist, 'input')
     __input.value = value
    ReactTestUtils.Simulate.change(_input)
}
function clickOption(datalist, index) {
    var _options = ReactTestUtils.scryRenderedDOMComponentsWithClass(datalist, 'react-datalist-option')
    ReactTestUtils.Simulate.click(_options[index])
}

/** TESTS **/

describe('DATALIST', function() {

    afterEach(function(done) {
        React.unmountComponentAtNode(document.body)
        document.body.innerHTML = ""
        setTimeout(done)
    })

    it('Should render a container, an input and a datalist', function(done) {
        render({},function() {
            var __datalist = nanodom('.react-datalist')
            assert(__datalist.length == 1)
            var __container = nanodom('.react-datalist-container')
            assert(__container.length == 1)
            var __input = nanodom('.react-datalist-input')
            assert(__input.length == 1)
            done()            
        })
    })

    it('Should be able to filter options', function(done) {
        var _datalist = render({},function() {
            var filtered = _datalist.filterOptions(options, null)
            assert(filtered.length === options.length)
            filtered = _datalist.filterOptions(options)
            assert(filtered.length === options.length)
            filtered = _datalist.filterOptions(options, "p")
            assert(filtered.length === 3)
            filtered = _datalist.filterOptions(options, "lon")
            assert(filtered.length === 1)
            done()            
        })
    })

    it('Should support setting initial input value throught initialFilter property', function(done) {
        render({initialFilter : 'meh'}, function() {
            var __input = nanodom('.react-datalist-input')[0]
            assert(__input.value === 'meh')
            done()
        })
    })

    it('Should render a datalist with the passed options', function(done) {
        var _datalist = render({}, function() {
            setInputValue(_datalist, '')
            var domlist = nanodom('.react-datalist')
            assert(domlist.length == 1)
            assert(domlist[0].childNodes.length == options.length)
            assert(domlist[0].id == 'fruit')
            done()
        })
    })

    it('Filter options should change when the input value changes', function(done) {
        var _datalist = render({}, function() {
            var __datalist = nanodom('.react-datalist')[0]
            setInputValue(_datalist, '')
            assert(__datalist.childNodes.length == options.length)
            setInputValue(_datalist, 'melon')
            assert(__datalist.childNodes.length == 1)
            done()
        })
    })

    it('Should call its callback', function(done) {
        var onInputChange = function(event) {
            assert(true)
            done()
        }
        var _datalist = render({ onInputChange : onInputChange }, function() {
            setInputValue(_datalist, 'p')
        })
    })

    it('Can navigate options with arrow keys', function(done) {
        var _datalist = render({}, function() {
            setInputValue(_datalist, '')
            var __datalist = nanodom('.react-datalist')[0]
            var __input    = nanodom('.react-datalist-input')[0]
            var _input     = ReactTestUtils.findRenderedDOMComponentWithTag(_datalist, 'input')
            assert(__datalist.childNodes.length == options.length)
            // Down to first item
            ReactTestUtils.Simulate.keyDown(_input, {which: 40, type: "keydown"})
            var __option = nanodom('.react-datalist-option-selected')
            assert(__option.length == 1)
            assert(__option[0].innerHTML == options[0])
            // Down to second item
            ReactTestUtils.Simulate.keyDown(_input, {which: 40, type: "keydown"})
            var __option = nanodom('.react-datalist-option-selected')
            assert(__option.length == 1)
            assert(__option[0].innerHTML == options[1])
            // Up to first item
            ReactTestUtils.Simulate.keyDown(_input, {which: 38, type: "keydown"})
            var __option = nanodom('.react-datalist-option-selected')
            assert(__option.length == 1)
            assert(__option[0].innerHTML == options[0])
            done()
        })
    })

    it('Can select an option by hitting enter', function(done) {
        var onOptionSelected = function(option) {
            assert(option == options[0])
            done()
        }
        var _datalist = render({ onOptionSelected : onOptionSelected }, function() {
            setInputValue(_datalist, '')
            var __datalist = nanodom('.react-datalist')[0]
            var __input    = nanodom('.react-datalist-input')[0]
            var _input     = ReactTestUtils.findRenderedDOMComponentWithTag(_datalist, 'input')
            assert(__datalist.childNodes.length == options.length)
            // Down to an item
            ReactTestUtils.Simulate.keyDown(_input, {which: 40, type: "keydown"})
            var __option = nanodom('.react-datalist-option-selected')
            assert(__option.length == 1)
            // Enter to select it
            ReactTestUtils.Simulate.keyDown(_input, {which: 13, type: "keydown"})
        })
    })

    // it('Can hide the options by pressing ESC', function(done) {
    //     setInputValue('p')
    //     var __datalist, __input, _input;
    //     _input     = ReactTestUtils.findRenderedDOMComponentWithTag(_datalist, 'input')
    //     ReactTestUtils.Simulate.change(_input)
    //     __datalist = nanodom('.react-datalist')[0]
    //     assert(__datalist.childNodes.length == 3)
    //     // ESC to hide the options
    //     ReactTestUtils.Simulate.keyUp(_input, {which: 27, type: "keyup"})
    //     __datalist = nanodom('.react-datalist')[0]
    //     __input    = nanodom('.react-datalist-input')[0]
{'fraction_non_alphanumeric': 0.13700301420984642, 'fraction_numerical': 0.006171953495048084, 'mean_word_length': 2.3675688738521026, 'pattern_counts': {'":': 0, '<': 11, '<?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': '5343158', 'n_tokens_mistral': 4121, 'n_tokens_neox': 3993, 'n_words': 779}