4da483086ee489e07d31322ccd3cdf99da5b599c
[evergreen-equinox.git] / Open-ILS / src / eg2 / src / app / core / format.spec.ts
1 import {DatePipe, DecimalPipe, 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';
18
19 describe('FormatService', () => {
20
21     let decimalPipe: DecimalPipe;
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     // eslint-disable-next-line prefer-const
34     let location: SpyLocation;
35     let service: FormatService;
36
37     beforeEach(() => {
38         decimalPipe = new DecimalPipe('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(
51             datePipe,
52             decimalPipe,
53             idlService,
54             orgService,
55             authService,
56             localeService
57         );
58     });
59
60     const initTestData = () => {
61         idlService.parseIdl();
62         const win: any = window; // trick TS
63         win._eg_mock_data.generateOrgTree(idlService, orgService);
64     };
65
66     it('should format an org unit name', () => {
67         initTestData();
68         const str = service.transform({
69             value: orgService.root(),
70             datatype: 'org_unit',
71             orgField: 'shortname' // currently the default
72         });
73         expect(str).toBe('ROOT');  // from eg_mock.js
74     });
75
76     it('should format a date', () => {
77         initTestData();
78         const str = service.transform({
79             value: new Date(2018, 6, 5),
80             datatype: 'timestamp',
81         });
82         expect(str).toBe('7/5/18');
83     });
84
85     it('should format a date plus time', () => {
86         initTestData();
87         const str = service.transform({
88             value: new Date(2018, 6, 5, 12, 30, 1),
89             datatype: 'timestamp',
90             datePlusTime: true
91         });
92         expect(str).toBe('7/5/18, 12:30 PM');
93     });
94
95
96
97     it('should format money', () => {
98         initTestData();
99         const str = service.transform({
100             value: '12.1',
101             datatype: 'money'
102         });
103         expect(str).toBe('12.10');
104     });
105
106     it('should transform M/d/yy, h:mm a Angular format string to a valid MomentJS one', () => {
107         const momentVersion = service['makeFormatParseable']('M/d/yy, h:mm a', 'en-US');
108         expect(momentVersion).toBe('M/D/YY, h:mm a');
109     });
110     it('should transform MMM d, y, h:mm:ss a Angular format string to a valid MomentJS one', () => {
111         const momentVersion = service['makeFormatParseable']('MMM d, y, h:mm:ss a', 'ar-JO');
112         expect(momentVersion).toBe('MMM D, Y, h:mm:ss a');
113     });
114     it('should transform MMMM d, y, h:mm:ss a z Angular format strings to a valid MomentJS one', () => {
115         const momentVersion = service['makeFormatParseable']('MMMM d, y, h:mm:ss a z', 'fr-CA');
116         expect(momentVersion).toBe('MMMM D, Y, h:mm:ss a [GMT]Z');
117     });
118     it('should transform full Angular format strings to a valid MomentJS one using Angular locale en-US', () => {
119         const momentVersion = service['makeFormatParseable']('full', 'en-US');
120         expect(momentVersion).toBe('dddd, MMMM D, Y [at] h:mm:ss a [GMT]Z');
121     });
122     it('should transform shortDate Angular format strings to a valid MomentJS one using Angular locale cs-CZ', () => {
123         registerLocaleData(localeCs);
124         const momentVersion = service['makeFormatParseable']('shortDate', 'cs-CZ');
125         expect(momentVersion).toBe('DD.MM.YY');
126     });
127     it('should transform mediumDate Angular format strings to a valid MomentJS one using Angular locale fr-CA', () => {
128         registerLocaleData(localeFrCA);
129         const momentVersion = service['makeFormatParseable']('mediumDate', 'fr-CA');
130         expect(momentVersion).toBe('D MMM Y');
131     });
132     it('should transform long Angular format strings to a valid MomentJS one using Angular locale ar-JO', () => {
133         registerLocaleData(localeArJO);
134         const momentVersion = service['makeFormatParseable']('long', 'ar-JO');
135         expect(momentVersion).toBe('D MMMM Y h:mm:ss a [GMT]Z');
136     });
137     it('can create a valid Momentjs object given a valid datetime string and correct format', () => {
138         const moment = service['momentize']('7/3/12, 6:06 PM', 'M/D/YY, h:mm a', 'Africa/Addis_Ababa', false);
139         expect(moment.isValid()).toBe(true);
140     });
141     it('can create a valid Momentjs object given a valid datetime string and a dateTimeFormat from org settings', () => {
142         service['dateTimeFormat'] = 'M/D/YY, h:mm a';
143         const moment = service.momentizeDateTimeString('7/3/12, 6:06 PM', 'Africa/Addis_Ababa', false, 'fr-CA');
144         expect(moment.isValid()).toBe(true);
145     });
146
147 });
148