Created
June 16, 2017 08:37
-
-
Save unlabeled/87874e97fd9c70a0211008a13df92eee to your computer and use it in GitHub Desktop.
Tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
cutFileName, | |
formatDate, | |
formatDateTime, | |
getUTCDate, | |
formatDateTimeForApi, | |
formatDateForApi, | |
formatMonthYear, | |
objectToQuery, | |
queryToObject, | |
stripFakeYear, | |
} from "../" | |
import moment from "moment" | |
const test = window.test | |
const expect = window.expect | |
/** | |
* Test cutFileName | |
*/ | |
test("cutFileName should cut long file names (> 15 symbols)", () => { | |
const longName = "ljhskjdfhgsdfgsdfgsdfg.dfgsdf dfgsdfg - fgsdf.pdf" | |
const longResult = "ljhskjdfhgsd...sdf.pdf" | |
expect(cutFileName(longName)).toBe(longResult) | |
// Object with param title should work the same way | |
expect(cutFileName({ title: longName })).toBe(longResult) | |
}) | |
test("cutFileName should return the same file name for short names (< 15 symbols)", () => { | |
const shortName = "ss.pdf" | |
expect(cutFileName(shortName)).toBe(shortName) | |
}) | |
test("cutFileName should return undefined for objetcs with no title param and if there is no string", () => { | |
expect(cutFileName(null)).toBeUndefined() | |
expect(cutFileName({})).toBeUndefined() | |
}) | |
/** | |
* Test formatDate | |
*/ | |
test("formatDate result should have format DD.MM.YYYY", () => { | |
const date = 1490026267408 | |
expect(formatDate(new Date(date))).toBe("20.03.2017") | |
}) | |
test("formatDate with no arg should return null", () => { | |
expect(formatDate()).toBeNull() | |
}) | |
test("formatDate result should have format DD.MM.YYYY", () => { | |
const date = 1490026267408 | |
expect(formatDate(new Date(date))).toBe("20.03.2017") | |
}) | |
/** | |
* Test for stripFakeYear | |
*/ | |
test("stripFakeYear result should strip .2800 from the date", () => { | |
const date = "20.03.2800" | |
expect(stripFakeYear(date)).toBe("20.03") | |
}) | |
test("stripFakeYear result should do nothing if it is not year 2800", () => { | |
const date = "20.03.2017" | |
expect(stripFakeYear(date)).toBe("20.03.2017") | |
const dateNull = null | |
expect(stripFakeYear(dateNull)).toBeNull() | |
}) | |
/** | |
* Test formatDateTime | |
*/ | |
test("formatDate result should have format DD.MM.YYYY HH:mm", () => { | |
const date = 1490026267408 | |
expect(formatDateTime(new Date(date))).toBe("20.03.2017 19:11") | |
}) | |
test("formatDate with no arg should return null", () => { | |
expect(formatDateTime()).toBeNull() | |
}) | |
/** | |
* Test formatDateTimeForApi | |
*/ | |
test( | |
"formatDateTimeForApi result should have format DD.MM.YYYY HH:mm:ss", | |
() => { | |
const date = 1490026267408 | |
expect(formatDateTimeForApi(new Date(date))).toBe("2017-03-20 19:11:07") | |
} | |
) | |
test("formatDateTimeForApi with no value should be close to our value", () => { | |
const format = "YYYY-MM-DD HH:mm:ss" | |
const date = new Date() | |
const dateForAPI1 = moment(formatDateTimeForApi(), format).valueOf() | |
const dateForAPI2 = moment(formatDateTimeForApi(date), format).valueOf() + | |
date.getTimezoneOffset() * 60 * 1000 | |
const diff = dateForAPI2 - dateForAPI1.valueOf() | |
expect(diff).toBeLessThan(1000) | |
}) | |
test("formatDateTimeForApi should return null if it gets null", () => { | |
expect(formatDateTimeForApi(null)).toBeNull() | |
}) | |
/** | |
* Test getTime | |
*/ | |
test("getTime from getUTCDate should be close to our value", () => { | |
const date = new Date() | |
const milliseconds = date.getTime() + date.getTimezoneOffset() * 60 * 1000 | |
const millisecondsUTC = getUTCDate().getTime() | |
const diff = milliseconds - millisecondsUTC | |
expect(diff).toBeLessThan(1000) | |
}) | |
/** | |
* Test formatDateForApi | |
*/ | |
test("formatDateForApi result should have format YYYY-MM-DD", () => { | |
const date = 1490026267408 | |
expect(formatDateForApi(new Date(date))).toBe("2017-03-20") | |
}) | |
test("formatDateForApi with no args should return null", () => { | |
expect(formatDateForApi()).toBeNull() | |
}) | |
/** | |
* Test objectToQuery | |
*/ | |
test("objectToQuery with no args should return empty string", () => { | |
expect(objectToQuery()).toBe("") | |
}) | |
test("objectToQuery should return string with keys/values", () => { | |
const obj = { a: "2", var: "key" } | |
expect(objectToQuery(obj)).toBe("a=2&var=key") | |
}) | |
/** | |
* Test queryToObject | |
*/ | |
test("queryToObject with no arg should return empty object", () => { | |
expect(queryToObject()).toEqual({}) | |
}) | |
/** | |
* Test queryToObject | |
*/ | |
test("queryToObject should return object with expected keys/values", () => { | |
const query = "var=key&a=2" | |
expect(queryToObject(query)).toEqual({ a: "2", var: "key" }) | |
}) | |
/** | |
* Test formatMonthYear | |
*/ | |
test("formatMonthYear with no args should return null", () => { | |
expect(formatMonthYear()).toBeNull() | |
}) | |
test("formatMonthYear result should return month name and year", () => { | |
const date = 1490026267408 | |
expect(formatMonthYear(new Date(date))).toBe("March 2017") | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment