Skip to content

Commit 731fe91

Browse files
Stringify Attributes improvements (#104)
1 parent 94997e1 commit 731fe91

File tree

6 files changed

+38
-32
lines changed

6 files changed

+38
-32
lines changed

package-lock.json

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vue3-snapshot-serializer",
33
"type": "module",
4-
"version": "2.7.1",
4+
"version": "2.8.0",
55
"description": "Vitest snapshot serializer for Vue 3 components",
66
"main": "index.js",
77
"scripts": {
@@ -29,8 +29,8 @@
2929
"eslint-config-tjw-jest": "^3.0.0",
3030
"eslint-config-tjw-jsdoc": "^2.0.1",
3131
"eslint-plugin-jest": "^28.11.0",
32-
"eslint-plugin-jsdoc": "^50.6.3",
33-
"happy-dom": "^17.4.3",
32+
"eslint-plugin-jsdoc": "^50.6.6",
33+
"happy-dom": "^17.4.4",
3434
"vitest": "^3.0.5"
3535
},
3636
"repository": {
@@ -57,6 +57,6 @@
5757
"homepage": "https://github.com/tjw-lint/vue3-snapshot-serializer#readme",
5858
"volta": {
5959
"node": "23.9.0",
60-
"npm": "11.1.0"
60+
"npm": "11.2.0"
6161
}
6262
}

src/helpers.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,15 @@ export const stringify = function (obj) {
173173
let props = Object
174174
.keys(obj)
175175
.map((key) => {
176-
return key + ':' + stringify(obj[key]);
176+
if (obj[key] === undefined) {
177+
return undefined;
178+
}
179+
return key + ': ' + stringify(obj[key]);
177180
})
178-
.join(',');
181+
.filter(Boolean)
182+
.join(', ');
179183

180-
return '{' + props + '}';
184+
return '{ ' + props + ' }';
181185
};
182186

183187
/**

tests/unit/src/__snapshots__/cheerioManipulation.test.js.snap

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ exports[`Cheerio Manipulation > Stringify attributes > Does not stringify attrib
336336

337337
exports[`Cheerio Manipulation > Stringify attributes > Replaces attribute values including child components 1`] = `
338338
<div>
339-
<h1 title="{x:'asdf'}">
339+
<h1 title="{ x: 'asdf' }">
340340
Te
341341
<s>
342342
xt
@@ -348,7 +348,7 @@ exports[`Cheerio Manipulation > Stringify attributes > Replaces attribute values
348348
Text
349349
</strong>
350350
</p>
351-
<h2 title="{y:2}">
351+
<h2 title="{ y: 2 }">
352352
Words
353353
</h2>
354354
<div title="Function">
@@ -359,14 +359,14 @@ exports[`Cheerio Manipulation > Stringify attributes > Replaces attribute values
359359
stuff.
360360
</span>
361361
</div>
362-
<div title="{name:'bob'}">
362+
<div title="{ name: 'bob' }">
363363
bob
364364
</div>
365365
`;
366366

367367
exports[`Cheerio Manipulation > Stringify attributes > Replaces attribute values with stubbed child component 1`] = `
368368
<div>
369-
<h1 title="{x:'asdf'}">
369+
<h1 title="{ x: 'asdf' }">
370370
Te
371371
<s>
372372
xt
@@ -378,7 +378,7 @@ exports[`Cheerio Manipulation > Stringify attributes > Replaces attribute values
378378
Text
379379
</strong>
380380
</p>
381-
<h2 title="{y:2}">
381+
<h2 title="{ y: 2 }">
382382
Words
383383
</h2>
384384
<div title="Function">
@@ -389,12 +389,12 @@ exports[`Cheerio Manipulation > Stringify attributes > Replaces attribute values
389389
stuff.
390390
</span>
391391
</div>
392-
<span person="{name:'bob'}"></span>
392+
<span person="{ name: 'bob' }"></span>
393393
`;
394394

395395
exports[`Cheerio Manipulation > Stringify attributes > Replaces prop values on children in shallow mounts 1`] = `
396396
<div>
397-
<h1 title="{x:'asdf'}">
397+
<h1 title="{ x: 'asdf' }">
398398
Te
399399
<s>
400400
xt
@@ -406,7 +406,7 @@ exports[`Cheerio Manipulation > Stringify attributes > Replaces prop values on c
406406
Text
407407
</strong>
408408
</p>
409-
<h2 title="{y:2}">
409+
<h2 title="{ y: 2 }">
410410
Words
411411
</h2>
412412
<div title="Function">
@@ -417,5 +417,5 @@ exports[`Cheerio Manipulation > Stringify attributes > Replaces prop values on c
417417
stuff.
418418
</span>
419419
</div>
420-
<stringify-props-stub person="{name:'bob'}"></stringify-props-stub>
420+
<stringify-props-stub person="{ name: 'bob' }"></stringify-props-stub>
421421
`;

tests/unit/src/__snapshots__/testingLibrary.test.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ exports[`TestingLibrary > Container > Contents change after toggle button clicke
2020
type="checkbox"
2121
value="'on'"
2222
/>
23-
<h1 title="{available:false}">
23+
<h1 title="{ available: false }">
2424
Unavailable
2525
</h1>
2626
</div>
@@ -39,7 +39,7 @@ exports[`TestingLibrary > Container > Renders default contents 1`] = `
3939
type="checkbox"
4040
value="'on'"
4141
/>
42-
<h1 title="{available:true}">
42+
<h1 title="{ available: true }">
4343
Available
4444
</h1>
4545
</div>
@@ -57,7 +57,7 @@ exports[`TestingLibrary > Wrapper > Contents change after toggle button clicked
5757
type="checkbox"
5858
value="'on'"
5959
/>
60-
<h1 title="{available:false}">
60+
<h1 title="{ available: false }">
6161
Unavailable
6262
</h1>
6363
</div>
@@ -74,7 +74,7 @@ exports[`TestingLibrary > Wrapper > Renders default contents 1`] = `
7474
type="checkbox"
7575
value="'on'"
7676
/>
77-
<h1 title="{available:true}">
77+
<h1 title="{ available: true }">
7878
Available
7979
</h1>
8080
</div>

tests/unit/src/helpers.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,14 @@ describe('Helpers', () => {
168168
test('Object', () => {
169169
const obj = {
170170
subValue: {
171-
key: '2'
171+
a: '2',
172+
b: undefined,
173+
c: 3
172174
}
173175
};
174176

175177
expect(stringify(obj))
176-
.toEqual('{subValue:{key:"2"}}');
178+
.toEqual('{ subValue: { a: "2", c: 3 } }');
177179
});
178180
});
179181

0 commit comments

Comments
 (0)