File tree Expand file tree Collapse file tree 7 files changed +78
-23
lines changed
src/components/NcCheckboxRadioSwitch
tests/unit/components/NcCheckboxRadioSwitch Expand file tree Collapse file tree 7 files changed +78
-23
lines changed Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ Files: package.json package-lock.json .github/pull_request_template.md
7
7
Copyright: Nextcloud GmbH and Nextcloud contributors
8
8
License: AGPL-3.0-or-later
9
9
10
- Files: styleguide/assets/icons.css styleguide/assets/server.css cypress/snapshots/* tests/unit/functions/usernameToColor/__snapshots__/usernameToColor.spec.js.snap
10
+ Files: styleguide/assets/icons.css cypress/snapshots/* tests/unit/functions/usernameToColor/__snapshots__/usernameToColor.spec.js.snap
11
11
Copyright: Nextcloud GmbH and Nextcloud contributors
12
12
License: AGPL-3.0-or-later
13
13
Original file line number Diff line number Diff line change
1
+ /**
2
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
3
+ * SPDX-License-Identifier: AGPL-3.0-or-later
4
+ */
5
+
6
+ import type { mount } from '@cypress/vue2'
7
+
8
+ // Augment the Cypress namespace to include type definitions for
9
+ // your custom commands
10
+ declare global {
11
+ // eslint-disable-next-line @typescript-eslint/no-namespace
12
+ namespace Cypress {
13
+ interface Chainable {
14
+ mount : typeof mount
15
+ }
16
+ }
17
+ }
18
+
19
+ export { }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * SPDX-FileCopyrightText: Nextcloud GmbH and Nextcloud contributors
3
+ * SPDX-License-Identifier: AGPL-3.0-or-later
4
+ */
5
+
6
+ import NcCheckboxRadioSwitch from '../../src/components/NcCheckboxRadioSwitch/NcCheckboxRadioSwitch.vue'
7
+
8
+ describe ( 'NcCheckboxRadioSwitch' , ( ) => {
9
+ it ( 'Sets attributes correctly' , ( ) => {
10
+ cy . mount ( {
11
+ render : ( h ) => h ( NcCheckboxRadioSwitch , {
12
+ // TODO: With Vue3 move class and style to attrs
13
+ class : 'my-class' ,
14
+ style : 'background: red;' ,
15
+ attrs : {
16
+ 'aria-describedby' : 'unique-id' ,
17
+ 'data-my-attribute' : 'yes' ,
18
+ } ,
19
+ } , [ 'My checkbox' ] ) ,
20
+ } ) . then ( ( { wrapper } ) => {
21
+ // Class and style belong the wrapper
22
+ expect ( wrapper . classes ( 'my-class' ) ) . to . be . true
23
+ // expect(wrapper.attributes('style')).to.equal('background: red;')
24
+ // Custom data attributes too
25
+ expect ( wrapper . attributes ( 'data-my-attribute' ) ) . to . equal ( 'yes' )
26
+ // real HTML attributes are passed to the real checkbox
27
+ expect ( wrapper . attributes ( 'aria-describedby' ) ) . to . be . undefined
28
+ } )
29
+
30
+ cy . findByRole ( 'checkbox' ) . should ( 'have.attr' , 'aria-describedby' , 'unique-id' )
31
+ } )
32
+ } )
Original file line number Diff line number Diff line change 3
3
* SPDX-License-Identifier: AGPL-3.0-or-later
4
4
*/
5
5
6
+ import { mount } from '@cypress/vue2'
6
7
import { addCompareSnapshotCommand } from 'cypress-visual-regression/dist/command'
7
8
8
9
import '@testing-library/cypress/add-commands'
9
10
10
11
addCompareSnapshotCommand ( )
12
+
13
+ // Example use:
14
+ // cy.mount(MyComponent)
15
+ Cypress . Commands . add ( 'mount' , mount )
Original file line number Diff line number Diff line change @@ -10,22 +10,3 @@ import '../../styleguide/assets/icons.css'
10
10
11
11
// cypress commands
12
12
import './commands'
13
- import { mount } from '@cypress/vue2'
14
-
15
- // Augment the Cypress namespace to include type definitions for
16
- // your custom command.
17
- // Alternatively, can be defined in cypress/support/component.d.ts
18
- // with a <reference path="./component" /> at the top of your spec.
19
-
20
- declare global {
21
- // eslint-disable-next-line @typescript-eslint/no-namespace
22
- namespace Cypress {
23
- interface Chainable {
24
- mount : typeof mount
25
- }
26
- }
27
- }
28
-
29
- // Example use:
30
- // cy.mount(MyComponent)
31
- Cypress . Commands . add ( 'mount' , mount )
Original file line number Diff line number Diff line change @@ -258,7 +258,7 @@ export default {
258
258
class="checkbox-radio-switch"
259
259
:style="cssVars"
260
260
:type="isButtonType ? 'button' : null"
261
- v-bind="isButtonType ? $attrs : {} "
261
+ v-bind="isButtonType ? $attrs : dataAttrs "
262
262
v-on="isButtonType ? listeners : null">
263
263
<input v-if="!isButtonType"
264
264
:id="id"
@@ -272,7 +272,7 @@ export default {
272
272
:indeterminate.prop="hasIndeterminate ? indeterminate : null"
273
273
:required="required"
274
274
:name="name"
275
- v-bind="$attrs "
275
+ v-bind="nonDataAttrs "
276
276
v-on="listeners">
277
277
<NcCheckboxContent :id="id"
278
278
class="checkbox-radio-switch__content"
@@ -450,6 +450,18 @@ export default {
450
450
emits: ['update:checked'],
451
451
452
452
computed: {
453
+ dataAttrs() {
454
+ // filter all data attributes
455
+ return Object.fromEntries(Object.entries(this.$attrs)
456
+ .filter(([key]) => key.startsWith('data-')))
457
+ },
458
+
459
+ nonDataAttrs() {
460
+ // filter all non-data attributes
461
+ return Object.fromEntries(Object.entries(this.$attrs)
462
+ .filter(([key]) => !key.startsWith('data-')))
463
+ },
464
+
453
465
isButtonType() {
454
466
return this.type === TYPE_BUTTON
455
467
},
Original file line number Diff line number Diff line change @@ -17,19 +17,25 @@ describe('NcCheckboxRadioSwitch', () => {
17
17
expect ( wrapper . text ( ) ) . toContain ( 'Test' )
18
18
} )
19
19
20
- it ( 'forwards aria-invalid and aria-errormessage to input' , ( ) => {
20
+ it ( 'forwards all but data- attributes to the input' , ( ) => {
21
21
const wrapper = shallowMount ( NcCheckboxRadioSwitch , {
22
22
slots : {
23
23
default : 'Test' ,
24
24
} ,
25
25
attrs : {
26
26
'aria-invalid' : 'true' ,
27
27
'aria-errormessage' : 'id-test' ,
28
+ 'data-test' : 'checkbox-test-data-attr' ,
29
+ title : 'Test title' ,
28
30
} ,
29
31
} )
30
32
31
33
const input = wrapper . find ( 'input' )
32
34
expect ( input . attributes ( 'aria-invalid' ) ) . toBe ( 'true' )
33
35
expect ( input . attributes ( 'aria-errormessage' ) ) . toBe ( 'id-test' )
36
+ expect ( input . attributes ( 'title' ) ) . toBe ( 'Test title' )
37
+ expect ( input . attributes ( 'data-test' ) ) . not . toBe ( 'checkbox-test-data-attr' )
38
+ expect ( wrapper . attributes ( 'data-test' ) ) . toBe ( 'checkbox-test-data-attr' )
39
+ expect ( wrapper . attributes ( 'title' ) ) . not . toBe ( 'Test title' )
34
40
} )
35
41
} )
You can’t perform that action at this time.
0 commit comments