UNPKG

@zeix/ui-element

Version:

UIElement - a HTML-first library for reactive Web Components

581 lines (519 loc) 14.8 kB
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Parse Attribute Tests</title> </head> <body> <script type="module"> import { runTests } from '@web/test-runner-mocha' import { assert } from '@esm-bundle/chai' import { asBoolean, asInteger, asNumber, asString, asEnum, asJSON, } from '../index.dev.js' runTests(() => { const body = document.querySelector('body') describe('asBoolean', function () { it('should be true for empty string', function () { const result = asBoolean()(body, '') assert.isTrue( result, 'Should return true for boolean attribute', ) }) it('should be true for any string', function () { const result = asBoolean()(body, 'any') assert.isTrue( result, 'Should return true for any defined attribute', ) }) it('should be false for undefined', function () { const result = asBoolean()() assert.isFalse( result, 'Should return false for undefined attribute', ) }) it('should be false for null', function () { const result = asBoolean()(body, null) assert.isFalse( result, 'Should return false for null attribute', ) }) it('should be false for "false" string', function () { const result = asBoolean()(body, 'false') assert.isFalse( result, 'Should return false for "false" string', ) }) it('should be true for "0" string', function () { const result = asBoolean()(body, '0') assert.isTrue( result, 'Should return true for "0" string (only "false" is falsy)', ) }) }) describe('asInteger()', function () { it('should be 0 for empty string', function () { const result = asInteger()(body, '') assert.equal( result, 0, 'Should return 0 for boolean attribute', ) }) it('should be 0 for non-parsable string', function () { const result = asInteger()(body, 'any') assert.equal( result, 0, 'Should return 0 for non-parsable attribute', ) }) it('should be 0 for undefined', function () { const result = asInteger()() assert.equal( result, 0, 'Should return 0 for undefined attribute', ) }) it('should be 42 for "42"', function () { const result = asInteger()(body, '42') assert.equal( result, 42, 'Should return number for parsable attribute with integer value', ) }) it('should be 3 for "3.14"', function () { const result = asInteger()(body, '3.14') assert.equal( result, 3, 'Should return number for parsable attribute with floating point value', ) }) it('should be 9007199254740992 for String(Number.MAX_SAFE_INTEGER + 1)', function () { const result = asInteger()( body, String(Number.MAX_SAFE_INTEGER + 1), ) assert.equal( result, 9007199254740992, 'Should return number for parsable attribute with integer outside safe range', ) }) it('should be -4 for " -4 "', function () { const result = asInteger()(body, ' -4 ') assert.equal( result, -4, 'Should return number for parsable attribute with integer and ignored whitespace', ) }) it('should use custom fallback for undefined', function () { const result = asInteger(42)() assert.equal( result, 42, 'Should return custom fallback for undefined attribute', ) }) it('should use custom fallback for null', function () { const result = asInteger(99)(body, null) assert.equal( result, 99, 'Should return custom fallback for null attribute', ) }) it('should be 100000 for "1e5"', function () { const result = asInteger()(body, '1e5') assert.equal( result, 100000, 'Should parse scientific notation', ) }) it('should be 255 for "0xFF"', function () { const result = asInteger()(body, '0xFF') assert.equal( result, 255, 'Should parse hexadecimal notation', ) }) it('should be 0 for "Infinity"', function () { const result = asInteger()(body, 'Infinity') assert.equal( result, 0, 'Should return fallback for Infinity (not finite)', ) }) it('should be 0 for "NaN"', function () { const result = asInteger()(body, 'NaN') assert.equal( result, 0, 'Should return fallback for NaN', ) }) }) describe('asNumber()', function () { it('should be 0 for empty string', function () { const result = asNumber()(body, '') assert.equal( result, 0, 'Should return 0 for boolean attribute', ) }) it('should be 0 for non-parsable string', function () { const result = asNumber()(body, 'any') assert.equal( result, 0, 'Should return 0 for non-parsable attribute', ) }) it('should be 0 for undefined', function () { const result = asNumber()() assert.equal( result, 0, 'Should return 0 for undefined attribute', ) }) it('should be 42 for "42"', function () { const result = asNumber()(body, '42') assert.equal( result, 42, 'Should return number for parsable attribute with integer value', ) }) it('should be 3.14 for "3.14"', function () { const result = asNumber()(body, '3.14') assert.equal( result, 3.14, 'Should return number for parsable attribute with floating point value', ) }) it('should be 9007199254740991.1 for String(Number.MAX_SAFE_INTEGER + 0.1)', function () { const result = asNumber()( body, String(Number.MAX_SAFE_INTEGER + 0.1), ) assert.equal( result, 9007199254740991.1, 'Should return number for parsable attribute with floating point value outside safe range', ) }) it('should be -4 for " -4 "', function () { const result = asNumber()(body, ' -4 ') assert.equal( result, -4, 'Should return number for parsable attribute with integer value and ignored whitespace', ) }) it('should use custom fallback for undefined', function () { const result = asNumber(3.14)() assert.equal( result, 3.14, 'Should return custom fallback for undefined attribute', ) }) it('should use custom fallback for null', function () { const result = asNumber(2.718)(body, null) assert.equal( result, 2.718, 'Should return custom fallback for null attribute', ) }) it('should be 100000 for "1e5"', function () { const result = asNumber()(body, '1e5') assert.equal( result, 100000, 'Should parse scientific notation', ) }) it('should be 0.0025 for "2.5e-3"', function () { const result = asNumber()(body, '2.5e-3') assert.equal( result, 0.0025, 'Should parse negative scientific notation', ) }) it('should be 0 for "Infinity"', function () { const result = asNumber()(body, 'Infinity') assert.equal( result, 0, 'Should return fallback for Infinity (not finite)', ) }) }) describe('asString()', function () { it('should be "" for undefined', function () { const result = asString()() assert.equal( result, '', 'Should return "" for undefined attribute', ) }) it('should be "" for boolean attribute', function () { const result = asString()(body, '') assert.equal( result, '', 'Should return "" for boolean attribute', ) }) it('should be "true" for boolean attribute', function () { const result = asString()(body, 'true') assert.equal( result, 'true', 'Should return "true" string for attribute with boolean value', ) }) it('should be "42" for 42', function () { const result = asString()(body, '42') assert.equal( result, '42', 'Should return string for attribute with integer value', ) }) it('should be "3.14" for 3.14', function () { const result = asString()(body, '3.14') assert.equal( result, '3.14', 'Should return string for attribute with floating point value', ) }) it('should be "foo" for "foo"', function () { const result = asString()(body, 'foo') assert.equal( result, 'foo', 'Should return string for attribute with string value', ) }) it('should be "{ "foo": "bar" }" for "{ "foo": "bar" }"', function () { const result = asString()(body, '{ "foo": "bar" }') assert.equal( result, '{ "foo": "bar" }', 'Should return string for attribute with JSON value', ) }) it('should use custom fallback for undefined', function () { const result = asString('default')() assert.equal( result, 'default', 'Should return custom fallback for undefined attribute', ) }) it('should use custom fallback for null', function () { const result = asString('fallback')(body, null) assert.equal( result, 'fallback', 'Should return custom fallback for null attribute', ) }) it('should preserve whitespace-only strings', function () { const result = asString()(body, ' ') assert.equal( result, ' ', 'Should preserve whitespace-only strings', ) }) }) describe('asEnum()', function () { it('should return first option for undefined', function () { const result = asEnum(['on', 'off'])() assert.equal( result, 'on', 'Should return first valid option for undefined attribute', ) }) it('should return first option for null', function () { const result = asEnum(['yes', 'no', 'maybe'])( body, null, ) assert.equal( result, 'yes', 'Should return first valid option for null attribute', ) }) it('should return first option for invalid value', function () { const result = asEnum(['small', 'medium', 'large'])( body, 'invalid', ) assert.equal( result, 'small', 'Should return first valid option for invalid value', ) }) it('should return matching value case-sensitively', function () { const result = asEnum(['On', 'Off'])(body, 'On') assert.equal( result, 'On', 'Should return exact matching value', ) }) it('should match case-insensitively but return original', function () { const result = asEnum(['TRUE', 'FALSE'])(body, 'true') assert.equal( result, 'true', 'Should match case-insensitively but return original input value', ) }) it('should work with single option', function () { const result = asEnum(['only'])(body, 'anything') assert.equal( result, 'only', 'Should return single option for any input', ) }) }) describe('asJSON()', function () { it('should throw for undefined', function () { assert.throws(() => asJSON()(), ReferenceError) }) it('should throw for boolean attribute', function () { assert.throws(() => asJSON({})(body, ''), SyntaxError) }) it('should be {} for "null"', function () { const result = asJSON({})(body, 'null') assert.deepEqual( result, {}, 'Should return {} for attribute with "null" value', ) }) it('should be {} for "{}"', function () { const result = asJSON({})(body, '{}') assert.deepEqual( result, {}, 'Should return empty object for attribute with "{}" value', ) }) it('should be [] for "[]"', function () { const result = asJSON({})(body, '[]') assert.deepEqual( result, [], 'Should return empty array for attribute with "[]" value', ) }) it('should be { foo: \'bar\' } for "{ "foo": "bar" }"', function () { const result = asJSON({})(body, '{ "foo": "bar" }') assert.deepEqual( result, { foo: 'bar' }, 'Should return JSON for attribute with JSON value with string', ) }) it('should be { 42: true } for "{ "42": true }"', function () { const result = asJSON({})(body, '{ "42": true }') assert.deepEqual( result, { 42: true }, 'Should return JSON for attribute with JSON value with numeric key', ) }) it('should return array fallback for null', function () { const result = asJSON(['default'])(body, null) assert.deepEqual( result, ['default'], 'Should return array fallback for null attribute', ) }) it('should parse nested objects', function () { const result = asJSON({})( body, '{"user": {"name": "John", "age": 30}}', ) assert.deepEqual( result, { user: { name: 'John', age: 30 } }, 'Should parse nested JSON objects', ) }) it('should parse arrays with mixed types', function () { const result = asJSON([])( body, '[1, "two", true, null]', ) assert.deepEqual( result, [1, 'two', true, null], 'Should parse arrays with mixed types', ) }) it('should throw for malformed JSON', function () { assert.throws( () => asJSON({})(body, '{"invalid": json}'), SyntaxError, 'Failed to parse JSON:', ) }) it('should throw for JSON with trailing comma', function () { assert.throws( () => asJSON({})(body, '{"valid": true,}'), SyntaxError, 'Failed to parse JSON:', ) }) it('should handle JSON with escaped characters', function () { const result = asJSON({})( body, '{"message": "Hello\\nWorld\\t!"}', ) assert.deepEqual( result, { message: 'Hello\nWorld\t!' }, 'Should handle JSON with escaped characters', ) }) }) }) </script> </body> </html>