Skip to content

Commit

Permalink
test: update dom, date test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
hustcc committed Oct 13, 2019
1 parent 4d870a6 commit 0c26b63
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 65 deletions.
23 changes: 23 additions & 0 deletions __tests__/helper.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, ms);
});
}
19 changes: 1 addition & 18 deletions __tests__/realtime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 + '');
Expand Down
19 changes: 9 additions & 10 deletions __tests__/utils/date.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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');
});
});
39 changes: 10 additions & 29 deletions __tests__/utils/dom.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
});
});
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions src/utils/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 0c26b63

Please sign in to comment.