htmx/test/core/tokenizer.js
MichaelWest22 24a0106f76
Update testing framework to web-test-runner and improve code coverage (#3273)
* Fix old npm dependencies

* implement web-test-runner tests for headless alongside Mocha browser tests

* Increase test and code coverage

* update to 100% coverage and impove eslint

* Update testing Doco

* revert all htmx changes and updates/disable tests needed

* fix browser mocha test

* Default testing to use playwrite only instead of puppeter

* playwright install fix

* Imporve test summary reporting

* flatten false looks closer to original
2025-04-17 17:55:43 -06:00

44 lines
1.4 KiB
JavaScript

describe('Core htmx tokenizer tests', function() {
beforeEach(function() {
this.server = makeServer()
clearWorkArea()
})
afterEach(function() {
this.server.restore()
clearWorkArea()
})
function tokenize(str) {
return htmx._('tokenizeString')(str)
}
function tokenizeTest(str, result) {
return tokenize(str).should.deep.equal(result)
}
it('tokenizes properly', function() {
tokenizeTest('', [])
tokenizeTest(' ', [' ', ' '])
tokenizeTest('(', ['('])
tokenizeTest('()', ['(', ')'])
tokenizeTest('(,)', ['(', ',', ')'])
tokenizeTest(' ( ) ', [' ', '(', ' ', ')', ' '])
tokenizeTest(' && ) ', [' ', '&', '&', ' ', ')', ' '])
tokenizeTest(" && ) 'asdf'", [' ', '&', '&', ' ', ')', ' ', "'asdf'"])
tokenizeTest(" && ) ',asdf'", [' ', '&', '&', ' ', ')', ' ', "',asdf'"])
tokenizeTest('",asdf"', ['",asdf"'])
tokenizeTest('&& ) ",asdf"', ['&', '&', ' ', ')', ' ', '",asdf"'])
tokenizeTest('",as\\"df"', ['",as\\"df"'])
})
it('generates conditionals property', function() {
var tokens = tokenize('[code==4||(code==5&&foo==true)]')
var conditional = htmx._('maybeGenerateConditional')(null, tokens)
var func = eval(conditional)
func({ code: 5, foo: true }).should.equal(true)
func({ code: 5, foo: false }).should.equal(false)
func({ code: 4, foo: false }).should.equal(true)
func({ code: 3, foo: true }).should.equal(false)
})
})