UNPKG

intl-js-sdk-dev

Version:

This is I18n level 2 common library for javascript based clients like g11n-angular-client,g11n-js-client

123 lines (122 loc) 5.46 kB
"use strict"; /* * Copyright 2019 VMware, Inc. * SPDX-License-Identifier: EPL-2.0 */ Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("../index"); const locale_de_1 = require("./locale_de"); const locale_en_1 = require("../src/data/locale_en"); describe('date formatter', () => { index_1.I18n.registerLocaleData('en', locale_en_1.default.categories); index_1.I18n.registerLocaleData('de', locale_de_1.default.categories); const dt_options = { pattern: 'full' }; const date = new Date(2019, 2, 22, 9, 3, 1, 550); //const formatter = new I18n.DateTimeFormat('en', dt_options); const formatter = index_1.I18n.DateTimeFormat.getInstance('en', dt_options); describe('support input type', () => { it('should support date', () => { expect(formatter.getStandardTime(date)).toEqual(date); }); it('should support time step', () => { expect(formatter.getStandardTime(date.getTime())) .toEqual(date); }); it('should support numeric strings', () => { expect(formatter.getStandardTime(date.getTime().toString())) .toEqual(date); }); it('should support ISO string', () => { const date1 = new Date(2019, 2, 22, 17, 3, 1, 550); expect(formatter.getStandardTime(date1.toISOString())) .toEqual(date1); }); it('should return origin string if string invalid', () => { expect(formatter.getStandardTime('')).toEqual(''); expect(formatter.getStandardTime(null)).toEqual(null); }); }); describe('format date according pattern', () => { it('should return correct formatted date in english', () => { const dateFixtures = { short: /3\/22\/19, 9:03 AM/, long: /March 22, 2019 at 9:03:01 AM GMT(\+|-)\d/, full: /Friday, March 22, 2019 at 9:03:01 AM GMT(\+|-)\d{2}:\d{2}/, medium: /Mar 22, 2019, 9:03:01 AM/, shortTime: /9:03 AM/, mediumTime: /9:03:01 AM/, longTime: /9:03:01 AM GMT(\+|-)\d/, fullTime: /9:03:01 AM GMT(\+|-)\d{2}:\d{2}/, shortDate: /3\/22\/19/, longDate: /March 22, 2019/, mediumDate: /Mar 22, 2019/, fullDate: /Friday, March 22, 2019/ }; Object.keys(dateFixtures).forEach((pattern) => { const options = { pattern: pattern, minusSign: '-' }; const IntlDate = index_1.I18n.DateTimeFormat.getInstance('en', options); const formattedDate = IntlDate.format(date); expect(formattedDate).toMatch(dateFixtures[pattern]); }); }); it('should return correct formatted date in various language', () => { const deReg = /Freitag, 22. März 2019 um 09:03:01 GMT(\+|-)\d{2}:\d{2}/; const options = { pattern: 'full', minusSign: '-' }; const IntlDate = index_1.I18n.DateTimeFormat.getInstance('de', options); const formattedDate = IntlDate.format(date); expect(formattedDate).toMatch(deReg); }); it('should format with timezones', () => { const dateFixtures = { z: /GMT(\+|-)\d/, zzzz: /GMT(\+|-)\d{2}\:30/, Z: /(\+|-)\d{2}30/, }; Object.keys(dateFixtures).forEach((pattern) => { const options = { pattern: pattern, minusSign: '-', timezone: '+0430' }; const IntlDate = index_1.I18n.DateTimeFormat.getInstance('de', options); const formattedDate = IntlDate.format(date); expect(formattedDate).toMatch(dateFixtures[pattern]); }); }); it('should format invalid in Safari ISO date', () => { const time = formatter.getStandardTime('2019-02-22T09:03:01+0000'); const options = { pattern: 'mediumDate', minusSign: '-' }; const IntlDate = index_1.I18n.DateTimeFormat.getInstance('en', options); const formattedDate = IntlDate.format(time); expect(formattedDate).toEqual('Feb 22, 2019'); }); it('should format invalid in IE ISO date', () => { const time = formatter.getStandardTime('2019-02-22T09:03:01.014-0500'); const options = { pattern: 'mediumDate', minusSign: '-' }; const IntlDate = index_1.I18n.DateTimeFormat.getInstance('en', options); const formattedDate = IntlDate.format(time); expect(formattedDate).toEqual('Feb 22, 2019'); }); it('should format correctly with iso strings that contain time', () => { const time = formatter.getStandardTime('2019-02-22T09:03:01'); const options = { pattern: 'dd-MM-yyyy HH:mm', minusSign: '-' }; const formattedDate = index_1.I18n.DateTimeFormat.getInstance('en', options).format(time); expect(formattedDate).toMatch(/22-02-2019 \d{2}:\d{2}/); }); }); });