Skip to content

Commit 83c7332

Browse files
Checkbox and radios checked (#88)
1 parent fc3eb7e commit 83c7332

File tree

6 files changed

+73
-3
lines changed

6 files changed

+73
-3
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ Related to issue #0
1111
**Checklist:**
1212

1313
* [ ] Update dependencies
14+
* [ ] Run `volta pin node@latest && volta pin npm@latest`
1415
* [ ] Bump
1516
* [ ] If API changed, update `types.js`

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vue3-snapshot-serializer",
33
"type": "module",
4-
"version": "2.3.0",
4+
"version": "2.4.0",
55
"description": "Vitest snapshot serializer for Vue 3 components",
66
"main": "index.js",
77
"scripts": {

src/cheerioManipulation.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ const addInputValues = function ($, vueWrapper) {
9090
const vnode = vueWrapper.find('[' + KEY_NAME + '="' + currentKey + '"]');
9191
const value = vnode.element.value;
9292
element.attribs.value = swapQuotes(stringify(value));
93+
if (['checkbox', 'radio'].includes(element.attribs.type)) {
94+
element.attribs.checked = String(vnode.element.checked);
95+
}
9396
});
9497
}
9598
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<template>
2+
<div>
3+
<input v-model="on" type="checkbox">
4+
<input v-model="off" type="checkbox">
5+
<fieldset>
6+
<input v-model="animal" type="radio" name="animal" value="cat">
7+
<input v-model="animal" type="radio" name="animal" value="cow">
8+
<input v-model="animal" type="radio" name="animal" value="dog">
9+
</fieldset>
10+
</div>
11+
</template>
12+
13+
<script>
14+
export default {
15+
name: 'CheckboxesAndRadios',
16+
data: function () {
17+
return {
18+
on: true,
19+
off: false,
20+
animal: 'cow'
21+
};
22+
}
23+
};
24+
</script>

tests/unit/src/cheerioManipulation.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { mount } from '@vue/test-utils';
33
import { cheerioManipulation } from '@/cheerioManipulation.js';
44

55
import DataVId from '@@/mockComponents/DataVId.vue';
6+
import CheckboxesAndRadios from '@@/mockComponents/CheckboxesAndRadios.vue';
67
import EmbeddedStyles from '@@/mockComponents/EmbeddedStyles.vue';
78
import InlineFunctions from '@@/mockComponents/InlineFunctions.vue';
89
import SeveralInputs from '@@/mockComponents/SeveralInputs.vue';
@@ -233,6 +234,47 @@ describe('Cheerio Manipulation', () => {
233234
expect(wrapper)
234235
.toMatchSnapshot();
235236
});
237+
238+
test('Checkboxes and radios', async () => {
239+
const wrapper = await mount(CheckboxesAndRadios);
240+
globalThis.vueSnapshots.addInputValues = true;
241+
242+
expect(wrapper)
243+
.toMatchInlineSnapshot(`
244+
<div>
245+
<input
246+
checked="true"
247+
type="checkbox"
248+
value="'on'"
249+
/>
250+
<input
251+
checked="false"
252+
type="checkbox"
253+
value="'on'"
254+
/>
255+
<fieldset>
256+
<input
257+
checked="false"
258+
name="animal"
259+
type="radio"
260+
value="'cat'"
261+
/>
262+
<input
263+
checked="true"
264+
name="animal"
265+
type="radio"
266+
value="'cow'"
267+
/>
268+
<input
269+
checked="false"
270+
name="animal"
271+
type="radio"
272+
value="'dog'"
273+
/>
274+
</fieldset>
275+
</div>
276+
`);
277+
});
236278
});
237279

238280
describe('Stringify attributes', () => {

0 commit comments

Comments
 (0)