From 15949b1c9394e197cc162f2606f6370fd1d33a89 Mon Sep 17 00:00:00 2001 From: Sune Simonsen Date: Fri, 13 Sep 2019 15:39:16 +0200 Subject: [PATCH] Added a round-trip test for formatting and parsing dates in all locales --- scripts/test/locales.test.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 scripts/test/locales.test.js diff --git a/scripts/test/locales.test.js b/scripts/test/locales.test.js new file mode 100644 index 0000000000..6be575b543 --- /dev/null +++ b/scripts/test/locales.test.js @@ -0,0 +1,34 @@ +/* eslint-env mocha */ + +import path from 'path' +import assert from 'power-assert' +import Chance from 'chance' +import format from '../../src/format' +import parse from '../../src/parse' + +import listLocales from '../../scripts/_lib/listLocales' + +const chance = new Chance(42) + +listLocales().forEach(({ name, code }) => { + describe(`${name}`, () => { + chance.n(chance.date, 1).forEach(date => { + it(`parse(format(${date.toISOString()})) = ${date.toISOString()}`, () => { + const locale = require(path.join('..', '..', 'src', 'locale', code)) + const formattedDate = format( + date, + locale.formatLong.date({ width: 'long' }), + { locale } + ) + + const parsedDate = parse(formattedDate, 'PP', date, { locale }) + console.log(formattedDate, parsedDate) + + assert.deepEqual( + parsedDate.toLocaleDateString(), + date.toLocaleDateString() + ) + }) + }) + }) +})