@zeix/ui-element
Version:
UIElement - minimal reactive framework based on Web Components
307 lines (274 loc) • 7.92 kB
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,
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",
);
});
});
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",
);
});
});
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",
);
});
});
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",
);
});
});
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",
);
});
});
});
</script>
</body>
</html>