@homebound/truss 2.8.0 → 2.8.2
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 +7 -4
- package/build/vitest.js +37 -17
- package/build/vitest.js.map +1 -1
- package/package.json +1 -1
package/build/vitest.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** Supported style expectations for the matcher. */
|
|
2
|
-
type StyleExpectation =
|
|
2
|
+
type StyleExpectation = Record<string, string | number>;
|
|
3
3
|
/** Minimal subset of Vitest's matcher context used for error formatting. */
|
|
4
4
|
type MatcherContext = {
|
|
5
5
|
utils?: {
|
|
@@ -15,9 +15,12 @@ type MatcherResult = {
|
|
|
15
15
|
/**
|
|
16
16
|
* Assert that an element's computed style matches the provided CSS declarations.
|
|
17
17
|
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
18
|
+
* This matcher is for asserting concrete CSS properties like `color` or
|
|
19
|
+
* `margin-top`, not raw custom properties like `--color`.
|
|
20
|
+
*
|
|
21
|
+
* In jsdom, computed styles sometimes leave values as `var(--token)` instead of
|
|
22
|
+
* resolving them. We follow those references so `expect(el).toHaveStyle({ color:
|
|
23
|
+
* "blue" })` still works for class rules like `.color_var { color: var(--color) }`.
|
|
21
24
|
*/
|
|
22
25
|
declare function toHaveStyle(this: MatcherContext, received: unknown, expected: StyleExpectation): MatcherResult;
|
|
23
26
|
|
package/build/vitest.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
// src/toHaveStyle.ts
|
|
2
|
+
var wholeValueCssVariablePattern = /^var\(\s*(--[\w-]+)\s*(?:,\s*(.+))?\)$/;
|
|
3
|
+
var probe;
|
|
2
4
|
function toHaveStyle(received, expected) {
|
|
3
5
|
if (!isElementLike(received)) {
|
|
4
6
|
return {
|
|
@@ -6,12 +8,14 @@ function toHaveStyle(received, expected) {
|
|
|
6
8
|
message: () => `expected an Element, received ${printValue(this, "printReceived", received)}`
|
|
7
9
|
};
|
|
8
10
|
}
|
|
9
|
-
|
|
11
|
+
probe ??= received.ownerDocument.createElement("div");
|
|
12
|
+
const expectedStyles = parseExpectedStyles(this, received, expected);
|
|
10
13
|
const mismatches = [];
|
|
11
14
|
for (const [property, expectedValue] of expectedStyles) {
|
|
12
|
-
const actualValue = getActualStyleValue(received, property);
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
const actualValue = canonicalizeValue(probe, property, getActualStyleValue(received, property));
|
|
16
|
+
const comparableExpectedValue = canonicalizeValue(probe, property, expectedValue);
|
|
17
|
+
if (actualValue !== comparableExpectedValue) {
|
|
18
|
+
mismatches.push(`${property}: expected ${comparableExpectedValue}, received ${actualValue || "<empty>"}`);
|
|
15
19
|
}
|
|
16
20
|
}
|
|
17
21
|
return {
|
|
@@ -32,26 +36,42 @@ function isElementLike(value) {
|
|
|
32
36
|
function toKebabCase(property) {
|
|
33
37
|
return property.startsWith("--") ? property : property.replace(/[A-Z]/g, (char) => `-${char.toLowerCase()}`);
|
|
34
38
|
}
|
|
35
|
-
function parseExpectedStyles(el, expected) {
|
|
36
|
-
const
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
39
|
+
function parseExpectedStyles(ctx, el, expected) {
|
|
40
|
+
const probe2 = el.ownerDocument.createElement("div");
|
|
41
|
+
for (const [property, value] of Object.entries(expected)) {
|
|
42
|
+
if (property.startsWith("--")) {
|
|
43
|
+
throw new Error(
|
|
44
|
+
`toHaveStyle does not support custom property expectations like ${printValue(ctx, "printExpected", {
|
|
45
|
+
[property]: value
|
|
46
|
+
})}; assert the custom property directly instead.`
|
|
47
|
+
);
|
|
43
48
|
}
|
|
49
|
+
probe2.style.setProperty(toKebabCase(property), String(value));
|
|
44
50
|
}
|
|
45
|
-
|
|
46
|
-
|
|
51
|
+
const styles = /* @__PURE__ */ new Map();
|
|
52
|
+
for (const property of Array.from(probe2.style)) {
|
|
53
|
+
styles.set(property, probe2.style.getPropertyValue(property).trim());
|
|
47
54
|
}
|
|
48
55
|
return styles;
|
|
49
56
|
}
|
|
50
57
|
function getActualStyleValue(el, property) {
|
|
51
|
-
|
|
52
|
-
|
|
58
|
+
const computedStyles = el.ownerDocument.defaultView.getComputedStyle(el);
|
|
59
|
+
const actualValue = computedStyles.getPropertyValue(property).trim();
|
|
60
|
+
return resolveWholeValueCssVariable(actualValue, computedStyles);
|
|
61
|
+
}
|
|
62
|
+
function canonicalizeValue(probe2, property, value) {
|
|
63
|
+
probe2.style.cssText = "";
|
|
64
|
+
probe2.style.setProperty(property, value);
|
|
65
|
+
return probe2.style.getPropertyValue(property).trim() || value;
|
|
66
|
+
}
|
|
67
|
+
function resolveWholeValueCssVariable(value, computedStyles) {
|
|
68
|
+
const match = value.match(wholeValueCssVariablePattern);
|
|
69
|
+
if (!match) {
|
|
70
|
+
return value;
|
|
53
71
|
}
|
|
54
|
-
|
|
72
|
+
const [, variableName, fallbackValue] = match;
|
|
73
|
+
const resolvedValue = computedStyles.getPropertyValue(variableName).trim();
|
|
74
|
+
return resolvedValue || fallbackValue?.trim() || value;
|
|
55
75
|
}
|
|
56
76
|
export {
|
|
57
77
|
toHaveStyle
|
package/build/vitest.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/toHaveStyle.ts"],"sourcesContent":["/** Supported style expectations for the matcher. */\nexport type StyleExpectation =
|
|
1
|
+
{"version":3,"sources":["../src/toHaveStyle.ts"],"sourcesContent":["/** Supported style expectations for the matcher. */\nexport type StyleExpectation = 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 * Match whole-value CSS variable references, i.e. `var(--color)` or\n * `var(--color, red)`.\n */\nconst wholeValueCssVariablePattern = /^var\\(\\s*(--[\\w-]+)\\s*(?:,\\s*(.+))?\\)$/;\n\nlet probe: HTMLDivElement | undefined;\n\n/**\n * Assert that an element's computed style matches the provided CSS declarations.\n *\n * This matcher is for asserting concrete CSS properties like `color` or\n * `margin-top`, not raw custom properties like `--color`.\n *\n * In jsdom, computed styles sometimes leave values as `var(--token)` instead of\n * resolving them. We follow those references so `expect(el).toHaveStyle({ color:\n * \"blue\" })` still works for class rules like `.color_var { color: var(--color) }`.\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 probe ??= received.ownerDocument.createElement(\"div\");\n\n const expectedStyles = parseExpectedStyles(this, received, expected);\n const mismatches: string[] = [];\n for (const [property, expectedValue] of expectedStyles) {\n const actualValue = canonicalizeValue(probe, property, getActualStyleValue(received, property));\n const comparableExpectedValue = canonicalizeValue(probe, property, expectedValue);\n if (actualValue !== comparableExpectedValue) {\n mismatches.push(`${property}: expected ${comparableExpectedValue}, 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} {\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/**\n * Normalize the expected style input into concrete CSS property/value pairs.\n *\n * Examples:\n * - `{ backgroundColor: \"#526675\" }` becomes `background-color: rgb(82, 102, 117)`\n * so it compares cleanly with `getComputedStyle(...)`.\n * - `{ marginTop: 0 }` becomes `margin-top: 0px`/`0`, matching the browser's\n * normalized CSS string output instead of the raw JS input.\n *\n * We do this by round-tripping through a real `CSSStyleDeclaration` on a probe\n * element instead of trying to hand-normalize CSS names and values ourselves.\n */\nfunction parseExpectedStyles(\n ctx: MatcherContext,\n el: { ownerDocument: Document },\n expected: StyleExpectation,\n): Map<string, string> {\n const probe = el.ownerDocument.createElement(\"div\");\n for (const [property, value] of Object.entries(expected)) {\n if (property.startsWith(\"--\")) {\n throw new Error(\n `toHaveStyle does not support custom property expectations like ${printValue(ctx, \"printExpected\", {\n [property]: value,\n })}; assert the custom property directly instead.`,\n );\n }\n probe.style.setProperty(toKebabCase(property), String(value));\n }\n const styles = new Map<string, string>();\n for (const property of Array.from(probe.style)) {\n styles.set(property, probe.style.getPropertyValue(property).trim());\n }\n return styles;\n}\n\n/**\n * Read the current value for a CSS property from the element under test.\n *\n * We intentionally read through `getComputedStyle(...)` because the matcher is\n * asserting the final applied property value, not whether the element happens\n * to have an inline style entry.\n *\n * Examples:\n * - `<div class=\"df\" />` with `.df { display: flex }` should compare as\n * `display: flex`, even though `el.style.display` is empty.\n * - `<div class=\"black\" />` with `.black { color: #353535 }` should compare as\n * `color: rgb(53, 53, 53)`, matching the browser's computed value.\n * - `<div class=\"color_var\" style=\"--color: blue\" />` with\n * `.color_var { color: var(--color) }` should compare as `color: blue`;\n * jsdom often returns `var(--color)` here, so we resolve that indirection\n * immediately after reading the computed value.\n */\nfunction getActualStyleValue(el: { style?: CSSStyleDeclaration; ownerDocument: Document }, property: string): string {\n const computedStyles = el.ownerDocument.defaultView!.getComputedStyle(el as Element);\n const actualValue = computedStyles.getPropertyValue(property).trim();\n return resolveWholeValueCssVariable(actualValue, computedStyles);\n}\n\n/**\n * Canonicalize a property/value pair through CSSOM serialization so equivalent\n * forms like `rgb(...)` and `rgba(..., 1)` compare equal.\n */\nfunction canonicalizeValue(probe: HTMLElement, property: string, value: string): string {\n probe.style.cssText = \"\";\n probe.style.setProperty(property, value);\n return probe.style.getPropertyValue(property).trim() || value;\n}\n\n/**\n * Resolve a whole-value `var(--token)` reference using the element's current\n * custom properties. jsdom leaves these unresolved in many computed values.\n *\n * This is intentionally a single-hop lookup for cases like\n * `.color_var { color: var(--color) }` plus `el.style.setProperty(\"--color\", \"blue\")`.\n */\nfunction resolveWholeValueCssVariable(value: string, computedStyles: CSSStyleDeclaration): string {\n const match = value.match(wholeValueCssVariablePattern);\n if (!match) {\n return value;\n }\n const [, variableName, fallbackValue] = match;\n const resolvedValue = computedStyles.getPropertyValue(variableName).trim();\n return resolvedValue || fallbackValue?.trim() || value;\n}\n"],"mappings":";AAqBA,IAAM,+BAA+B;AAErC,IAAI;AAYG,SAAS,YAAkC,UAAmB,UAA2C;AAC9G,MAAI,CAAC,cAAc,QAAQ,GAAG;AAC5B,WAAO;AAAA,MACL,MAAM;AAAA,MACN,SAAS,MAAM,iCAAiC,WAAW,MAAM,iBAAiB,QAAQ,CAAC;AAAA,IAC7F;AAAA,EACF;AAEA,YAAU,SAAS,cAAc,cAAc,KAAK;AAEpD,QAAM,iBAAiB,oBAAoB,MAAM,UAAU,QAAQ;AACnE,QAAM,aAAuB,CAAC;AAC9B,aAAW,CAAC,UAAU,aAAa,KAAK,gBAAgB;AACtD,UAAM,cAAc,kBAAkB,OAAO,UAAU,oBAAoB,UAAU,QAAQ,CAAC;AAC9F,UAAM,0BAA0B,kBAAkB,OAAO,UAAU,aAAa;AAChF,QAAI,gBAAgB,yBAAyB;AAC3C,iBAAW,KAAK,GAAG,QAAQ,cAAc,uBAAuB,cAAc,eAAe,SAA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