Skip to content

Commit

Permalink
Added query parser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcodpt committed Sep 19, 2024
1 parent 2c10de0 commit c990c27
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

## ❤️ Features
- ES6 module.
- [ES6 module](https://github.com/marcodpt/wand/blob/main/index.js).
- Support browser and any javascript runtime
([node](https://nodejs.org/en), [deno](https://deno.com/), etc).
- Pure functional design.
Expand All @@ -16,7 +16,7 @@ very understandable.
[plugin](https://github.com/marcodpt/wand/blob/main/src/queryParser.js).
- Designed following the principles of
[UNIX philosophy](https://en.wikipedia.org/wiki/Unix_philosophy).
- Very well tested.
- Very well [tested](https://marcodpt.github.io/wand/tests/).
- Ridiculously small [API](#-api). After reading this file you will
understand `Wand` better than me.

Expand Down
13 changes: 13 additions & 0 deletions tests/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<link rel="shortcut icon" type="image/x-icon" href="../favicon.ico">
<title>Wand Tests</title>
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.20.1.css">
</head>
<body>
<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="./query_parser.js"></script>
</body>
96 changes: 96 additions & 0 deletions tests/query_parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
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)
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.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')
})
}
})

0 comments on commit c990c27

Please sign in to comment.