From f16c64d01e0c9ac891bded2beccff3be73cc567c Mon Sep 17 00:00:00 2001 From: "Rong Sen Ng (motss)" Date: Sat, 18 Sep 2021 20:23:57 +0800 Subject: [PATCH] test: add tests for toDateString --- src/helpers/to-date-string.ts | 9 ++------- src/tests/helpers/to-date-string.test.ts | 13 +++++++++++++ 2 files changed, 15 insertions(+), 7 deletions(-) create mode 100644 src/tests/helpers/to-date-string.test.ts diff --git a/src/helpers/to-date-string.ts b/src/helpers/to-date-string.ts index 73259d2a..dd55df37 100644 --- a/src/helpers/to-date-string.ts +++ b/src/helpers/to-date-string.ts @@ -1,8 +1,3 @@ export function toDateString(date: Date): string { - try { - const dateString = date.toJSON(); - return dateString == null ? '' : dateString.replace(/^(.+)T.+/i, '$1'); - } catch { - return ''; - } -} \ No newline at end of file + return date.toISOString().replace(/^(.+)T.+/i, '$1'); +} diff --git a/src/tests/helpers/to-date-string.test.ts b/src/tests/helpers/to-date-string.test.ts new file mode 100644 index 00000000..fb486344 --- /dev/null +++ b/src/tests/helpers/to-date-string.test.ts @@ -0,0 +1,13 @@ +import { expect } from '@open-wc/testing'; + +import { toDateString } from '../../helpers/to-date-string'; + +describe(toDateString.name, () => { + it('returns date string', () => { + const testDateStr = '2020-02-02'; + + const result = toDateString(new Date(testDateStr)); + + expect(result).equal(testDateStr); + }); +});