From 98a59074d3efb3a058f2e40d77c89b83dd90f691 Mon Sep 17 00:00:00 2001 From: tangjinzhou <415800467@qq.com> Date: Sun, 23 Feb 2020 10:11:00 +0800 Subject: [PATCH] test: update statistic test --- build/config.js | 2 +- .../__tests__/__snapshots__/demo.test.js.snap | 14 +++++++------- components/statistic/__tests__/index.test.js | 11 +++++++++++ components/statistic/demo/countdown.md | 2 +- components/statistic/utils.js | 17 ++++++++++++++--- 5 files changed, 34 insertions(+), 12 deletions(-) diff --git a/build/config.js b/build/config.js index db593c9a1..a7602110d 100644 --- a/build/config.js +++ b/build/config.js @@ -1,5 +1,5 @@ module.exports = { dev: { - componentName: 'skeleton', // dev components + componentName: 'statistic', // dev components }, }; diff --git a/components/statistic/__tests__/__snapshots__/demo.test.js.snap b/components/statistic/__tests__/__snapshots__/demo.test.js.snap index 42acdc5d0..2168fd38f 100644 --- a/components/statistic/__tests__/__snapshots__/demo.test.js.snap +++ b/components/statistic/__tests__/__snapshots__/demo.test.js.snap @@ -16,7 +16,7 @@ exports[`renders ./components/statistic/demo/basic.md correctly 1`] = ` exports[`renders ./components/statistic/demo/card.md correctly 1`] = `
-
+
@@ -26,7 +26,7 @@ exports[`renders ./components/statistic/demo/card.md correctly 1`] = `
-
+
@@ -42,19 +42,19 @@ exports[`renders ./components/statistic/demo/card.md correctly 1`] = ` exports[`renders ./components/statistic/demo/countdown.md correctly 1`] = `
-
+
Countdown
48:00:30
-
+
Million Seconds
48:00:30:000
-
+
Day Level
2 天 0 时 0 分 30 秒
@@ -65,13 +65,13 @@ exports[`renders ./components/statistic/demo/countdown.md correctly 1`] = ` exports[`renders ./components/statistic/demo/unit.md correctly 1`] = `
-
+
Feedback
1,128
-
+
Unmerged
93 / 100
diff --git a/components/statistic/__tests__/index.test.js b/components/statistic/__tests__/index.test.js index a6755b9e2..f01c2bc85 100644 --- a/components/statistic/__tests__/index.test.js +++ b/components/statistic/__tests__/index.test.js @@ -3,8 +3,14 @@ import { asyncExpect } from '@/tests/utils'; import MockDate from 'mockdate'; import moment from 'moment'; import Statistic from '..'; +import { formatTimeStr } from '../utils'; +import { sleep } from '../../../tests/utils'; +import mountTest from '../../../tests/shared/mountTest'; describe('Statistic', () => { + mountTest(Statistic); + mountTest(Statistic.Countdown); + beforeAll(() => { MockDate.set(moment('2018-11-28 00:00:00')); }); @@ -104,4 +110,9 @@ describe('Statistic', () => { // expect(instance.countdownId).toBe(undefined); }); }); + describe('utils', () => { + it('format should support escape', () => { + expect(formatTimeStr(1000 * 60 * 60 * 24, 'D [Day]')).toBe('1 Day'); + }); + }); }); diff --git a/components/statistic/demo/countdown.md b/components/statistic/demo/countdown.md index 51e875774..f646b75f9 100644 --- a/components/statistic/demo/countdown.md +++ b/components/statistic/demo/countdown.md @@ -41,7 +41,7 @@ Countdown component. }, methods: { onFinish() { - console.log('over'); + console.log('finished!'); }, }, }; diff --git a/components/statistic/utils.js b/components/statistic/utils.js index 0a6efb40c..25e6be2d7 100644 --- a/components/statistic/utils.js +++ b/components/statistic/utils.js @@ -14,10 +14,14 @@ const timeUnits = [ ['S', 1], // million seconds ]; -function formatTimeStr(duration, format) { +export function formatTimeStr(duration, format) { let leftDuration = duration; - return timeUnits.reduce((current, [name, unit]) => { + const escapeRegex = /\[[^\]]*\]/g; + const keepList = (format.match(escapeRegex) || []).map(str => str.slice(1, -1)); + const templateText = format.replace(escapeRegex, '[]'); + + const replacedText = timeUnits.reduce((current, [name, unit]) => { if (current.indexOf(name) !== -1) { const value = Math.floor(leftDuration / unit); leftDuration -= value * unit; @@ -27,7 +31,14 @@ function formatTimeStr(duration, format) { }); } return current; - }, format); + }, templateText); + + let index = 0; + return replacedText.replace(escapeRegex, () => { + const match = keepList[index]; + index += 1; + return match; + }); } export function formatCountdown(value, config) {