Skip to content
Open
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
9 changes: 7 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"presets": [
["@babel/env", {"modules": false}]
],
}
],
"env": {
"test": {
"plugins": ["@babel/plugin-transform-modules-commonjs"]
}
}
}
48 changes: 35 additions & 13 deletions __tests__/SubDate.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@

// To be able to run coverage, we have to require CommonJS, which is the compiled code
// const SubDate = require('..').SubDate;

// For --watch to work, we have to include the ESM version of the code
const esm = require('esm')(module);
const SubDate = esm('../src/SubDate.js').default;
import SubDate from '../src/SubDate';

describe('SubDate', () => {
let date = '2019-12-01 00:00:00';
Expand All @@ -15,19 +9,19 @@ describe('SubDate', () => {
expect(SubDate).toBeTruthy();
expect(sd).toEqual(nsd);
});

it('should be instance of SubDate', () => {
expect(sd instanceof SubDate).toBeTruthy();
expect(sd.constructor).toBe(SubDate);

expect(nsd instanceof SubDate).toBeTruthy();
expect(nsd.constructor).toBe(SubDate);
});

it('should be instance of Date', () => {
expect(sd instanceof Date).toBeTruthy();
expect(sd.constructor).toBe(SubDate);

expect(sd instanceof Date).toBeTruthy();
expect(sd.constructor).toBe(SubDate);
});
Expand All @@ -39,7 +33,7 @@ describe('SubDate', () => {
describe('.addYear(year)', function () {
it('should add years', () => {
let year = sd.getYear();

sd.addYear(1);
expect(sd.getYear()).toBe(year + 1);

Expand Down Expand Up @@ -136,11 +130,27 @@ describe('SubDate', () => {
});
});

describe('.addSeconds(seconds)', function () {
it('should add seconds', () => {
sd.addSeconds(1);
expect(sd.getSeconds()).toBe(1);

sd.addSeconds(-2);
expect(sd.getSeconds()).toBe(59);

sd.addSeconds(11);
expect(sd.getSeconds()).toBe(10);

sd.addSeconds(-6);
expect(sd.getSeconds()).toBe(4);
});
});

describe('.to_str(pattern)', function () {
it('should return date by pattern', () => {
date = '2019-12-11 14:28:15';
sd = SubDate(date);

expect(sd.to_str('a/A')).toBe('pm/PM');
expect(sd.to_str('H-h--G-g')).toBe('14-02--14-2');
expect(sd.to_str('y-m-d')).toBe('19-12-11');
Expand All @@ -165,6 +175,18 @@ describe('SubDate', () => {
expect(sd.to_str('Y-m-d H:i:s')).toBe('2001-03-10 17:16:18');

});

it('should escape some characters', () => {
const date = '2021-07-11 13:01:44';
const sd = SubDate(date);

expect(sd.to_str('H\\h:i\\m:s\\s')).toBe('13h:01m:44s');
expect(sd.to_str('\\A: ga, \\i: i [s]')).toBe('A: 1pm, i: 01 [44]');
expect(sd.to_str('\\Y: Y, \\m: m, \\d: d')).toBe('Y: 2021, m: 07, d: 11');
expect(sd.to_str('Curre\\nt t\\i\\me: \\\\ha:i\\m:s\\s\\\\')).toBe('Current time: \\01pm:01m:44s\\');
expect(sd.to_str('To\\d\\a\\y \\i\\s: [m-d-y]')).toBe('Today is: [07-11-21]');
expect(sd.to_str('\\Дата d (час g-ый)\\')).toBe('\\Дата 11 (час 1-ый)\\');
});
});

});
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ module.exports = {
"**/__tests__/**/*.?(m)js",
"**/?(*.)(spec|test).?(m)js",
],
"transform": {
"^.+\\.js$": "babel-jest"
},
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
"browserslist": "> 0.25%, not dead",
"devDependencies": {
"@babel/core": "^7.7.4",
"@babel/plugin-transform-modules-commonjs": "^7.14.5",
"@babel/preset-env": "^7.7.4",
"esm": "^3.2.25",
"babel-jest": "^27.0.6",
"rollup": "^1.27.7",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-terser": "^5.1.2"
Expand Down