Skip to content
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

fix(countdown): fix milisecond problem and add unit test #313

Merged
merged 2 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions src/count-down/__test__/index.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { nextTick } from 'vue';
import { describe, it, expect, vi } from 'vitest';
import { mount } from '@vue/test-utils';
import CountDown from '../count-down.vue';

const sleep = (duration) =>
new Promise((resolve) =>
setTimeout(() => {
resolve();
}, duration),
);

describe('countdown.vue', async () => {
describe('props', async () => {
it('time', async () => {
const wrapper = mount(<CountDown time={2000}></CountDown>);
expect(wrapper.text()).toContain('00:00:02');
await sleep(1000);
expect(wrapper.text()).toContain('00:00:01');
await sleep(500);
expect(wrapper.text()).toContain('00:00:00');
await sleep(500);
await nextTick();
expect(wrapper.text()).toContain('default');
});

it('content', async () => {
const wrapper = mount(<CountDown time={1000} content="ok"></CountDown>);
await sleep(1500);
await nextTick();
expect(wrapper.text()).toContain('ok');

await wrapper.setProps({ content: 'change' });
expect(wrapper.text()).toContain('change');
});

it('format', async () => {
const wrapper = mount(<CountDown time={100000} format="mm:ss"></CountDown>);
expect(wrapper.text()).not.toContain('00:01:40');
expect(wrapper.text()).toContain('01:40');

const wrapper2 = mount(<CountDown time={100000} format="ss"></CountDown>);
expect(wrapper2.text()).not.toContain(':40');
expect(wrapper2.text()).toContain('40');

const wrapper3 = mount(<CountDown time={100000} format="中文"></CountDown>);
expect(wrapper3.find('.t-countdown---split-with-unit').exists()).toBe(false);

const wrapper4 = mount(<CountDown></CountDown>);
expect(wrapper4).toBeTruthy();
});

it('millisecond', async () => {
const wrapper = mount(<CountDown time={100000} millisecond={true}></CountDown>);
expect(wrapper.text()).toContain('00:01:40:000');
});

it('autoStart', async () => {
const wrapper = mount(<CountDown time={100000} autoStart={false}></CountDown>);
await sleep(1000);
expect(wrapper.text()).toContain('00:01:40');
});

it('size', async () => {
const wrapper = mount(<CountDown time={100000}></CountDown>);
expect(wrapper.find('.t-countdown--small').exists()).toBe(true);
});

it('theme', async () => {
const wrapper = mount(<CountDown time={100000}></CountDown>);
expect(wrapper.find('.t-countdown--default').exists()).toBe(true);
});
});

describe('events', async () => {
it('change', async () => {
const onChange = vi.fn();
const wrapper = mount(<CountDown time={2000} onChange={onChange}></CountDown>);
await sleep(1000);
expect(onChange).toHaveBeenCalled();
});

it('finish', async () => {
const onFinish = vi.fn();
const wrapper = mount(<CountDown time={1000} onFinish={onFinish}></CountDown>);
await sleep(1500);
expect(onFinish).toHaveBeenCalled();
});
});

describe('slots', async () => {
it('content', async () => {
const temp = <div>测试</div>;
const wrapper = mount(<CountDown time={1000}></CountDown>, {
slots: {
content: temp,
},
});
await sleep(1500);
expect(wrapper.text()).toContain('测试');
});
});
});
6 changes: 2 additions & 4 deletions src/count-down/count-down.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ export default defineComponent({
...CountDownProps,
},
setup(props) {
const { content, size = 'small', theme = 'default', ...other } = props || {};
//
const { time, showTimes } = useCountDown(other);
const { time, showTimes } = useCountDown(props);
const internalInstance = getCurrentInstance();
const hasChinese = /.*[\u4e00-\u9fa5]+.*$/?.test?.(other?.format);
const hasChinese = /.*[\u4e00-\u9fa5]+.*$/?.test?.(props?.format);
const contentLayout = computed(() => renderTNode(internalInstance, 'content'));
// return
return {
Expand Down
2 changes: 1 addition & 1 deletion src/shared/useCountDown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function useCountDown(props: TdUseCountDownProps): TdUseCountDown {
// state
const fps = ref();
const count = ref(Number(time));
const showTimes = reactive(getShowTimes(getRemainTimes(time), format));
const showTimes = reactive(getShowTimes(getRemainTimes(time), format, !!millisecond));

// raf
const { pause, resume } = useRafFn(
Expand Down
17 changes: 14 additions & 3 deletions src/shared/useCountDown/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,17 @@ export const getRemainTimes = (time?: number): TimeData => {
* @param num
* @returns
*/
export const fillZero = (num: number): string | number => (num >= 10 ? num : `0${num}`);
export const fillZero = (num: number, isMillieconds = false): string | number => {
if (isMillieconds) {
if (num >= 100) {
return num;
}

return num >= 10 ? `0${num}` : `00${num}`;
}

return num >= 10 ? num : `0${num}`;
};

/**
* getMark
Expand All @@ -45,8 +55,9 @@ export const getMark = (format: string, type: string): string => format?.split?.
* @param time
* @returns
*/
export const getShowTimes = (times: TimeData, format: string): TdUseCountDownShowTimes => {
export const getShowTimes = (times: TimeData, format: string, milliseconds = false): TdUseCountDownShowTimes => {
format = (format || 'DD:HH:mm:ss')?.toUpperCase?.();
milliseconds && !format.includes(':SSS') && (format = format.concat(':SSS'));
const showTimes: TdUseCountDownShowTimes = [];
if (format?.indexOf('DD') > -1) {
showTimes?.push({
Expand Down Expand Up @@ -75,7 +86,7 @@ export const getShowTimes = (times: TimeData, format: string): TdUseCountDownSho
if (format?.indexOf('SSS') > -1) {
showTimes?.push({
mark: getMark(format, 'SSS'),
value: fillZero(times?.milliseconds),
value: fillZero(times?.milliseconds, true),
});
}

Expand Down