Skip to content

Commit 14cc964

Browse files
committed
feat: improve quoting mechanism
1 parent e7634da commit 14cc964

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/lib/getExpression.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1-
export function escape(val) {
2-
return val.replace(/'/g, `\\'`);
1+
export function wrapInQuotes(val) {
2+
if (!val.includes(`'`)) {
3+
return `'${val}'`;
4+
}
5+
6+
if (!val.includes('"')) {
7+
return `"${val}"`;
8+
}
9+
10+
if (!val.includes('`')) {
11+
return `\`${val}\``;
12+
}
13+
14+
return `'${val.replace(/'/g, `\\'`)}'`;
315
}
416

517
export function getFieldName(method) {
@@ -10,11 +22,13 @@ export function getExpression({ method, data }) {
1022
const field = getFieldName(method);
1123

1224
if (method === 'getByRole' && data.role && data.name) {
13-
return `screen.getByRole('${data.role}', { name: '${escape(data.name)}' })`;
25+
return `screen.getByRole('${data.role}', { name: ${wrapInQuotes(
26+
data.name,
27+
)} })`;
1428
}
1529

1630
if (data[field]) {
17-
return `screen.${method}('${escape(data[field])}')`;
31+
return `screen.${method}(${wrapInQuotes(data[field])})`;
1832
}
1933

2034
return '';

0 commit comments

Comments
 (0)