'use strict';
/* global describe, it */
let assert = require('assert');
let {run} = require('@motorcycle/core');
let Fixture89 = require('./fixtures/issue-89');
let most = require('most');
let {h, svg, div, input, p, span, h2, h3, h4, select, option, makeDOMDriver} = require('../../src');

function createRenderTarget(id = null) {
  let element = document.createElement('div');
  element.className = 'cycletest';
  if (id) {
    element.id = id;
  }
  document.body.appendChild(element);
  return element;
}

describe('makeDOMDriver', function () {
  it('should accept a DOM element as input', function () {
    const element = createRenderTarget();
    assert.doesNotThrow(function () {
      makeDOMDriver(element);
    });
  });

  it('should accept a DocumentFragment as input', function () {
    const element = document.createDocumentFragment();
    assert.doesNotThrow(function () {
      makeDOMDriver(element);
    });
  });

  it('should accept a string selector to an existing element as input', function () {
    const id = 'testShouldAcceptSelectorToExisting';
    const element = createRenderTarget();
    element.id = id;
    assert.doesNotThrow(function () {
      makeDOMDriver('#' + id);
    });
  });

  it('should not accept a selector to an unknown element as input', function () {
    assert.throws(function () {
      makeDOMDriver('#nonsenseIdToNothing');
    }, /Given container is not a DOM element neither a selector string/);
  });

  it('should not accept a number as input', function () {
    assert.throws(function () {
      makeDOMDriver(123);
    }, /Given container is not a DOM element neither a selector string/);
  });
});

describe('DOM Driver', function () {
  it('should throw if input is not an Observable<VTree>', function () {
    const domDriver = makeDOMDriver(createRenderTarget());
    assert.throws(function () {
      domDriver({});
    }, /The DOM driver function expects as input an Observable of virtual/);
  });

  it('should have isolateSource() and isolateSink() in source', function (done) {
    function app() {
      return {
        DOM: most.just(div())
      };
    }

    const {sinks, sources, dispose} = run(app, {
      DOM: makeDOMDriver(createRenderTarget())
    });

    assert.strictEqual(typeof sources.DOM.isolateSource, 'function');
    assert.strictEqual(typeof sources.DOM.isolateSink, 'function');
    dispose();
    done();
  });
});

@@ -1655,308 +1655,1150 @@
 %7D);%0A
-%7D);%0A%0Adescribe('DOM Driver', function () %7B%0A  it('should throw if input is not an Observable%3CVTree%3E', function () %7B%0A    const domDriver = makeDOMDriver(createRenderTarget());%0A    assert.throws(function () %7B%0A      domDriver(%7B%7D);%0A    %7D, /The DOM driver function expects as input an Observable of virtual/
+%0A  it('should accept function as error callback', function () %7B%0A    const element = document.createDocumentFragment();%0A    const onError = function() %7B%7D;%0A    assert.doesNotThrow(function () %7B%0A      makeDOMDriver(element, %7BonError%7D);%0A    %7D);%0A  %7D);%0A%0A  it('should not accept number as error callback', function () %7B%0A    const element = document.createDocumentFragment();%0A    assert.throws(function () %7B%0A      makeDOMDriver(element, %7BonError: 42%7D);%0A    %7D);%0A  %7D);%0A%7D);%0A%0Adescribe('DOM Driver', function () %7B%0A  it('should throw if input is not an Observable%3CVTree%3E', function () %7B%0A    const domDriver = makeDOMDriver(createRenderTarget());%0A    assert.throws(function () %7B%0A      domDriver(%7B%7D);%0A    %7D, /The DOM driver function expects as input an Observable of virtual/);%0A  %7D);%0A%0A  it('should pass errors to error callback', function (done) %7B%0A    const error = new Error();%0A    const errorCallback = function(e) %7B%0A      assert.strictEqual(e, error);%0A      done();%0A    %7D;%0A%0A    function app() %7B%0A      return %7B%0A        DOM: most.throwError(error)%0A      %7D;%0A    %7D%0A%0A    run(app, %7B%0A      DOM: makeDOMDriver(createRenderTarget(), %7BonError: errorCallback%7D)%0A    %7D
 );%0A 

add tests for onError option