var fixture = require('../test/fixtures/behavior'),
  autocomplete = require('./autocomplete'),
  dom = require('../services/dom'),
  sinon = require('sinon');

describe('autocomplete behavior', function () {

  var sandbox,
    fakeApi = '/lists/authors',
    fakeList = [
      'Amy Koran',
      'Zena Strother',
      'Karole Herdt',
      'Sheila Cowell',
      'Josiah Hagaman',
      'Beatris Doetsch',
      'Zachary Brunell',
      'Manuel Grassi',
      'Amie Ridgell',
      'Lupe Harrill',
      'Adriana Bakke',
      'Francoise Lashley',
      'Gwenn Sampley',
      'Randall Coller',
      'Terence Villegas',
      'Matthew Mcconnaughey',
      'Rosella Conroy',
      'Gemma Osburn',
      'Shanel Holt'
    ],
    fakeInput = document.createElement('input');

  beforeEach(function () {

    sandbox = sinon.sandbox.create();

    // reset element in fixture to have no children.
    dom.clearChildren(fixture.el);

  });

  afterEach(function () {
    sandbox.restore();
  });

  it('does not render a datalist if no api', function () {
    fixture.el.appendChild(fakeInput);
    expect(autocomplete(fixture, {}).el.querySelector('datalist')).to.equal(null);
  });

  it('does not render a datalist if no input', function () {
    expect(autocomplete(fixture, {api: fakeApi}).el.querySelector('datalist')).to.equal(null);
  });

  it('formats values into option elements', function () {
    expect(Array.prototype.slice.call(autocomplete.formatOptions(fakeList).querySelectorAll('option')).map(function (x) { return x.textContent; })).to.deep.equal(fakeList);
  });

  it('assigns an unique id to each datalist', function () {
    var firstId, secondId;

    fixture.el.appendChild(fakeInput);
    firstId = autocomplete(fixture, {api: fakeApi}).el.querySelector('datalist').id;

    dom.clearChildren(fixture.el);
    fixture.el.appendChild(fakeInput);
    secondId = autocomplete(fixture, {api: fakeApi}).el.querySelector('datalist').id;

    expect(firstId).to.not.equal(secondId);
  });

});

@@ -127,36 +127,8 @@
 om')
-,%0A  sinon = require('sinon')
 ;%0A%0Ad
@@ -185,21 +185,8 @@
 var 
-sandbox,%0A    
 fake
@@ -766,94 +766,80 @@
     
-sandbox = sinon.sandbox.create();%0A%0A    // reset element in fixture to have no children
+// Autocomplete expects an input element, so resets fixture.el each time
 .%0A  
@@ -875,65 +875,48 @@
 l);%0A
-%0A
   
-%7D);%0A%0A  afterEach(function () %7B%0A    sandbox.restore(
+  fixture.el.appendChild(fakeInput
 );%0A
+%0A
   %7D)
@@ -981,47 +981,8 @@
 ) %7B%0A
-    fixture.el.appendChild(fakeInput);%0A
     
@@ -1120,32 +1120,84 @@
 , function () %7B%0A
+    dom.clearChildren(fixture.el); // Remove input.%0A
     expect(autoc
@@ -1612,47 +1612,8 @@
 d;%0A%0A
-    fixture.el.appendChild(fakeInput);%0A
     
@@ -1668,32 +1668,35 @@
 el.querySelector
+All
 ('datalist').id;
@@ -1695,86 +1695,14 @@
 st')
+%5B0%5D
 .id
-;%0A%0A    dom.clearChildren(fixture.el);%0A    fixture.el.appendChild(fakeInput)
 ;%0A  
@@ -1760,32 +1760,35 @@
 el.querySelector
+All
 ('datalist').id;
@@ -1783,16 +1783,19 @@
 talist')
+%5B1%5D
 .id;%0A%0A  

Tweak tests.