feat: adding testing for the function

This commit is contained in:
Carlos Gutierrez 2021-11-22 12:36:40 -06:00
parent 5701c5d878
commit 56f287d54e
5 changed files with 9544 additions and 5 deletions

View File

@ -9,6 +9,45 @@ const addCero = (num) => {
return num < 10 ? `0${num}` : num
}
const gmtChecker = (gmt) => {
const array = [
'GMT+00:00',
'GMT-00:00',
'GMT+01:00',
'GMT+02:00',
'GMT+02:00',
'GMT+03:00',
'GMT+03:30',
'GMT+04:00',
'GMT+05:00',
'GMT+05:30',
'GMT+06:00',
'GMT+07:00',
'GMT+08:00',
'GMT+09:00',
'GMT+09:30',
'GMT+10:00',
'GMT+11:00',
'GMT+12:00',
'GMT-11:00',
'GMT-10:00',
'GMT-09:00',
'GMT-08:00',
'GMT-07:00',
'GMT-07:00',
'GMT-06:00',
'GMT-05:00',
'GMT-05:00',
'GMT-04:00',
'GMT-03:30',
'GMT-03:00',
'GMT-03:00',
'GMT-01:00'
]
if (gmt === undefined) return true
return array.includes(gmt)
}
/**
* Retrieve the date as format yyyy/mm/dd hh:mm:ss
* @param {timestamp} date
@ -21,8 +60,10 @@ module.exports = (dateGetThem, gmtHours) => {
const valid = (new Date(dateGetThem)).getTime() > 0;
if (!valid) return -1
}
gmtHours = !gmtHours ? 'GMT-00:00' : gmtHours
if (!gmtChecker(gmtHours)) return -1
gmtHours = !gmtHours ? 'GMT+00:00' : gmtHours
const gmt = new GMT(gmtHours)
let currentDate = (!dateGetThem) ? new Date() : new Date(dateGetThem)
currentDate = gmt.relativeDate(currentDate)
@ -36,3 +77,4 @@ module.exports = (dateGetThem, gmtHours) => {
const yearMonthDay = `${year}/${addCero(month + 1)}/${addCero(date)} ${addCero(hour)}:${addCero(minutes)}:${addCero(seconds)}`
return yearMonthDay
}

3385
node_modules/.package-lock.json generated vendored

File diff suppressed because it is too large Load Diff

6065
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -4,7 +4,7 @@
"description": "convert the timestamp in a format dd/mm/yyyy hh:mm:ss",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest"
},
"keywords": [
"time",
@ -23,7 +23,8 @@
"devDependencies": {
"@commitlint/cli": "^15.0.0",
"@commitlint/config-conventional": "^15.0.0",
"husky": "^4.3.8"
"husky": "^4.3.8",
"jest": "^27.3.1"
},
"dependencies": {
"node-gmt": "^0.0.2"

48
test/checkerGmt.test.js Normal file
View File

@ -0,0 +1,48 @@
const gmthours = require('../index')
describe('hours format', () => {
it ('get current hours format', () => {
const result = gmthours()
expect(result).not.toBeNull()
})
it ('get hours by timestamp', () => {
const dateNow = new Date()
expect(gmthours(dateNow)).not.toBeNull()
})
it ('get hours by timestamp specifically', () => {
const dateNow = new Date(1995, 11, 17)
expect(gmthours(dateNow)).toBe('1995/12/17 06:00:00')
})
it ('get hours by time and gmt specifically', () => {
const dateNow = new Date(1995, 11, 17)
const gmt = 'GMT-05:00'
expect(gmthours(dateNow, gmt)).toBe('1995/12/17 01:00:00')
})
})
describe('check bad request', () => {
it ('string is not allowed', () => {
const stringWord = 'string word'
expect(gmthours(stringWord)).toBe(-1)
})
it ('number is not allowed', () => {
const numberWord = 10
expect(gmthours(numberWord)).toBe(-1)
})
it ('gmt allowed in GMT format', () => {
const dateNow = new Date(1995, 11, 17)
const gmt = 'esto es un string'
expect(gmthours(dateNow, gmt)).toBe(-1)
})
it ('gmt allowed in GMT format', () => {
const dateNow = new Date(1995, 11, 17)
const gmt = 10
expect(gmthours(dateNow, gmt)).toBe(-1)
})
})