lion/packages/localize/test/date/formatDate.test.js
Joren Broekema 09d9675963 feat(localize): add types to localize
Co-authored-by: narzac <narzac@gmail.com>
2020-08-03 17:00:26 +02:00

162 lines
4.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { expect } from '@open-wc/testing';
import { localize } from '../../src/localize.js';
import { localizeTearDown } from '../../test-helpers.js';
import { formatDate } from '../../src/date/formatDate.js';
import { parseDate } from '../../src/date/parseDate.js';
describe('formatDate', () => {
beforeEach(() => {
localizeTearDown();
});
it('displays the appropriate date based on locale', async () => {
const testDate = new Date('2012/05/21');
expect(formatDate(testDate)).to.equal('21/05/2012');
localize.locale = 'nl-NL';
expect(formatDate(testDate)).to.equal('21-05-2012');
localize.locale = 'fr-FR';
expect(formatDate(testDate)).to.equal('21/05/2012');
localize.locale = 'de-DE';
expect(formatDate(testDate)).to.equal('21.05.2012');
localize.locale = 'en-US';
expect(formatDate(testDate)).to.equal('05/21/2012');
});
it('displays the date based on options', async () => {
const testDate = new Date('2012/05/21');
const options = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: '2-digit',
};
expect(formatDate(testDate, options)).to.equal('Monday, 21 May 2012');
localize.locale = 'nl-NL';
expect(formatDate(testDate, options)).to.equal('maandag 21 mei 2012');
localize.locale = 'fr-FR';
expect(formatDate(testDate, options)).to.equal('lundi 21 mai 2012');
localize.locale = 'de-DE';
expect(formatDate(testDate, options)).to.equal('Montag, 21. Mai 2012');
localize.locale = 'en-US';
expect(formatDate(testDate, options)).to.equal('Monday, May 21, 2012');
});
it('displays Hungarian dates correctly', async () => {
const options = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: '2-digit',
locale: 'en-US',
};
localize.locale = 'hu-HU';
let date = /** @type {Date} */ (parseDate('2018-5-28'));
expect(formatDate(date)).to.equal('2018. 05. 28.');
date = /** @type {Date} */ (parseDate('1970-11-3'));
expect(formatDate(date, options)).to.equal('Tuesday, November 03, 1970');
});
it('displays Bulgarian dates correctly', async () => {
const options = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: '2-digit',
locale: 'en-US',
};
localize.locale = 'bg-BG';
let date = /** @type {Date} */ (parseDate('29-12-2017'));
expect(formatDate(date)).to.equal('29.12.2017 г.');
date = /** @type {Date} */ (parseDate('13-1-1940'));
expect(formatDate(date)).to.equal('13.01.1940 г.');
date = /** @type {Date} */ (parseDate('3-11-1970'));
expect(formatDate(date, options)).to.equal('Tuesday, November 03, 1970');
});
it('displays US dates correctly', async () => {
const options = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: '2-digit',
locale: 'en-US',
};
localize.locale = 'en-US';
let date = /** @type {Date} */ (parseDate('12-29-1940'));
expect(formatDate(date)).to.equal('12/29/1940');
date = /** @type {Date} */ (parseDate('1-13-1940'));
expect(formatDate(date)).to.equal('01/13/1940');
date = /** @type {Date} */ (parseDate('11-3-1970'));
expect(formatDate(date, options)).to.equal('Tuesday, November 03, 1970');
});
it('handles locales in options', async () => {
let options = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: '2-digit',
locale: 'en-US',
};
let parsedDate = /** @type {Date} */ (parseDate('05.11.2017'));
expect(formatDate(parsedDate, options)).to.equal('Sunday, November 05, 2017');
parsedDate = /** @type {Date} */ (parseDate('01-01-1940'));
options = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: '2-digit',
locale: 'nl-BE',
};
expect(formatDate(parsedDate, options)).to.equal('maandag 01 januari 1940');
});
it('handles options without year', async () => {
const options = {
weekday: 'long',
month: 'long',
day: '2-digit',
};
const parsedDate = /** @type {Date} */ (parseDate('12.10.2019'));
expect(formatDate(parsedDate, options)).to.equal('Saturday, 12 October');
});
it('handles options without month', async () => {
const options = {
weekday: 'long',
year: 'numeric',
day: '2-digit',
};
const parsedDate = /** @type {Date} */ (parseDate('12.10.2019'));
expect(formatDate(parsedDate, options)).to.equal('Saturday 12 2019');
});
it('handles options without day', async () => {
const options = {
weekday: 'long',
year: 'numeric',
month: 'long',
};
const parsedDate = /** @type {Date} */ (parseDate('12.10.2019'));
expect(formatDate(parsedDate, options)).to.equal('October 2019 Saturday');
});
it('returns empty string when input is not a Date object', async () => {
const date = '1-1-2016';
// @ts-ignore tests what happens if you use a wrong type
expect(formatDate(date)).to.equal('');
});
});