1 import {DatePipe, CurrencyPipe, registerLocaleData} from '@angular/common';
2 import {IdlService} from './idl.service';
3 import {EventService} from './event.service';
4 import {DbStoreService} from './db-store.service';
5 import {NetService} from './net.service';
6 import {AuthService} from './auth.service';
7 import {PcrudService} from './pcrud.service';
8 import {StoreService} from './store.service';
9 import {OrgService} from './org.service';
10 import {LocaleService} from './locale.service';
11 import {Location} from '@angular/common';
12 import {FormatService} from './format.service';
13 import {HatchService} from './hatch.service';
14 import {SpyLocation} from '@angular/common/testing';
15 import localeArJO from '@angular/common/locales/ar-JO';
16 import localeCs from '@angular/common/locales/cs';
17 import localeFrCA from '@angular/common/locales/fr-CA';
19 describe('FormatService', () => {
21 let currencyPipe: CurrencyPipe;
22 let datePipe: DatePipe;
23 let idlService: IdlService;
24 let netService: NetService;
25 let authService: AuthService;
26 let pcrudService: PcrudService;
27 let orgService: OrgService;
28 let evtService: EventService;
29 let storeService: StoreService;
30 let dbStoreService: DbStoreService;
31 let localeService: LocaleService;
32 let hatchService: HatchService;
33 // tslint:disable-next-line:prefer-const
34 let location: SpyLocation;
35 let service: FormatService;
38 currencyPipe = new CurrencyPipe('en');
39 datePipe = new DatePipe('en');
40 idlService = new IdlService();
41 evtService = new EventService();
42 hatchService = new HatchService();
43 storeService = new StoreService(null /* CookieService */, hatchService);
44 netService = new NetService(evtService);
45 authService = new AuthService(evtService, netService, storeService);
46 pcrudService = new PcrudService(idlService, netService, authService);
47 dbStoreService = new DbStoreService();
48 orgService = new OrgService(dbStoreService, netService, authService, pcrudService);
49 localeService = new LocaleService(location, null, pcrudService);
50 service = new FormatService(
59 const initTestData = () => {
60 idlService.parseIdl();
61 const win: any = window; // trick TS
62 win._eg_mock_data.generateOrgTree(idlService, orgService);
65 it('should format an org unit name', () => {
67 const str = service.transform({
68 value: orgService.root(),
70 orgField: 'shortname' // currently the default
72 expect(str).toBe('ROOT'); // from eg_mock.js
75 it('should format a date', () => {
77 const str = service.transform({
78 value: new Date(2018, 6, 5),
79 datatype: 'timestamp',
81 expect(str).toBe('7/5/18');
84 it('should format a date plus time', () => {
86 const str = service.transform({
87 value: new Date(2018, 6, 5, 12, 30, 1),
88 datatype: 'timestamp',
91 expect(str).toBe('7/5/18, 12:30 PM');
96 it('should format money', () => {
98 const str = service.transform({
102 expect(str).toBe('$12.10');
105 it('should transform M/d/yy, h:mm a Angular format string to a valid MomentJS one', () => {
106 const momentVersion = service['makeFormatParseable']('M/d/yy, h:mm a', 'en-US');
107 expect(momentVersion).toBe('M/D/YY, h:mm a');
109 it('should transform MMM d, y, h:mm:ss a Angular format string to a valid MomentJS one', () => {
110 const momentVersion = service['makeFormatParseable']('MMM d, y, h:mm:ss a', 'ar-JO');
111 expect(momentVersion).toBe('MMM D, Y, h:mm:ss a');
113 it('should transform MMMM d, y, h:mm:ss a z Angular format strings to a valid MomentJS one', () => {
114 const momentVersion = service['makeFormatParseable']('MMMM d, y, h:mm:ss a z', 'fr-CA');
115 expect(momentVersion).toBe('MMMM D, Y, h:mm:ss a [GMT]Z');
117 it('should transform full Angular format strings to a valid MomentJS one using Angular locale en-US', () => {
118 const momentVersion = service['makeFormatParseable']('full', 'en-US');
119 expect(momentVersion).toBe('dddd, MMMM D, Y [at] h:mm:ss a [GMT]Z');
121 it('should transform shortDate Angular format strings to a valid MomentJS one using Angular locale cs-CZ', () => {
122 registerLocaleData(localeCs);
123 const momentVersion = service['makeFormatParseable']('shortDate', 'cs-CZ');
124 expect(momentVersion).toBe('DD.MM.YY');
126 it('should transform mediumDate Angular format strings to a valid MomentJS one using Angular locale fr-CA', () => {
127 registerLocaleData(localeFrCA);
128 const momentVersion = service['makeFormatParseable']('mediumDate', 'fr-CA');
129 expect(momentVersion).toBe('D MMM Y');
131 it('should transform long Angular format strings to a valid MomentJS one using Angular locale ar-JO', () => {
132 registerLocaleData(localeArJO);
133 const momentVersion = service['makeFormatParseable']('long', 'ar-JO');
134 expect(momentVersion).toBe('D MMMM Y h:mm:ss a [GMT]Z');
136 it('can create a valid Momentjs object given a valid datetime string and correct format', () => {
137 const moment = service['momentize']('7/3/12, 6:06 PM', 'M/D/YY, h:mm a', 'Africa/Addis_Ababa', false);
138 expect(moment.isValid()).toBe(true);
140 it('can create a valid Momentjs object given a valid datetime string and a dateTimeFormat from org settings', () => {
141 service['dateTimeFormat'] = 'M/D/YY, h:mm a';
142 const moment = service.momentizeDateTimeString('7/3/12, 6:06 PM', 'Africa/Addis_Ababa', false, 'fr-CA');
143 expect(moment.isValid()).toBe(true);