@homebound/truss 2.5.1 → 2.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/vitest.d.ts +33 -0
- package/build/vitest.js +59 -0
- package/build/vitest.js.map +1 -0
- package/package.json +6 -1
- package/tsup.config.ts +1 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** Supported style expectations for the matcher. */
|
|
2
|
+
type StyleExpectation = string | Record<string, string | number>;
|
|
3
|
+
/** Minimal subset of Vitest's matcher context used for error formatting. */
|
|
4
|
+
type MatcherContext = {
|
|
5
|
+
utils?: {
|
|
6
|
+
printExpected?: (value: unknown) => string;
|
|
7
|
+
printReceived?: (value: unknown) => string;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
/** Standard matcher result shape returned to Vitest. */
|
|
11
|
+
type MatcherResult = {
|
|
12
|
+
pass: boolean;
|
|
13
|
+
message: () => string;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Assert that an element's computed style matches the provided CSS declarations.
|
|
17
|
+
*
|
|
18
|
+
* Static class-based styles are read via `getComputedStyle`, while CSS custom
|
|
19
|
+
* properties are read directly from the element's inline style because jsdom
|
|
20
|
+
* does not resolve them through computed styles.
|
|
21
|
+
*/
|
|
22
|
+
declare function toHaveStyle(this: MatcherContext, received: unknown, expected: StyleExpectation): MatcherResult;
|
|
23
|
+
|
|
24
|
+
declare module "vitest" {
|
|
25
|
+
interface Assertion<T = any> {
|
|
26
|
+
toHaveStyle(expected: StyleExpectation): T;
|
|
27
|
+
}
|
|
28
|
+
interface AsymmetricMatchersContaining {
|
|
29
|
+
toHaveStyle(expected: StyleExpectation): void;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export { type StyleExpectation, toHaveStyle };
|
package/build/vitest.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// src/toHaveStyle.ts
|
|
2
|
+
function toHaveStyle(received, expected) {
|
|
3
|
+
if (!isElementLike(received)) {
|
|
4
|
+
return {
|
|
5
|
+
pass: false,
|
|
6
|
+
message: () => `expected an Element, received ${printValue(this, "printReceived", received)}`
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
const expectedStyles = parseExpectedStyles(received, expected);
|
|
10
|
+
const mismatches = [];
|
|
11
|
+
for (const [property, expectedValue] of expectedStyles) {
|
|
12
|
+
const actualValue = getActualStyleValue(received, property);
|
|
13
|
+
if (actualValue !== expectedValue) {
|
|
14
|
+
mismatches.push(`${property}: expected ${expectedValue}, received ${actualValue || "<empty>"}`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
pass: mismatches.length === 0,
|
|
19
|
+
message: () => {
|
|
20
|
+
const expectedLabel = printValue(this, "printExpected", expected);
|
|
21
|
+
return mismatches.length === 0 ? `expected element not to have style ${expectedLabel}` : `expected element to have style ${expectedLabel}
|
|
22
|
+
${mismatches.join("\n")}`;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function printValue(ctx, kind, value) {
|
|
27
|
+
return ctx.utils?.[kind]?.(value) ?? JSON.stringify(value);
|
|
28
|
+
}
|
|
29
|
+
function isElementLike(value) {
|
|
30
|
+
return typeof value === "object" && value !== null && "ownerDocument" in value && Boolean(value.ownerDocument?.defaultView);
|
|
31
|
+
}
|
|
32
|
+
function toKebabCase(property) {
|
|
33
|
+
return property.startsWith("--") ? property : property.replace(/[A-Z]/g, (char) => `-${char.toLowerCase()}`);
|
|
34
|
+
}
|
|
35
|
+
function parseExpectedStyles(el, expected) {
|
|
36
|
+
const probe = el.ownerDocument.createElement("div");
|
|
37
|
+
const styles = /* @__PURE__ */ new Map();
|
|
38
|
+
if (typeof expected === "string") {
|
|
39
|
+
probe.setAttribute("style", expected);
|
|
40
|
+
} else {
|
|
41
|
+
for (const [property, value] of Object.entries(expected)) {
|
|
42
|
+
probe.style.setProperty(toKebabCase(property), String(value));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
for (const property of Array.from(probe.style)) {
|
|
46
|
+
styles.set(property, probe.style.getPropertyValue(property).trim());
|
|
47
|
+
}
|
|
48
|
+
return styles;
|
|
49
|
+
}
|
|
50
|
+
function getActualStyleValue(el, property) {
|
|
51
|
+
if (property.startsWith("--")) {
|
|
52
|
+
return el.style?.getPropertyValue(property).trim() ?? "";
|
|
53
|
+
}
|
|
54
|
+
return el.ownerDocument.defaultView.getComputedStyle(el).getPropertyValue(property).trim();
|
|
55
|
+
}
|
|
56
|
+
export {
|
|
57
|
+
toHaveStyle
|
|
58
|
+
};
|
|
59
|
+
//# sourceMappingURL=vitest.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/toHaveStyle.ts"],"sourcesContent":["/** Supported style expectations for the matcher. */\nexport type StyleExpectation = string | Record<string, string | number>;\n\n/** Minimal subset of Vitest's matcher context used for error formatting. */\ntype MatcherContext = {\n utils?: {\n printExpected?: (value: unknown) => string;\n printReceived?: (value: unknown) => string;\n };\n};\n\n/** Standard matcher result shape returned to Vitest. */\ntype MatcherResult = {\n pass: boolean;\n message: () => string;\n};\n\n/**\n * Assert that an element's computed style matches the provided CSS declarations.\n *\n * Static class-based styles are read via `getComputedStyle`, while CSS custom\n * properties are read directly from the element's inline style because jsdom\n * does not resolve them through computed styles.\n */\nexport function toHaveStyle(this: MatcherContext, received: unknown, expected: StyleExpectation): MatcherResult {\n if (!isElementLike(received)) {\n return {\n pass: false,\n message: () => `expected an Element, received ${printValue(this, \"printReceived\", received)}`,\n };\n }\n\n const expectedStyles = parseExpectedStyles(received, expected);\n const mismatches: string[] = [];\n\n for (const [property, expectedValue] of expectedStyles) {\n const actualValue = getActualStyleValue(received, property);\n if (actualValue !== expectedValue) {\n mismatches.push(`${property}: expected ${expectedValue}, received ${actualValue || \"<empty>\"}`);\n }\n }\n\n return {\n pass: mismatches.length === 0,\n message: () => {\n const expectedLabel = printValue(this, \"printExpected\", expected);\n return mismatches.length === 0\n ? `expected element not to have style ${expectedLabel}`\n : `expected element to have style ${expectedLabel}\\n${mismatches.join(\"\\n\")}`;\n },\n };\n}\n\n/** Format matcher values using Vitest's printers when available. */\nfunction printValue(ctx: MatcherContext, kind: \"printExpected\" | \"printReceived\", value: unknown): string {\n return ctx.utils?.[kind]?.(value) ?? JSON.stringify(value);\n}\n\n/** Narrow an unknown matcher input to a DOM element-like object. */\nfunction isElementLike(value: unknown): value is {\n ownerDocument: Document;\n style?: CSSStyleDeclaration;\n} {\n return (\n typeof value === \"object\" &&\n value !== null &&\n \"ownerDocument\" in value &&\n Boolean((value as { ownerDocument?: Document }).ownerDocument?.defaultView)\n );\n}\n\n/** Convert camelCase property names into CSS kebab-case. */\nfunction toKebabCase(property: string): string {\n return property.startsWith(\"--\") ? property : property.replace(/[A-Z]/g, (char) => `-${char.toLowerCase()}`);\n}\n\n/** Normalize the expected style input into concrete CSS property/value pairs. */\nfunction parseExpectedStyles(el: { ownerDocument: Document }, expected: StyleExpectation): Map<string, string> {\n const probe = el.ownerDocument.createElement(\"div\");\n const styles = new Map<string, string>();\n\n if (typeof expected === \"string\") {\n probe.setAttribute(\"style\", expected);\n } else {\n for (const [property, value] of Object.entries(expected)) {\n probe.style.setProperty(toKebabCase(property), String(value));\n }\n }\n\n for (const property of Array.from(probe.style)) {\n styles.set(property, probe.style.getPropertyValue(property).trim());\n }\n\n return styles;\n}\n\n/** Read the current value for a CSS property from the element under test. */\nfunction getActualStyleValue(el: { style?: CSSStyleDeclaration; ownerDocument: Document }, property: string): string {\n if (property.startsWith(\"--\")) {\n return el.style?.getPropertyValue(property).trim() ?? \"\";\n }\n\n return el.ownerDocument.defaultView!.getComputedStyle(el as Element).getPropertyValue(property).trim();\n}\n"],"mappings":";AAwBO,SAAS,YAAkC,UAAmB,UAA2C;AAC9G,MAAI,CAAC,cAAc,QAAQ,GAAG;AAC5B,WAAO;AAAA,MACL,MAAM;AAAA,MACN,SAAS,MAAM,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Q,GAAG,OAAO,KAAK,CAAC;AAAA,IAC9D;AAAA,EACF;AAEA,aAAW,YAAY,MAAM,KAAK,MAAM,KAAK,GAAG;AAC9C,WAAO,IAAI,UAAU,MAAM,MAAM,iBAAiB,QAAQ,EAAE,KAAK,CAAC;AAAA,EACpE;AAEA,SAAO;AACT;AAGA,SAAS,oBAAoB,IAA8D,UAA0B;AACnH,MAAI,SAAS,WAAW,IAAI,GAAG;AAC7B,WAAO,GAAG,OAAO,iBAAiB,QAAQ,EAAE,KAAK,KAAK;AAAA,EACxD;AAEA,SAAO,GAAG,cAAc,YAAa,iBAAiB,EAAa,EAAE,iBAAiB,QAAQ,EAAE,KAAK;AACvG;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@homebound/truss",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.6.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"bin": "cli.js",
|
|
@@ -19,6 +19,11 @@
|
|
|
19
19
|
"types": "./build/runtime.d.ts",
|
|
20
20
|
"import": "./build/runtime.js",
|
|
21
21
|
"default": "./build/runtime.js"
|
|
22
|
+
},
|
|
23
|
+
"./vitest": {
|
|
24
|
+
"types": "./build/vitest.d.ts",
|
|
25
|
+
"import": "./build/vitest.js",
|
|
26
|
+
"default": "./build/vitest.js"
|
|
22
27
|
}
|
|
23
28
|
},
|
|
24
29
|
"scripts": {
|