test-urlchange.html 5.1 KB
Newer Older
S Anand's avatar
S Anand committed
1 2 3 4 5 6 7 8 9 10
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>urlchange tests</title>
  <script src="../node_modules/jquery/dist/jquery.min.js"></script>
  <script src="../dist/event.min.js"></script>
  <script src="../dist/urlfilter.min.js"></script>
  <script src="../dist/urlchange.min.js"></script>
  <script src="tape.js"></script>
11
  <script src="tape-stream.js"></script>
S Anand's avatar
S Anand committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25
</head>

<body>
  <script>
    function reset(hash) {
      $(window).off('.urlchange').off('.test')
      history.pushState('', document.title, window.location.pathname + window.location.search)
      if (hash)
        location.hash = hash
      $(window).urlchange()
    }
  </script>

  <script>
26
    tape('#?高 triggers # and #?高 events', function(t) {
S Anand's avatar
S Anand committed
27
      reset()
28 29 30 31 32 33 34
      t.plan(9)
      $(window).one('#.test', function(e, hash) {
        t.deepEqual(hash, e.hash)
        t.deepEqual(hash.searchList, { '': [''] })
        t.equal(e.old.pathname, '')
        t.deepEqual(e.old.searchList, {})
        t.deepEqual(e.change, { '': [''] })
S Anand's avatar
S Anand committed
35 36 37
      })
      $(window).one('#?高.test', function(e, val) {
        t.equal(val, '')
38 39 40
        t.deepEqual(e.vals, [''])
        t.deepEqual(e.hash.searchList, { '': [''] })
        t.deepEqual(e.old.searchList, {})
S Anand's avatar
S Anand committed
41 42 43 44
      })
      window.location.hash = '#?高'
    })

45
    tape('#?高=λ&►=σ trigges #, #?高 and #?► events', function(t) {
S Anand's avatar
S Anand committed
46
      reset()
47 48 49 50 51 52 53
      t.plan(13)
      $(window).one('#.test', function (e, hash) {
        t.deepEqual(hash, e.hash)
        t.deepEqual(hash.searchList, { '': ['λ'], '': ['σ'] })
        t.equal(e.old.pathname, '')
        t.deepEqual(e.old.searchList, {})
        t.deepEqual(e.change, { '': ['λ'], '': ['σ'] })
S Anand's avatar
S Anand committed
54 55 56
      })
      $(window).one('#?高.test', function (e, val) {
        t.equal(val, 'λ')
57 58 59
        t.deepEqual(e.vals, ['λ'])
        t.deepEqual(e.hash.searchList, { '': ['λ'], '': ['σ'] })
        t.deepEqual(e.old.searchList, {})
S Anand's avatar
S Anand committed
60 61 62
      })
      $(window).one('#?►.test', function (e, val) {
        t.equal(val, 'σ')
63 64 65
        t.deepEqual(e.vals, ['σ'])
        t.deepEqual(e.hash.searchList, { '': ['λ'], '': ['σ'] })
        t.deepEqual(e.old.searchList, {})
S Anand's avatar
S Anand committed
66 67 68 69 70 71
      })
      window.location.hash = '#?高=λ&►=σ'
    })

    tape('Adding #?y=2 on triggers #?y events, not #?x', function(t) {
      reset('#?x=1')
72 73 74
      t.plan(9)     // One less than no. of tests. #?x.test should not be fired
      $(window).one('#.test', function (e, hash) {
        t.deepEqual(hash, e.hash)
S Anand's avatar
S Anand committed
75
        t.deepEqual(e.hash.searchList, { 'x': ['1'], 'y': ['2'] })
76 77
        t.equal(e.old.pathname, '')
        t.deepEqual(e.old.searchList, { 'x': ['1'] })
S Anand's avatar
S Anand committed
78 79 80 81
        t.deepEqual(e.change, { 'y': ['2'] })
      })
      $(window).one('#?y.test', function (e, val) {
        t.equal(val, '2')
82 83 84
        t.deepEqual(e.vals, ['2'])
        t.deepEqual(e.hash.searchList, { 'x': ['1'], 'y': ['2'] })
        t.deepEqual(e.old.searchList, { 'x': ['1'] })
S Anand's avatar
S Anand committed
85 86 87 88 89 90 91
      })
      $(window).one('#?x.test', function (e, val) {
        t.fail('#?x should not be triggered')
      })
      window.location.hash = '#?x=1&y=2'
    })

Karmanya Aggarwal's avatar
Karmanya Aggarwal committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    tape('clearing x should still trigger an event', function(t) {
      reset('#?x=1')
      t.plan(2)     // One less than no. of tests. #?x.test should not be fired
      $(window).one('#?x', function (e, val) {
        t.deepEqual(e.hash.searchList, {})
        t.deepEqual(e.old.searchList, {'x': ['1']})
      })
      window.location.hash = ''
    })

    tape('Adding #?abc=1&xyz=2 triggers on both abc and xyz events', function(t) {
      reset('#?abc=1&xyz=1')
      t.plan(3)     // One less than no. of tests. #?x.test should not be fired
      $(window).one('#?abc #?xyz', function (e, val) {
        t.equal(val, '2')
        t.deepEqual(e.hash.searchList, { 'abc': ['1'], 'xyz': ['2'] })
        t.deepEqual(e.old.searchList,  { 'abc': ['1'], 'xyz': ['1'] })
      })
      window.location.hash = '#?abc=1&xyz=2'
    })

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    tape('#a?x=1&y=2&z=3 -> #b?x=1&x=2&y=3', function(t) {
      reset('#a?x=1&y=2&z=3')
      t.plan(18)
      $(window).one('#.test', function (e, hash) {
        t.deepEqual(hash, e.hash)
        t.equal(e.old.pathname, 'a')
        t.equal(hash.pathname, 'b')
        t.deepEqual(e.old.searchList, { 'x': ['1'], 'y': ['2'], 'z': ['3'] })
        t.deepEqual(hash.searchList, { 'x': ['1', '2'], 'y': ['3'] })
        t.deepEqual(e.change, { 'x': ['1', '2'], 'y': ['3'], 'z': [], '/': 'b' })
      })
      $(window).one('#?x.test', function (e, val) {
        t.equal(val, '1')
        t.deepEqual(e.vals, ['1', '2'])
        t.deepEqual(e.hash.searchList['x'], ['1', '2'])
        t.deepEqual(e.old.searchList['x'], ['1'])
      })
      $(window).one('#?y.test', function (e, val) {
        t.equal(val, '3')
        t.deepEqual(e.vals, ['3'])
        t.deepEqual(e.hash.searchList['y'], ['3'])
        t.deepEqual(e.old.searchList['y'], ['2'])
      })
      $(window).one('#?z.test', function (e, val) {
        t.equal(val, '')
        t.deepEqual(e.vals, [])
        t.deepEqual(e.hash.searchList['z'], undefined)
        t.deepEqual(e.old.searchList['z'], ['3'])
      })
      window.location.hash = '#b?x=1&x=2&y=3'
    })
S Anand's avatar
S Anand committed
144 145 146 147
  </script>
</body>

</html>