var test = require("tape")
var h = require("virtual-dom/virtual-hyperscript")
var document = require("global/document")
var raf = require("raf")

var mainLoop = require("../index")

test("mainLoop is a function", function (assert) {
    assert.equal(typeof mainLoop, "function")
    assert.end()
})

test("can set up main loop", function (assert) {
    var initState = { fruits: ["apple", "banana"], name: "Steve" }

    function render(state) {
        return h("div", [
            h("div", [
                h("span", "hello "),
                h("span.name", state.name)
            ]),
            h("ul", state.fruits.map(renderFruit))
        ])

        function renderFruit(fruitName) {
            return h("li", [
                h("span", fruitName)
            ])
        }
    }

    // set up a loop
    var loop = mainLoop(initState, render, {
        document: document,
        create: require("virtual-dom/create-element"),
        diff: require("virtual-dom/diff"),
        patch: require("virtual-dom/patch")
    })
    document.body.appendChild(loop.target)

    var div = loop.target
    var span = div.childNodes[0].childNodes[1]
    var ul = div.childNodes[1]

    assert.equal(div.tagName, "DIV")
    assert.equal(span.childNodes[0].data, "Steve")
    assert.equal(ul.childNodes.length, 2)

    // update the loop with the new application state
    loop.update({
        fruits: ["apple", "banana", "cherry"],
        name: "Steve"
    })

    raf(function () {
        assert.equal(ul.childNodes.length, 3)

        loop.update({
            fruits: ["apple", "banana", "cherry"],
            name: "Stevie"
        })

        raf(function () {
            assert.equal(span.childNodes[0].data, "Stevie")

            document.body.removeChild(loop.target)
            assert.end()
        })
    })
})

test("loop.state exposed", function (assert) {
    var loop = mainLoop({ n: 0 }, render, {
        document: document,
        create: require("virtual-dom/create-element"),
        diff: require("virtual-dom/diff"),
        patch: require("virtual-dom/patch")
    })
    assert.equal(loop.state.n, 0)
    loop.update({ n: 4 })
    assert.equal(loop.state.n, 4)
    assert.end()
 
    function render(state) {
        return h('div', String(state.n))
    }
})

@@ -2204,17 +2204,16 @@
 t.end()%0A
- 
 %0A    fun
@@ -2283,9 +2283,727 @@
     %7D%0A%7D)
+%0A%0Atest(%22render called with monotonically increasing times%22, function(assert)%7B%0A    assert.plan(2)%0A%0A    var times = %5B%5D%0A    var loop = mainLoop(%7B n: 0 %7D, render, %7B%0A        document: document,%0A        create: require(%22virtual-dom/create-element%22),%0A        diff: require(%22virtual-dom/diff%22),%0A        patch: require(%22virtual-dom/patch%22)%0A    %7D)%0A%0A%0A    function render(state, time) %7B%0A        times.push(time);%0A        return h('div', String(state.n))%0A    %7D%0A%0A%0A    raf(function () %7B%0A%0A        loop.update(%7B n: 1%7D)%0A%0A        raf(function () %7B%0A            loop.update(%7B n: 2%7D)%0A%0A            assert.equal(times.length, 2)%0A            assert.ok(times%5B0%5D %3C times%5B1%5D, %22should be increasing%22)%0A            assert.end()%0A        %7D)%0A    %7D)%0A%7D);
 %0A

test for new time value