2022-08-23 20:08:53 +02:00
|
|
|
import { parseTimestamp } from "./time";
|
2022-07-21 18:39:33 +02:00
|
|
|
import moment from "moment";
|
|
|
|
|
|
|
|
const TIMESTAMP_INVALID_STRING = "2020-01-02T42:99:23.000Z";
|
|
|
|
const TIMESTAMP_VALID_STRING = "2020-01-02T12:34:56.000Z";
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
test("parseTimestamp() should fail parsing non-string timestamp", () => {
|
2022-07-21 18:39:33 +02:00
|
|
|
// given
|
|
|
|
const timestamp = {};
|
|
|
|
|
|
|
|
// when
|
|
|
|
const parsedTimestamp = parseTimestamp(timestamp);
|
|
|
|
|
|
|
|
// then
|
|
|
|
expect(parsedTimestamp).toEqual(null);
|
|
|
|
});
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
test("parseTimestamp() should fail parsing empty timestamp string", () => {
|
2022-07-21 18:39:33 +02:00
|
|
|
// given
|
|
|
|
const timestamp = "";
|
|
|
|
|
|
|
|
// when
|
|
|
|
const parsedTimestamp = parseTimestamp(timestamp);
|
|
|
|
|
|
|
|
// then
|
|
|
|
expect(parsedTimestamp).toEqual(null);
|
|
|
|
});
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
test("parseTimestamp() should fail parsing invalid timestamp string", () => {
|
2022-07-21 18:39:33 +02:00
|
|
|
// given
|
|
|
|
// noinspection UnnecessaryLocalVariableJS
|
|
|
|
const timestamp = TIMESTAMP_INVALID_STRING;
|
|
|
|
|
|
|
|
// when
|
|
|
|
const parsedTimestamp = parseTimestamp(timestamp);
|
|
|
|
|
|
|
|
// then
|
|
|
|
expect(parsedTimestamp).toEqual(null);
|
|
|
|
});
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
test("parseTimestamp() should succeed parsing valid timestamp string", () => {
|
2022-07-21 18:39:33 +02:00
|
|
|
// given
|
|
|
|
const timestamp = TIMESTAMP_VALID_STRING;
|
|
|
|
|
|
|
|
// when
|
|
|
|
const parsedTimestamp = parseTimestamp(timestamp);
|
|
|
|
|
|
|
|
// then
|
|
|
|
if (parsedTimestamp === null) {
|
2022-08-23 20:08:53 +02:00
|
|
|
fail("timestamp should not be null");
|
2022-07-21 18:39:33 +02:00
|
|
|
}
|
|
|
|
expect(moment.unix(parsedTimestamp).toISOString()).toEqual(timestamp);
|
|
|
|
});
|