2022-02-12 18:37:16 +08:00
|
|
|
import { ref, nextTick, defineComponent } from 'vue'
|
2020-07-27 18:58:35 +08:00
|
|
|
import { mount } from '@vue/test-utils'
|
2021-12-04 11:20:06 +08:00
|
|
|
import { Loading, Search } from '@element-plus/icons-vue'
|
2020-08-04 11:20:52 +08:00
|
|
|
import Button from '../src/button.vue'
|
|
|
|
import ButtonGroup from '../src/button-group.vue'
|
2022-02-12 18:37:16 +08:00
|
|
|
import type { ComponentSize } from '@element-plus/constants'
|
2020-07-24 17:26:08 +08:00
|
|
|
|
|
|
|
const AXIOM = 'Rem is the best girl'
|
|
|
|
|
2020-07-24 16:44:56 +08:00
|
|
|
describe('Button.vue', () => {
|
2020-07-27 18:58:35 +08:00
|
|
|
it('create', () => {
|
2020-07-27 21:32:08 +08:00
|
|
|
const wrapper = mount(Button, {
|
2020-07-27 18:58:35 +08:00
|
|
|
props: { type: 'primary' },
|
|
|
|
})
|
2020-07-27 21:32:08 +08:00
|
|
|
expect(wrapper.classes()).toContain('el-button--primary')
|
2020-07-27 18:58:35 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('icon', () => {
|
2020-07-27 21:32:08 +08:00
|
|
|
const wrapper = mount(Button, {
|
2021-10-27 23:17:13 +08:00
|
|
|
props: { icon: Search },
|
2020-07-27 18:58:35 +08:00
|
|
|
})
|
2021-10-27 23:17:13 +08:00
|
|
|
expect(wrapper.findComponent(Search).exists()).toBeTruthy()
|
2020-07-27 18:58:35 +08:00
|
|
|
})
|
|
|
|
it('nativeType', () => {
|
2020-07-27 21:32:08 +08:00
|
|
|
const wrapper = mount(Button, {
|
2020-07-27 18:58:35 +08:00
|
|
|
props: { nativeType: 'submit' },
|
|
|
|
})
|
2020-07-27 21:32:08 +08:00
|
|
|
expect(wrapper.attributes('type')).toBe('submit')
|
2020-07-27 18:58:35 +08:00
|
|
|
})
|
|
|
|
it('loading', () => {
|
2020-07-27 21:32:08 +08:00
|
|
|
const wrapper = mount(Button, {
|
2020-07-27 18:58:35 +08:00
|
|
|
props: { loading: true },
|
|
|
|
})
|
2020-07-27 21:32:08 +08:00
|
|
|
expect(wrapper.classes()).toContain('is-loading')
|
2021-10-27 23:17:13 +08:00
|
|
|
expect(wrapper.findComponent(Loading).exists()).toBeTruthy()
|
2020-07-27 18:58:35 +08:00
|
|
|
})
|
|
|
|
it('size', () => {
|
2020-07-27 21:32:08 +08:00
|
|
|
const wrapper = mount(Button, {
|
2021-12-28 19:38:23 +08:00
|
|
|
props: { size: 'large' },
|
2020-07-27 18:58:35 +08:00
|
|
|
})
|
2021-12-28 19:38:23 +08:00
|
|
|
expect(wrapper.classes()).toContain('el-button--large')
|
2020-07-27 18:58:35 +08:00
|
|
|
})
|
|
|
|
it('plain', () => {
|
2020-07-27 21:32:08 +08:00
|
|
|
const wrapper = mount(Button, {
|
2020-07-27 18:58:35 +08:00
|
|
|
props: { plain: true },
|
|
|
|
})
|
2020-07-27 21:32:08 +08:00
|
|
|
expect(wrapper.classes()).toContain('is-plain')
|
2020-07-27 18:58:35 +08:00
|
|
|
})
|
|
|
|
it('round', () => {
|
2020-07-27 21:32:08 +08:00
|
|
|
const wrapper = mount(Button, {
|
2020-07-27 18:58:35 +08:00
|
|
|
props: { round: true },
|
|
|
|
})
|
2020-07-27 21:32:08 +08:00
|
|
|
expect(wrapper.classes()).toContain('is-round')
|
2020-07-27 18:58:35 +08:00
|
|
|
})
|
|
|
|
it('circle', () => {
|
2020-07-27 21:32:08 +08:00
|
|
|
const wrapper = mount(Button, {
|
2020-07-27 18:58:35 +08:00
|
|
|
props: { circle: true },
|
|
|
|
})
|
2020-07-27 21:32:08 +08:00
|
|
|
expect(wrapper.classes()).toContain('is-circle')
|
2020-07-27 18:58:35 +08:00
|
|
|
})
|
|
|
|
|
2020-07-24 17:26:08 +08:00
|
|
|
test('render text', () => {
|
2020-07-27 21:32:08 +08:00
|
|
|
const wrapper = mount(Button, {
|
2020-07-24 17:26:08 +08:00
|
|
|
slots: {
|
2020-07-27 16:17:41 +08:00
|
|
|
default: AXIOM,
|
2020-07-24 17:26:08 +08:00
|
|
|
},
|
|
|
|
})
|
2020-07-27 21:32:08 +08:00
|
|
|
expect(wrapper.text()).toEqual(AXIOM)
|
2020-07-24 17:26:08 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('handle click', async () => {
|
2020-07-27 21:32:08 +08:00
|
|
|
const wrapper = mount(Button, {
|
2020-07-24 17:26:08 +08:00
|
|
|
slots: {
|
2020-07-27 16:17:41 +08:00
|
|
|
default: AXIOM,
|
2020-07-24 17:26:08 +08:00
|
|
|
},
|
|
|
|
})
|
2020-07-27 21:32:08 +08:00
|
|
|
await wrapper.trigger('click')
|
|
|
|
expect(wrapper.emitted()).toBeDefined()
|
2020-07-24 16:44:56 +08:00
|
|
|
})
|
2020-07-27 18:58:35 +08:00
|
|
|
|
|
|
|
test('handle click inside', async () => {
|
2020-07-27 21:32:08 +08:00
|
|
|
const wrapper = mount(Button, {
|
2020-07-27 18:58:35 +08:00
|
|
|
slots: {
|
|
|
|
default: '<span class="inner-slot"></span>',
|
|
|
|
},
|
|
|
|
})
|
2022-02-12 18:37:16 +08:00
|
|
|
wrapper.element.querySelector<HTMLElement>('.inner-slot')!.click()
|
2020-07-27 21:32:08 +08:00
|
|
|
expect(wrapper.emitted()).toBeDefined()
|
2020-07-27 18:58:35 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('loading implies disabled', async () => {
|
2020-07-27 21:32:08 +08:00
|
|
|
const wrapper = mount(Button, {
|
2020-07-27 18:58:35 +08:00
|
|
|
slots: {
|
|
|
|
default: AXIOM,
|
|
|
|
},
|
|
|
|
props: { loading: true },
|
|
|
|
})
|
2020-07-27 21:32:08 +08:00
|
|
|
await wrapper.trigger('click')
|
|
|
|
expect(wrapper.emitted('click')).toBeUndefined()
|
2020-07-27 18:58:35 +08:00
|
|
|
})
|
|
|
|
|
2020-07-27 19:18:09 +08:00
|
|
|
it('disabled', async () => {
|
2020-07-27 21:32:08 +08:00
|
|
|
const wrapper = mount(Button, {
|
2020-07-27 19:18:09 +08:00
|
|
|
props: { disabled: true },
|
|
|
|
})
|
2020-07-27 21:32:08 +08:00
|
|
|
expect(wrapper.classes()).toContain('is-disabled')
|
|
|
|
await wrapper.trigger('click')
|
|
|
|
expect(wrapper.emitted('click')).toBeUndefined()
|
2020-07-27 19:18:09 +08:00
|
|
|
})
|
2022-01-22 10:11:35 +08:00
|
|
|
|
|
|
|
it('loading icon', () => {
|
|
|
|
const wrapper = mount(Button, {
|
|
|
|
props: {
|
|
|
|
loadingIcon: Search,
|
|
|
|
loading: true,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
expect(wrapper.findComponent(Search).exists()).toBeTruthy()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('loading slot', () => {
|
|
|
|
const App = defineComponent({
|
2022-02-12 18:37:16 +08:00
|
|
|
setup: () => () =>
|
|
|
|
(
|
|
|
|
<Button
|
|
|
|
v-slots={{ loading: <span class="custom-loading">111</span> }}
|
|
|
|
loading={true}
|
|
|
|
>
|
|
|
|
Loading
|
|
|
|
</Button>
|
|
|
|
),
|
2022-01-22 10:11:35 +08:00
|
|
|
})
|
|
|
|
const wrapper = mount(App)
|
|
|
|
expect(wrapper.find('.custom-loading').exists()).toBeTruthy()
|
|
|
|
})
|
2020-07-24 16:44:56 +08:00
|
|
|
})
|
2020-08-04 11:20:52 +08:00
|
|
|
describe('Button Group', () => {
|
|
|
|
it('create', () => {
|
2021-08-29 12:33:35 +08:00
|
|
|
const wrapper = mount({
|
2022-02-12 18:37:16 +08:00
|
|
|
setup: () => () =>
|
|
|
|
(
|
|
|
|
<ButtonGroup>
|
|
|
|
<Button type="primary">Prev</Button>
|
|
|
|
<Button type="primary">Next</Button>
|
|
|
|
</ButtonGroup>
|
|
|
|
),
|
2021-08-29 12:33:35 +08:00
|
|
|
})
|
2020-08-04 11:20:52 +08:00
|
|
|
expect(wrapper.classes()).toContain('el-button-group')
|
|
|
|
expect(wrapper.findAll('button').length).toBe(2)
|
|
|
|
})
|
2021-08-29 12:33:35 +08:00
|
|
|
|
|
|
|
it('button group reactive size', async () => {
|
2022-02-12 18:37:16 +08:00
|
|
|
const size = ref<ComponentSize>('small')
|
2021-08-29 12:33:35 +08:00
|
|
|
const wrapper = mount({
|
2022-02-12 18:37:16 +08:00
|
|
|
setup: () => () =>
|
|
|
|
(
|
|
|
|
<ButtonGroup size={size.value}>
|
|
|
|
<Button type="primary">Prev</Button>
|
|
|
|
<Button type="primary">Next</Button>
|
|
|
|
</ButtonGroup>
|
|
|
|
),
|
2021-08-29 12:33:35 +08:00
|
|
|
})
|
|
|
|
expect(wrapper.classes()).toContain('el-button-group')
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(
|
|
|
|
wrapper.findAll('.el-button-group button.el-button--small').length
|
|
|
|
).toBe(2)
|
2021-08-29 12:33:35 +08:00
|
|
|
|
2021-12-28 19:38:23 +08:00
|
|
|
size.value = 'large'
|
2021-08-29 12:33:35 +08:00
|
|
|
await nextTick()
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
expect(
|
2021-12-28 19:38:23 +08:00
|
|
|
wrapper.findAll('.el-button-group button.el-button--large').length
|
2021-09-04 19:29:28 +08:00
|
|
|
).toBe(2)
|
2021-08-29 12:33:35 +08:00
|
|
|
})
|
2021-09-28 20:51:08 +08:00
|
|
|
|
|
|
|
it('button group type', async () => {
|
|
|
|
const wrapper = mount({
|
2022-02-12 18:37:16 +08:00
|
|
|
setup: () => () =>
|
|
|
|
(
|
|
|
|
<ButtonGroup type="warning">
|
|
|
|
<Button type="primary">Prev</Button>
|
|
|
|
<Button>Next</Button>
|
|
|
|
</ButtonGroup>
|
|
|
|
),
|
2021-09-28 20:51:08 +08:00
|
|
|
})
|
|
|
|
expect(wrapper.classes()).toContain('el-button-group')
|
|
|
|
expect(
|
|
|
|
wrapper.findAll('.el-button-group button.el-button--primary').length
|
|
|
|
).toBe(1)
|
|
|
|
expect(
|
|
|
|
wrapper.findAll('.el-button-group button.el-button--warning').length
|
|
|
|
).toBe(1)
|
|
|
|
})
|
2021-10-24 22:47:39 +08:00
|
|
|
|
|
|
|
it('add space in two Chinese characters', async () => {
|
|
|
|
const wrapper = mount(Button, {
|
|
|
|
slots: {
|
|
|
|
default: '中文',
|
|
|
|
},
|
2021-11-10 15:21:20 +08:00
|
|
|
props: {
|
|
|
|
autoInsertSpace: true,
|
|
|
|
},
|
2021-10-24 22:47:39 +08:00
|
|
|
})
|
|
|
|
expect(wrapper.find('.el-button span').text()).toBe('中文')
|
|
|
|
expect(wrapper.find('.el-button span').classes()).toContain(
|
|
|
|
'el-button__text--expand'
|
|
|
|
)
|
|
|
|
})
|
2020-08-04 11:20:52 +08:00
|
|
|
})
|