From 0c26b63bfe0b240be550d896086d6078c21c1435 Mon Sep 17 00:00:00 2001 From: hustcc Date: Sun, 13 Oct 2019 11:24:32 +0800 Subject: [PATCH] test: update dom, date test cases --- __tests__/helper.ts | 23 +++++++++++++++++++++ __tests__/realtime.spec.ts | 19 +----------------- __tests__/utils/date.spec.ts | 19 +++++++++--------- __tests__/utils/dom.spec.ts | 39 +++++++++--------------------------- package.json | 6 ++---- src/utils/date.ts | 8 ++++---- 6 files changed, 49 insertions(+), 65 deletions(-) create mode 100644 __tests__/helper.ts diff --git a/__tests__/helper.ts b/__tests__/helper.ts new file mode 100644 index 0000000..62a5bfc --- /dev/null +++ b/__tests__/helper.ts @@ -0,0 +1,23 @@ +/** + * 创建一个 time 节点 + */ +export function createTimeNode(timestamp?: number): HTMLElement { + const time = document.createElement('time'); + document.body.append(time); + + if (timestamp) time.setAttribute('datetime', `${timestamp}`); + + return time; +} + +/** + * 延迟 ms + * @param ms + */ +export function delay(ms = 1100): Promise { + return new Promise((resolve, reject) => { + setTimeout(() => { + resolve(); + }, ms); + }); +} diff --git a/__tests__/realtime.spec.ts b/__tests__/realtime.spec.ts index a10bf04..fd53c27 100644 --- a/__tests__/realtime.spec.ts +++ b/__tests__/realtime.spec.ts @@ -4,27 +4,10 @@ */ import { render, cancel } from '../src/'; +import { createTimeNode, delay } from './helper'; const now = +new Date(); -/** - * 创建一个 time 节点 - */ -function createTimeNode(): HTMLElement { - const time = document.createElement('time'); - document.body.append(time); - - return time; -} - -function delay(ms = 1100) { - return new Promise((resolve, reject) => { - setTimeout(() => { - resolve(); - }, ms); - }); -} - const time1 = createTimeNode(); const time2 = createTimeNode(); time1.setAttribute('datetime', now - 15000 + ''); diff --git a/__tests__/utils/date.spec.ts b/__tests__/utils/date.spec.ts index e3a0a5e..ece8097 100644 --- a/__tests__/utils/date.spec.ts +++ b/__tests__/utils/date.spec.ts @@ -3,20 +3,19 @@ * Contract: i@hust.cc */ -import { toDate, formatDiff, diffSec, nextInterval } from '../../src/utils/date'; +import { toTimestamp, formatDiff, diffSec, nextInterval } from '../../src/utils/date'; import { getLocale } from '../../src/locales'; describe('date', () => { - // TODO fill all test cases - test('toDate', () => { - expect(typeof toDate('1992-08-01')).toEqual('number'); - expect(typeof toDate(712627200000)).toEqual('number'); + test('toTimestamp', () => { + expect(toTimestamp('1992-08-01')).toBe(712598400000); + expect(toTimestamp(712627200000)).toBe(712627200000); - expect(typeof toDate('2017-2-5 3:57:52UTC')).toEqual('number'); - expect(typeof toDate('2017-2-5T3:57:52Z')).toEqual('number'); + expect(typeof toTimestamp('2017-2-5 3:57:52UTC')).toBe('number'); + expect(typeof toTimestamp('2017-2-5T3:57:52Z')).toBe('number'); - expect(typeof toDate()).toEqual('number'); + expect(typeof toTimestamp()).toBe('number'); }); test('diffSec', () => { @@ -45,7 +44,7 @@ describe('date', () => { expect(formatDiff(-1000, getLocale('en'))).toEqual('in 16 minutes'); expect(formatDiff(-1000, getLocale('en'))).toEqual('in 16 minutes'); - expect(formatDiff(-1000, getLocale('x'))).toEqual('in 16 minutes'); - expect(formatDiff(-1000, getLocale('x'))).toEqual('in 16 minutes'); + expect(formatDiff(-1000, getLocale('not-exist-locale'))).toEqual('in 16 minutes'); + expect(formatDiff(-1000, getLocale('not-exist-locale'))).toEqual('in 16 minutes'); }); }); diff --git a/__tests__/utils/dom.spec.ts b/__tests__/utils/dom.spec.ts index 2a3ba10..2052304 100644 --- a/__tests__/utils/dom.spec.ts +++ b/__tests__/utils/dom.spec.ts @@ -4,41 +4,22 @@ */ import { getTimerId, getDateAttribute, setTimerId } from '../../src/utils/dom'; +import { createTimeNode } from '../helper'; -describe('dom', () => { - test('getTimerId', () => { - const node = { - getAttribute: (name: string): string => '123', - }; - - // @ts-ignore - expect(getTimerId(node)).toEqual(123); +const ms = +new Date(); +const time = createTimeNode(ms); - expect(() => { - // @ts-ignore - getTimerId({}); - }).toThrow(); - }); +describe('dom', () => { + test('timer id', () => { + setTimerId(time, 123); - test('getDateAttribute', () => { - const node1 = { - getAttribute: (name: string): string => { - if (name === 'datetime') return 'datetime-value'; - }, - }; + expect(getTimerId(time)).toEqual(123); // @ts-ignore - expect(getDateAttribute(node1)).toEqual('datetime-value'); + expect(() => getTimerId()).toThrow(); }); - test('setTimerId', () => { - const node1 = { - setAttribute: jest.fn(), - }; - - // @ts-ignore - setTimerId(node1, 1); - - expect(node1.setAttribute).toBeCalledWith('timeago-tid', 1); + test('getDateAttribute', () => { + expect(getDateAttribute(time)).toBe(`${ms}`); }); }); diff --git a/package.json b/package.json index 2dd5a45..16ca90e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "timeago.js", "officialName": "timeago.js", - "version": "4.0.0-beta.3", + "version": "4.0.0", "summary": "timeago.js is a simple library (less than 1kb) to used to format datetime with `*** time ago` statement. eg: '3 hours ago'.", "description": "timeago.js is a simple library (only 1kb) to used to format datetime with `*** time ago` statement. eg: '3 hours ago'. localization supported.", "main": "lib/index.js", @@ -11,14 +11,12 @@ "esm" ], "scripts": { - "clean": "rimraf -rf lib", "lint": "eslint src/**/* __tests__/**/*", "lint-staged": "lint-staged", "size": "size-limit", "test": "jest", - "coveralls": "cat ./coverage/lcov.info | coveralls", - "start": "tsc -w", "ci": "npm run lint && npm run test && npm run size && lint-md .", + "coveralls": "cat ./coverage/lcov.info | coveralls", "build:umd": "rimraf ./dist && rollup -c && cp -rf dist/ gh-pages && npm run size", "build:cjs": "rimraf ./lib && tsc --module commonjs --outDir lib", "build:esm": "rimraf ./esm && tsc --module ESNext --outDir esm", diff --git a/src/utils/date.ts b/src/utils/date.ts index 7029da0..8610073 100644 --- a/src/utils/date.ts +++ b/src/utils/date.ts @@ -8,11 +8,11 @@ import { LocaleFunc, TDate } from '../interface'; const SEC_ARRAY = [60, 60, 24, 7, 365 / 7 / 12, 12]; /** - * format Date / string / timestamp to Date instance. + * format Date / string / timestamp to timestamp * @param input * @returns {*} */ -export function toDate(input?: Date | string | number): number { +export function toTimestamp(input?: Date | string | number): number { if (input instanceof Date) return +input; // @ts-ignore if (!isNaN(input) || /^\d+$/.test(input)) return +new Date(parseInt(input)); @@ -63,8 +63,8 @@ export function formatDiff(diff: number, localeFunc: LocaleFunc): string { * @returns */ export function diffSec(date: TDate, relativeDate): number { - relativeDate = relativeDate ? toDate(relativeDate) : +new Date(); - return (relativeDate - toDate(date)) / 1000; + relativeDate = relativeDate ? toTimestamp(relativeDate) : +new Date(); + return (relativeDate - toTimestamp(date)) / 1000; } /**