Skip to content

Commit 532ed48

Browse files
committed
100% coverage
1 parent 74d4637 commit 532ed48

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

packages/onchainkit/plugins/__tests__/babel-prefix-react-classnames.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ describe('babel-prefix-react-classnames', () => {
5050
expect(result).toContain('className: `prefix-foo ${dynamic} prefix-bar`');
5151
});
5252

53+
it('should handle continuous class names in template literals with expressions', () => {
54+
const code =
55+
'<div className={`foo${isLarge ? "-large" : ""}-bar prefix-blah ${active && "active"}`}>Hello</div>';
56+
const result = transform(code);
57+
expect(result).toContain(
58+
'className: `prefix-foo${isLarge ? "-large" : ""}-bar prefix-blah ${active && "active"}`',
59+
);
60+
});
61+
5362
it('should handle identifiers by using the helper function', () => {
5463
const code = '<div className={classes}>Hello</div>';
5564
const result = transform(code);
@@ -76,6 +85,26 @@ describe('babel-prefix-react-classnames', () => {
7685
expect(result).toContain('"prefix-bar"');
7786
});
7887

88+
it('should ensure helper function is only added once', () => {
89+
const code = `
90+
<div>
91+
<div className={styles.container}>First</div>
92+
<span className={otherStyles.text}>Second</span>
93+
<button className={btnStyles.button}>Third</button>
94+
</div>
95+
`;
96+
const result = transform(code);
97+
98+
// Helper function should be defined only once
99+
const helperMatches = result.match(/function __prefixClassNames\(value\)/g);
100+
expect(helperMatches).toHaveLength(1);
101+
102+
// All three elements should use the helper
103+
expect(result).toContain('className: __prefixClassNames(styles.container)');
104+
expect(result).toContain('className: __prefixClassNames(otherStyles.text)');
105+
expect(result).toContain('className: __prefixClassNames(btnStyles.button)');
106+
});
107+
79108
it('should handle cn utility function calls with variables', () => {
80109
const code = '<div className={cn(classes, "bar")}>Hello</div>';
81110
const result = transform(code);

packages/onchainkit/plugins/__tests__/postcss-prefix-classnames.test.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import postcss from 'postcss';
2-
import { describe, it, expect } from 'vitest';
2+
import { describe, it, expect, vi } from 'vitest';
33
import postcssPrefixClassnames from '../postcss-prefix-classnames';
44

55
// Helper function to process CSS through the plugin
@@ -226,4 +226,23 @@ describe('postcss-prefix-classnames', () => {
226226
]).process(input);
227227
expect(result.css).toBe('.prefix-foo { color: red; }');
228228
});
229+
230+
it('should handle unsupported matcher type gracefully', async () => {
231+
// Mock console.warn to verify it's called when an invalid matcher is provided
232+
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
233+
234+
const input = '.foo { color: red; }';
235+
const output = await run(input, {
236+
prefix: 'prefix-',
237+
// @ts-expect-error - Intentionally passing invalid type to test edge case
238+
includeFiles: 42, // number is not a valid matcher type
239+
});
240+
241+
// Since the matcher is invalid, it should return false in isMatch
242+
// which means no files will be included, so no prefixing should happen
243+
expect(output).toBe('.foo { color: red; }');
244+
245+
// Restore the spy
246+
warnSpy.mockRestore();
247+
});
229248
});

0 commit comments

Comments
 (0)