<commit_before>'use strict'

var test = require('tape')
var Router = require('../')

test('hooks', function (t) {
  t.test('global', function (t) {
    t.plan(2)

    var state = Router({
      path: '/packages/sour'
    })
    var values = []

    Router.route(state, {
      path: '/packages/:name',
      render: function () {
        return values.join(',')
      }
    })

    Router.hook(state, 'enter.before', function (params, callback) {
      t.deepEqual(params, {name: 'sour'})
      process.nextTick(function () {
        values.push(0)
        callback()
      })
    })

    Router.hook(state, 'enter.before', function (params, callback) {
      values.push(1)
      callback()
    })

    state.active(function onChange () {
      t.equal(Router.render(state()), '0,1')
    })

    Router.watch(state)
  })
})<commit_msg>Add test for route hooks<commit_after>'use strict'

var test = require('tape')
var Router = require('../')

test('hooks', function (t) {
  t.test('global', function (t) {
    t.plan(2)

    var state = Router({
      path: '/packages/sour'
    })
    var values = []

    Router.route(state, {
      path: '/packages/:name',
      render: function () {
        return values.join(',')
      }
    })

    Router.hook(state, 'enter.before', function (params, callback) {
      t.deepEqual(params, {name: 'sour'})
      process.nextTick(function () {
        values.push(0)
        callback()
      })
    })

    Router.hook(state, 'enter.before', function (params, callback) {
      values.push(1)
      callback()
    })

    state.active(function onChange () {
      t.equal(Router.render(state()), '0,1')
    })

    Router.watch(state)
  })

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

    var state = Router({
      path: '/packages/sour'
    })
    var values = []

    var pkg = Router.route(state, {
      path: '/packages/:name',
      render: function () {
        return values.join(',')
      }
    })

    var other = Router.route(state, {
      path: '/other/route'
    })

    Router.hook(state, 'enter.before', function (params, callback) {
      values.push(0, 1)
      callback()
    })

    Router.hook(state, pkg, 'enter.before', function (params, callback) {
      values.push(2, 3)
      callback()
    })

    Router.hook(state, other, 'enter.before', function () {
      t.fail('other route hook called')
    })

    state.active(function onChange () {
      t.equal(Router.render(state()), '0,1,2,3')
    })

    Router.watch(state)
  })
})
{'fraction_non_alphanumeric': 0.17804391217564872, 'fraction_numerical': 0.007584830339321357, 'mean_word_length': 2.3060686015831133, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2302414', 'n_tokens_mistral': 856, 'n_tokens_neox': 826, 'n_words': 163}