-
Notifications
You must be signed in to change notification settings - Fork 7
Prevent unintentional form submission in button components #1087
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| <template> | ||
| <button | ||
| ref="componentRef" | ||
| type="button" | ||
|
Comment on lines
3
to
+4
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| class="flat-button__container" | ||
| :class="computedStyle" | ||
| @click="clickHandler" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -81,6 +81,7 @@ | |
| </div> | ||
|
|
||
| <button | ||
| type="button" | ||
| class="mobile-navigation__sidebar-logout" | ||
| @click="emit('logout')" | ||
| > | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { describe, test, expect } from 'vitest'; | ||
| import SegmentedControl from '../components/SegmentedControl.vue'; | ||
| import { mount } from '@vue/test-utils'; | ||
|
|
||
| describe('SegmentedControl', () => { | ||
| test('renders correctly', async () => { | ||
| const wrapper = mount(SegmentedControl, { | ||
| props: { | ||
| segments: ['Segment 1', 'Segment 2'], | ||
| }, | ||
| }); | ||
|
|
||
| expect(wrapper.html()).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| test('should have type="button" in all segment buttons to prevent form submission', () => { | ||
| const wrapper = mount(SegmentedControl, { | ||
| props: { | ||
| segments: ['Segment 1', 'Segment 2'], | ||
| }, | ||
| }); | ||
|
|
||
| const buttons = wrapper.findAll('button'); | ||
| buttons.forEach((button) => { | ||
| expect(button.attributes('type')).toBe('button'); | ||
| }); | ||
| }); | ||
|
|
||
| test('should NOT trigger form submit when a segment is clicked', async () => { | ||
| const TestComponent = { | ||
| components: { SegmentedControl }, | ||
| template: ` | ||
| <form @submit.prevent="handleSubmit"> | ||
| <SegmentedControl :segments="['S1', 'S2']" /> | ||
| </form> | ||
| `, | ||
| data() { | ||
| return { submitted: false }; | ||
| }, | ||
| methods: { | ||
| handleSubmit() { | ||
| this.submitted = true; | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const wrapper = mount(TestComponent); | ||
| const button = wrapper.find('button'); | ||
|
|
||
| await button.trigger('click'); | ||
|
|
||
| expect(wrapper.vm.submitted).toBe(false); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
|
||
| exports[`IconButton > renders correctly 1`] = `"<span data-v-f6045c74="" id="CdsIcon-button"><span data-v-f6045c74=""><button data-v-f6045c74="" class="CdsIcon-button__container CdsIcon-button--lg CdsIcon-button__container--white"><cds-icon-stub data-v-f6045c74="" name="trash-outline" width="24" height="24" color="currentColor" dark="false" light="false" class="CdsIcon-button__icon"></cds-icon-stub></button></span></span>"`; | ||
| exports[`IconButton > renders correctly 1`] = `"<span data-v-f6045c74="" id="CdsIcon-button"><span data-v-f6045c74=""><button data-v-f6045c74="" type="button" class="CdsIcon-button__container CdsIcon-button--lg CdsIcon-button__container--white"><cds-icon-stub data-v-f6045c74="" name="trash-outline" width="24" height="24" color="currentColor" dark="false" light="false" class="CdsIcon-button__icon"></cds-icon-stub></button></span></span>"`; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
|
||
| exports[`SegmentedControl > renders correctly 1`] = `"<div class="segment-control"><button type="button" class="segment-control__button segment-control__button--inactive"><span>Segment 1</span></button><button type="button" class="segment-control__button segment-control__button--inactive"><span>Segment 2</span></button></div>"`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typeaceita qualquer string, mas o atributo nativotypede<button>só suporta'button','submit'e'reset'. Sem um validador, valores inválidos como'text'ou'foo'seriam passados silenciosamente para o HTML sem qualquer aviso no console durante o desenvolvimento.