Skip to content

Commit

Permalink
fix(jest-serializer): fix space handling (#583)
Browse files Browse the repository at this point in the history
  • Loading branch information
layershifter authored Jul 12, 2024
1 parent ec2b486 commit 2694b31
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix: improve trailing space handling",
"packageName": "@griffel/jest-serializer",
"email": "[email protected]",
"dependentChangeType": "patch"
}
61 changes: 53 additions & 8 deletions packages/jest-serializer/src/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,20 @@ const TestComponent: React.FC<{ id?: string }> = ({ id }) => {
'class-b',
);

return <div data-testid={id} className={styles} />;
return <div data-testid={id} className={styles} data-test-attr="true" />;
};

const TestResetComponent: React.FC<{ id?: string }> = ({ id }) => {
const TestResetComponent: React.FC<{ id?: string; children?: React.ReactNode }> = ({ id, children }) => {
const classes = useStyles1();
const baseClassName = useBaseStyles();

const className = mergeClasses('class-a', baseClassName, classes.paddingLeft, 'class-b');
const className = mergeClasses('class-reset-a', baseClassName, classes.paddingLeft, 'class-reset-b');

return <div data-testid={id} className={className} />;
return (
<div data-testid={id} className={className}>
{children}
</div>
);
};

const RtlWrapper: React.FC<{ children: React.ReactNode }> = ({ children }) => (
Expand Down Expand Up @@ -85,22 +89,24 @@ describe('serializer', () => {
expect(render(<TestComponent />).container.firstChild).toMatchInlineSnapshot(`
<div
class="class-a class-b"
data-test-attr="true"
/>
`);
expect(render(<TestResetComponent />).container.firstChild).toMatchInlineSnapshot(`
<div
class="class-a class-b"
class="class-reset-a class-reset-b"
/>
`);

expect(render(<TestComponent />, { wrapper: RtlWrapper }).container.firstChild).toMatchInlineSnapshot(`
<div
class="class-a class-b"
data-test-attr="true"
/>
`);
expect(render(<TestResetComponent />, { wrapper: RtlWrapper }).container.firstChild).toMatchInlineSnapshot(`
<div
class="class-a class-b"
class="class-reset-a class-reset-b"
/>
`);

Expand All @@ -113,10 +119,49 @@ describe('serializer', () => {

it('handles HTML strings', () => {
expect(render(<TestResetComponent />).container.innerHTML).toMatchInlineSnapshot(
`"<div class="class-a class-b"></div>"`,
`"<div class="class-reset-a class-reset-b"></div>"`,
);
expect(render(<TestComponent />).container.innerHTML).toMatchInlineSnapshot(
`"<div class="class-a class-b"></div>"`,
`"<div class="class-a class-b" data-test-attr="true"></div>"`,
);
});

it('handles nested elements', () => {
expect(
render(
<TestResetComponent>
<TestComponent />
</TestResetComponent>,
).container.firstChild,
).toMatchInlineSnapshot(`
<div
class="class-reset-a class-reset-b"
>
<div
class="class-a class-b"
data-test-attr="true"
/>
</div>
`);

expect(
render(
<TestResetComponent>
<TestComponent />
</TestResetComponent>,
).container.innerHTML,
).toMatchInlineSnapshot(
`"<div class="class-reset-a class-reset-b"><div class="class-a class-b" data-test-attr="true"></div></div>"`,
);
});

it('does not assert on non-sequence strings', () => {
expect({
toString: () => 'class="foo"',
}).toMatchInlineSnapshot(`
Object {
"toString": [Function],
}
`);
});
});
20 changes: 17 additions & 3 deletions packages/jest-serializer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CSSClasses } from '@griffel/core';
import { DEBUG_RESET_CLASSES, DEFINITION_LOOKUP_TABLE } from '@griffel/core';
import { DEBUG_RESET_CLASSES, DEFINITION_LOOKUP_TABLE, SEQUENCE_PREFIX } from '@griffel/core';

export function print(val: unknown) {
/**
Expand Down Expand Up @@ -56,15 +56,29 @@ export function print(val: unknown) {
/**
* Trim whitespace from className
*/
return `"${valStrippedClassNames.replace(/(?:class|className)="(.*)"/, (match, p1) =>
return `"${valStrippedClassNames.replace(/(?:class|className)="([^"]+)"/g, (match, p1) =>
match.replace(p1, p1.trim()),
)}"`;
}

function isRegisteredSequence(value: string) {
// Heads up!
// There will be a lot of strings that will be tested here, assert only if it's a sequence-like string
if (value.indexOf(SEQUENCE_PREFIX) === 0) {
return (
Object.prototype.hasOwnProperty.call(DEFINITION_LOOKUP_TABLE, value) ||
Object.prototype.hasOwnProperty.call(DEBUG_RESET_CLASSES, value)
);
}

return false;
}

export function test(val: unknown) {
if (typeof val === 'string') {
return val.split(' ').some(v => DEBUG_RESET_CLASSES[v] || DEFINITION_LOOKUP_TABLE[v]);
return val.split(' ').some(v => isRegisteredSequence(v));
}

return false;
}

Expand Down

0 comments on commit 2694b31

Please sign in to comment.