const test = require('tape')
const Event = require('geval/event')
const proxyquire = require('proxyquire')
const view = require('../../html')

test('routing', function (t) {
  t.test('history', function (t) {
    t.plan(3)

    const history = Event()
    const choo = proxyquire('../..', {
      'sheet-router/history': history.listen
    })

    const app = choo()

    app.model({
      state: {
        user: null
      },
      reducers: {
        set: (action, state) => ({user: action.id})
      },
      effects: {
        open: function (action, state, send, done) {
          t.deepEqual(action, {id: 1})
          send('set', {id: 1}, function (err) {
            if (err) return done(err)
            history.broadcast('https://foo.com/users/1')
          })
        }
      }
    })

    app.router('/users', (route) => [
      route('/users', parentView, [
        route('/:user', childView)
      ])
    ])

    const tree = app.start()
    document.body.appendChild(tree)

    t.equal(tree.innerHTML.trim(), 'Open')
    tree.onclick()

    function parentView (state, prev, send) {
      return view`
        <button onclick=${() => send('open', {id: 1})}>
          Open
        </button>
      `
    }

    function childView (state, prev, send) {
      t.equal(state.user, 1)
      return view`<div>${state.user}</div>`
    }
  })

  t.test('hash', function (t) {
    t.plan(1)

    const hash = Event()
    const choo = proxyquire('../..', {
      'sheet-router/hash': hash.listen
    })

    const app = choo()

    app.model({
      state: {
        user: null
      },
      reducers: {
        set: (action, state) => ({user: action.id})
      },
      effects: {
        open: function (action, state, send, done) {
          send('set', {id: 1}, function (err) {
            if (err) return done(err)
            hash.broadcast('#users/1')
          })
        }
      }
    })

    app.router('/users', (route) => [
      route('/users', parentView, [
        route('/:user', childView)
      ])
    ])

    const tree = app.start({hash: true})
    document.body.appendChild(tree)

    tree.onclick()

    function parentView (state, prev, send) {
      return view`
        <button onclick=${() => send('open', {id: 1})}>
          Open
        </button>
      `
    }

    function childView (state, prev, send) {
      t.equal(state.user, 1)
      return view`<div>${state.user}</div>`
    }
  })
})

@@ -2419,12 +2419,698 @@
   %7D%0A  %7D)
+%0A%0A  t.test('disabling history', function (t) %7B%0A    t.plan(1)%0A%0A    const choo = proxyquire('../..', %7B%0A      'sheet-router/history': () =%3E t.fail('history listener attached')%0A    %7D)%0A%0A    const app = choo()%0A%0A    app.router('/', (route) =%3E %5B%0A      route('/', function () %7B%0A        t.pass('rendered')%0A      %7D)%0A    %5D)%0A%0A    app.start(%7Bhistory: false%7D)%0A  %7D)%0A%0A  t.test('disabling href', function (t) %7B%0A    t.plan(1)%0A%0A    const choo = proxyquire('../..', %7B%0A      'sheet-router/href': () =%3E t.fail('href listener attached')%0A    %7D)%0A%0A    const app = choo()%0A%0A    app.router('/', (route) =%3E %5B%0A      route('/', function () %7B%0A        t.pass('rendered')%0A      %7D)%0A    %5D)%0A%0A    app.start(%7Bhref: false%7D)%0A  %7D)
 %0A%7D)%0A

Add test coverage for disabling sheet-router history/href listeners