Skip to content

Commit

Permalink
added runtime tests and fix query_parser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcodpt committed Sep 19, 2024
1 parent c990c27 commit 0f9d7e0
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 31 deletions.
1 change: 1 addition & 0 deletions tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="https://code.jquery.com/qunit/qunit-2.20.1.js"></script>
<script type="module" src="./runtime.js"></script>
<script type="module" src="./query_parser.js"></script>
</body>
48 changes: 17 additions & 31 deletions tests/query_parser.js
Original file line number Diff line number Diff line change
@@ -1,96 +1,82 @@
import wand from '../index.js'

var index = 0
var Query = null
var query = null
wand({
routes: {
'*': state => {
index++
query = state.query
Query = state.Query
}
},
runtime: change => {
QUnit.test('query parser', assert => {
assert.deepEqual(index, 0)
QUnit.test('query parser', assert => {
var Query = null
var query = null
wand({
routes: {
'*': state => {
query = state.query
Query = state.Query
}
},
runtime: change => {
assert.deepEqual(Query, null)
assert.deepEqual(query, null)

change('')
assert.deepEqual(index, 1)
assert.deepEqual(Query, {})
assert.deepEqual(query, '')

change('?')
assert.deepEqual(index, 2)
assert.deepEqual(Query, {})
assert.deepEqual(query, '')

change('/route')
assert.deepEqual(index, 3)
assert.deepEqual(Query, {})
assert.deepEqual(query, '')

change('/route?')
assert.deepEqual(index, 4)
assert.deepEqual(Query, {})
assert.deepEqual(query, '')

change('/some/route')
assert.deepEqual(index, 5)
assert.deepEqual(Query, {})
assert.deepEqual(query, '')

change('/some/route?')
assert.deepEqual(index, 6)
assert.deepEqual(Query, {})
assert.deepEqual(query, '')

change('?a=3')
assert.deepEqual(index, 7)
assert.deepEqual(Query, {a: '3'})
assert.deepEqual(query, 'a=3')

change('?a=&b=false&c.d={}')
assert.deepEqual(index, 8)
assert.deepEqual(Query, {a: '', b: 'false', 'c.d': '{}'})
assert.deepEqual(query, 'a=&b=false&c.d={}')

change('?a')
assert.deepEqual(index, 9)
assert.deepEqual(Query, {a: ''})
assert.deepEqual(query, 'a')

change('?a?b??&c?=x')
assert.deepEqual(Query, {'a?b??': '', 'c?': 'x'})
assert.deepEqual(query, 'a?b??&c?=x')

change('?a=&b&c.d={}')
assert.deepEqual(index, 10)
assert.deepEqual(Query, {a: '', b: '', 'c.d': '{}'})
assert.deepEqual(query, 'a=&b&c.d={}')

change('?pet=dog&pet=cat')
assert.deepEqual(index, 11)
assert.deepEqual(Query, {pet: 'cat'})
assert.deepEqual(query, 'pet=dog&pet=cat')

change('?pet[]=dog')
assert.deepEqual(index, 12)
assert.deepEqual(Query, {pet: ['dog']})
assert.deepEqual(query, 'pet[]=dog')

change('?pet[]=dog&pet[]=cat')
assert.deepEqual(index, 13)
assert.deepEqual(Query, {pet: ['dog', 'cat']})
assert.deepEqual(query, 'pet[]=dog&pet[]=cat')

change('?pet=dog&pet[]=cat')
assert.deepEqual(index, 14)
assert.deepEqual(Query, {pet: ['cat']})
assert.deepEqual(query, 'pet=dog&pet[]=cat')

change('?pet[]=dog&pet=cat')
assert.deepEqual(index, 15)
assert.deepEqual(Query, {pet: 'cat'})
assert.deepEqual(query, 'pet[]=dog&pet=cat')
})
}
}
})
})
155 changes: 155 additions & 0 deletions tests/runtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import wand from '../index.js'

QUnit.test('runtime', assert => {
const done = assert.async()
const H = []
const stop = wand({
routes: {
'a': ({url}) => {
H.push('a (action): '+url)
return ({url}) => {H.push('a (done): '+url)}
},
'b': ({url}) => {
H.push('b (action): '+url)
return ({url}) => {H.push('b (done): '+url)}
}
},
runtime: change => {
assert.deepEqual(H, [])

change('c')
assert.deepEqual(H, [])

change('a')
assert.deepEqual(H, [
'a (action): a'
])

change('b')
assert.deepEqual(H, [
'a (action): a',
'a (done): b',
'b (action): b'
])

change('c')
assert.deepEqual(H, [
'a (action): a',
'a (done): b',
'b (action): b'
])

change('b')
assert.deepEqual(H, [
'a (action): a',
'a (done): b',
'b (action): b',
'b (done): b',
'b (action): b'
])

change('a')
assert.deepEqual(H, [
'a (action): a',
'a (done): b',
'b (action): b',
'b (done): b',
'b (action): b',
'b (done): a',
'a (action): a'
])

change('a')
assert.deepEqual(H, [
'a (action): a',
'a (done): b',
'b (action): b',
'b (done): b',
'b (action): b',
'b (done): a',
'a (action): a',
'a (done): a',
'a (action): a'
])

change('c')
assert.deepEqual(H, [
'a (action): a',
'a (done): b',
'b (action): b',
'b (done): b',
'b (action): b',
'b (done): a',
'a (action): a',
'a (done): a',
'a (action): a'
])

setTimeout(() => {
stop()
assert.deepEqual(H, [
'a (action): a',
'a (done): b',
'b (action): b',
'b (done): b',
'b (action): b',
'b (done): a',
'a (action): a',
'a (done): a',
'a (action): a',
'a (done): a',
'finish'
])

change('c')
assert.deepEqual(H, [
'a (action): a',
'a (done): b',
'b (action): b',
'b (done): b',
'b (action): b',
'b (done): a',
'a (action): a',
'a (done): a',
'a (action): a',
'a (done): a',
'finish'
])

change('b')
assert.deepEqual(H, [
'a (action): a',
'a (done): b',
'b (action): b',
'b (done): b',
'b (action): b',
'b (done): a',
'a (action): a',
'a (done): a',
'a (action): a',
'a (done): a',
'finish'
])

change('a')
assert.deepEqual(H, [
'a (action): a',
'a (done): b',
'b (action): b',
'b (done): b',
'b (action): b',
'b (done): a',
'a (action): a',
'a (done): a',
'a (action): a',
'a (done): a',
'finish'
])

done()
})

return ({url}) => {H.push('finish')}
}
})
})

0 comments on commit 0f9d7e0

Please sign in to comment.