From e6fe1aec511c1e1c0101dab8ce48606c50f8a6ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Wei=C3=9F?= Date: Mon, 5 Sep 2022 17:38:07 +0200 Subject: [PATCH] fixup! feat(common): add injection token for default date pipe format --- packages/common/test/pipes/date_pipe_spec.ts | 26 ++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/common/test/pipes/date_pipe_spec.ts b/packages/common/test/pipes/date_pipe_spec.ts index a047416580d86c..6f9eb938eb7146 100644 --- a/packages/common/test/pipes/date_pipe_spec.ts +++ b/packages/common/test/pipes/date_pipe_spec.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {DatePipe} from '@angular/common'; +import {DATE_PIPE_DEFAULT_FORMAT, DatePipe} from '@angular/common'; import localeEn from '@angular/common/locales/en'; import localeEnExtra from '@angular/common/locales/extra/en'; import {Component, ɵregisterLocaleData, ɵunregisterLocaleData} from '@angular/core'; @@ -72,9 +72,31 @@ import {TestBed} from '@angular/core/testing'; }); describe('transform', () => { - it('should use "mediumDate" as the default format', + it('should use "mediumDate" as the default format if no default format is provided', () => expect(pipe.transform('2017-01-11T10:14:39+0000')).toEqual('Jan 11, 2017')); + it('should give precedence to the passed in format over the default one', + () => expect(pipe.transform('2017-01-11T10:14:39+0000', 'shortDate')).toEqual('1/11/17')); + + it('should take the default format into account when no format is passed in', () => { + @Component({ + selector: 'test-component', + imports: [DatePipe], + template: '{{ value | date }}', + standalone: true, + providers: [{provide: DATE_PIPE_DEFAULT_FORMAT, useValue: 'shortDate'}] + }) + class TestComponent { + value = '2017-01-11T10:14:39+0000'; + } + + const fixture = TestBed.createComponent(TestComponent); + fixture.detectChanges(); + + const content = fixture.nativeElement.textContent; + expect(content).toBe('1/11/17'); + }); + it('should return first week if some dates fall in previous year but belong to next year according to ISO 8601 format', () => { expect(pipe.transform('2019-12-28T00:00:00', 'w')).toEqual('52');