Skip to content

Commit

Permalink
Fix/switch unit test (#1729)
Browse files Browse the repository at this point in the history
* test(unit): add switch unit test

* feat(switch): 新增switch逻辑层单元测试

* test(unit): add prop loading unit test for switch

* test(unit): 修正单元描述错误

* test(unit): 恢复example文件
  • Loading branch information
ChrisLee0211 authored Sep 24, 2022
1 parent 1198228 commit 43ddb8c
Show file tree
Hide file tree
Showing 2 changed files with 205 additions and 0 deletions.
140 changes: 140 additions & 0 deletions src/switch/__tests__/base.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { mount } from '@vue/test-utils';
import { nextTick } from 'vue';
import { describe, expect, it } from 'vitest';
import Switch from '@/src/switch/index.ts';

describe('switch', () => {
describe('ui render test', () => {
// test prop disabled
describe(':props.disabled', () => {
it('disabled default value is false', () => {
const wrapper = mount(Switch);
const ele = wrapper.find('.t-switch');
const eleCls = ele.classes();
expect(eleCls.includes('t-is-disabled')).toBe(false);
});
it('disabled={true} works fine', () => {
const wrapper = mount({
render() {
return <Switch disabled />;
},
});
const ele = wrapper.find('.t-switch');
const eleCls = ele.classes();
expect(eleCls.includes('t-is-disabled')).toBe(true);
});
});

// test props label
describe(':props.label', () => {
it('label default value is empty', () => {
const wrapper = mount(Switch);
const contentEle = wrapper.find('.t-switch__content');
const text = contentEle.element.textContent;
expect(!text).toBe(true);
});
it('label={Array<string>} works find', () => {
const label = ['open', 'close'];
const wrapper = mount({
render() {
return <Switch disabled label={label} />;
},
});
const contentEle = wrapper.find('.t-switch__content');
const text = contentEle.element.innerHTML;
expect(text === label[1]).toBe(true);
});
it('label={Array<TNode>} works find', () => {
const TnodeOpen = () => <span id="switch_open">open</span>;
const TnodeClose = () => <span id="switch_close">close</span>;
const label = [TnodeOpen, TnodeClose];
const wrapper = mount({
render() {
return <Switch disabled label={label} />;
},
});
const contentEle = wrapper.find('.t-switch__content');
const text = contentEle.find('#switch_close').element.innerHTML;
expect(text === 'close').toBe(true);
});
it('label={TNode<value>} works find', () => {
const label = (val) => (val ? <span id="switch_open">open</span> : <span id="switch_close">close</span>);
const wrapper = mount({
render() {
return <Switch disabled label={label} />;
},
});
const contentEle = wrapper.find('.t-switch__content');
const text = contentEle.find('#switch_open').element.innerHTML;
expect(text === 'open').toBe(true);
});
});
// test props defaultValue
describe(':props.defaultValue', () => {
it('defaultValue is false', () => {
const wrapper = mount(Switch);
const ele = wrapper.find('.t-switch');
const eleCls = ele.classes();
expect(eleCls.includes('t-is-checked')).toBeFalsy();
});
it('defaultValue={true} works find', () => {
const wrapper = mount({
render() {
return <Switch defaultValue={true} />;
},
});
const ele = wrapper.find('.t-switch');
const eleCls = ele.classes();
expect(eleCls.includes('t-is-checked')).toBe(true);
});
});
// test props loading
describe(':props.loading', () => {
it('loading default value is false', () => {
const wrapper = mount(Switch);
const ele = wrapper.find('.t-switch');
const loadingEle = ele.find('.t-is-loading');
expect(loadingEle.exists() === false).toBe(true);
});
it('loading={true} works find', () => {
const wrapper = mount({
render() {
return <Switch loading={true} />;
},
});
const ele = wrapper.find('.t-switch');
const loadingEle = ele.find('.t-is-loading');
expect(loadingEle.exists() === true).toBe(true);
});
});
// test props size
describe(':props.size', () => {
it('size default value is medium', () => {
const wrapper = mount(Switch);
const ele = wrapper.find('.t-switch');
const eleCls = ele.classes();
expect(eleCls.includes('t-size-m')).toBe(true);
});
it('size={large} works find', () => {
const wrapper = mount({
render() {
return <Switch size={'large'} />;
},
});
const ele = wrapper.find('.t-switch');
const eleCls = ele.classes();
expect(eleCls.includes('t-size-l')).toBe(true);
});
it('size={small} works find', () => {
const wrapper = mount({
render() {
return <Switch size={'small'} />;
},
});
const ele = wrapper.find('.t-switch');
const eleCls = ele.classes();
expect(eleCls.includes('t-size-s')).toBe(true);
});
});
});
});
65 changes: 65 additions & 0 deletions src/switch/__tests__/behavior.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { mount } from '@vue/test-utils';
import { describe, expect, it } from 'vitest';
import Switch from '@/src/switch/index.ts';

describe('switch', () => {
describe('behavior test', () => {
describe('behavior for props.disabled', () => {
it('disabled={true} can forbbid onChange event', async () => {
const defaultValue = false;
const wrapper = mount({
render() {
return <Switch disabled defaultValue={defaultValue} />;
},
});
await wrapper.trigger('click');
const ele = wrapper.find('.t-switch');
const eleCls = ele.classes();
expect(eleCls.includes('t-is-checked')).toBeFalsy();
});
});
describe('behavior for props.customValue', () => {
it('prop customValue works fine with defaultValue', () => {
const defaultValue = 1;
const customValue = [1, 2];
const wrapper = mount({
render() {
return <Switch customValue={customValue} defaultValue={defaultValue} />;
},
});
const ele = wrapper.find('.t-switch');
const eleCls = ele.classes();
expect(eleCls.includes('t-is-checked')).toBe(true);
});
it('prop customValue works fine with onChange event', async () => {
let onChangeEventCallbackValue;
const customValue = ['a', 'b'];
const onChangeFn = (val) => {
onChangeEventCallbackValue = val;
};
const wrapper = mount({
render() {
return <Switch customValue={customValue} defaultValue={'a'} onChange={onChangeFn} />;
},
});
await wrapper.trigger('click');
expect(customValue.includes(onChangeEventCallbackValue)).toBe(true);
});
});
describe('behavior for props.loading', () => {
it('loading={true} can forbbid onChange event', async () => {
let isChangeEventTrigger = false;
const onChangeFn = (val) => {
isChangeEventTrigger = true;
};
const wrapper = mount({
render() {
return <Switch loading={true} onChange={onChangeFn} />;
},
});
await wrapper.trigger('click');
expect(isChangeEventTrigger === false).toBe(true);
});
});
});
});

0 comments on commit 43ddb8c

Please sign in to comment.