浏览代码

add button, buttongroup spec

杨磊 7 年之前
父节点
当前提交
8f5a5b7c8a

+ 27 - 58
src/packages/button/__test__/button.spec.js

@@ -3,103 +3,72 @@ import Button from '../button.vue';
 import Vue from 'vue';
 
 describe('Button.vue', () => {
+    let wrapper = shallowMount(Button, {
+        slots: {
+            default: '去结算'
+        }
+    });
     it('设置type', () => {
-        let wrapper = shallowMount(Button, {
-            slots: {
-                default: '去结算'
-            },
-            propsData:{
-                type: 'light'
-            }
-        });
+        wrapper.setProps({
+            type: 'light'
+        })
         return Vue.nextTick().then(function () {
             expect(wrapper.attributes('class')).toContain('light');
         })
     });
     it('设置slot', () => {
-        let wrapper = shallowMount(Button, {
-            slots: {
-                default: '去结算'
-            }
-        });
         return Vue.nextTick().then(function () {
             expect(wrapper.text()).toBe('去结算');
         })
     });
     it('设置disabled', () => {
-        let wrapper = shallowMount(Button, {
-            slots: {
-                default: '去结算'
-            },
-            propsData:{
-                disabled: true
-            }
-        });
+        wrapper.setProps({
+            disabled: true
+        })
         return Vue.nextTick().then(function () {
             expect(wrapper.text()).toBe('去结算');
             expect(wrapper.attributes('disabled')).toContain('disabled');
         })
     });
     it('设置shape', () => {
-        let wrapper = shallowMount(Button, {
-            slots: {
-                default: '去结算'
-            },
-            propsData:{
-                shape: 'circle'
-            }
-        });
+        wrapper.setProps({
+            shape: 'circle'
+        })
         return Vue.nextTick().then(function () {
             expect(wrapper.text()).toBe('去结算');
             expect(wrapper.attributes('class')).toContain('circle');
         })
     });
     it('设置small', () => {
-        let wrapper = shallowMount(Button, {
-            slots: {
-                default: '去结算'
-            },
-            propsData:{
-                small: true
-            }
-        });
+        wrapper.setProps({
+            small: true
+        })
         return Vue.nextTick().then(function () {
             expect(wrapper.text()).toBe('去结算');
             expect(wrapper.attributes('class')).toContain('small');
         })
     });
     it('设置icon', () => {
-        let wrapper = shallowMount(Button, {
-            slots: {
-                default: '全部100万'
-            },
-            propsData:{
-                type: 'actived',
-                icon: 'tick'
-            }
-        });
+        wrapper.setProps({
+            type: 'actived',
+            icon: 'tick'
+        })
         return Vue.nextTick().then(function () {
-            expect(wrapper.text()).toBe('全部100万');
+            expect(wrapper.text()).toBe('去结算');
             expect(wrapper.contains('.txt-icon')).toBe(true);
             expect(wrapper.find('.txt-icon').attributes('type')).toBe('tick');            
-            expect(wrapper.find('span').text()).toBe('全部100万');
+            expect(wrapper.find('span').text()).toBe('去结算');
         })
     });
     it('设置color', () => {
-        let wrapper = shallowMount(Button, {
-            slots: {
-                default: '去结算'
-            },
-            propsData:{
-                icon: 'tick',
-                color: '#fff'
-            }
-        });
+        wrapper.setProps({
+            icon: 'tick',
+            color: '#fff'
+        })
         return Vue.nextTick().then(function () {
             expect(wrapper.text()).toBe('去结算');
             expect(wrapper.find('span').attributes('style')).toBe('color: rgb(255, 255, 255);');
             expect(wrapper.find('.txt-icon').attributes('color')).toBe('#fff');
         })
     });
-
 });

+ 15 - 22
src/packages/button/button.vue

@@ -1,5 +1,5 @@
 <template>
-    <button :class="btnCls" :disabled="disabled" @click="clickHandler">
+    <button :class="clsStyle" :disabled="disabled" @click="clickHandler">
         <nut-icon class="txt-icon" v-if="icon != ''" :type="icon" :color="this.color"></nut-icon>
         <span :style="{color:this.color}"><slot></slot></span>
     </button>
@@ -41,31 +41,24 @@ export default {
     components: {
         'nut-icon': Icon
     },
-    data() {
-        return {
-            btnCls: ''
-        };
-    },
-    mounted() {
-        this.initStyle();
-        this.initIcon();
-    },
-    methods: {
-        initStyle() {
-            this.btnCls = `nut-button ${this.type} ${this.shape}`;
-            this.btnCls += this.small ? ' small': '';
-            this.btnCls += this.block ? ' block': '';
-            this.btnCls += this.label ? ' label': '';
-        },
-        initIcon() {
+    computed: {
+        clsStyle() {
+            let cls = 'nut-button ';
+            cls += `${this.type} ${this.shape}`;
+            cls += this.small ? ' small': '';
+            cls += this.block ? ' block': '';
+            cls += this.label ? ' label': '';
             if(!this.$slots.default) {
                 if(this.small) {
-                    this.btnCls += ' no-txt-small';
+                    cls += ' no-txt-small';
                 }else {
-                    this.btnCls += ' no-txt';
+                    cls += ' no-txt';
                 }
-            }            
-        },
+            }
+            return cls;
+        }
+    },
+    methods: {
         clickHandler() {
             this.$emit('click');
         }

+ 2 - 2
src/packages/button/demo.vue

@@ -10,10 +10,10 @@
       </div>
 
       <h4>常规按钮组</h4>
-      <nut-button-group>
+      <nut-buttongroup>
         <nut-button type="light">重置</nut-button>
         <nut-button>确定</nut-button>
-      </nut-button-group>
+      </nut-buttongroup>
 
       <h4>通栏按钮</h4>
       <div>

+ 32 - 0
src/packages/buttongroup/__test__/buttongroup.spec.js

@@ -0,0 +1,32 @@
+import { shallowMount, mount } from '@vue/test-utils'
+import ButtonGroup from '../buttongroup.vue';
+import Vue from 'vue';
+
+describe('ButtonGroup.vue', () => {
+    it('设置shape', () => {
+        let wrapper = shallowMount(ButtonGroup, {
+            slots: {
+                default: '<div>测试</div>'
+            },
+            propsData:{
+                shape: 'circle'
+            }
+        });
+        return Vue.nextTick().then(function () {
+            expect(wrapper.attributes('class')).toContain('circle');
+        })
+    });
+    it('设置type', () => {
+        let wrapper = shallowMount(ButtonGroup, {
+            slots: {
+                default: `<div>测试</div>`
+            },
+            propsData:{
+                type: 'menu'
+            }
+        });
+        return Vue.nextTick().then(function () {
+            expect(wrapper.attributes('class')).toContain('menu');
+        })
+    });
+});