-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: 100% coverage of format-date directive (#2423)
* test: 100% coverage of format-date directive * minor
- Loading branch information
1 parent
051f106
commit 8b0df7b
Showing
1 changed file
with
68 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,68 @@ | ||
// TODO: uncomment when elementRef is figured out for testing | ||
// import { FormatDateDirective } from './format-date.directive'; | ||
|
||
// xdescribe('FormatDateDirective', () => { | ||
// it('should create an instance', () => { | ||
// const directive = new FormatDateDirective(); | ||
// expect(directive).toBeTruthy(); | ||
// }); | ||
// }); | ||
import { Component, DebugElement, Renderer2 } from '@angular/core'; | ||
import { FormatDateDirective } from './format-date.directive'; | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { By } from '@angular/platform-browser'; | ||
|
||
@Component({ | ||
template: `<input appFormatDate type="date" />`, | ||
}) | ||
class TestFormatDateDirectiveComponent {} | ||
|
||
describe('FormatDateDirective', () => { | ||
let component: TestFormatDateDirectiveComponent; | ||
let fixture: ComponentFixture<TestFormatDateDirectiveComponent>; | ||
let inputEl: DebugElement; | ||
let directive: FormatDateDirective; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [TestFormatDateDirectiveComponent, FormatDateDirective], | ||
}); | ||
fixture = TestBed.createComponent(TestFormatDateDirectiveComponent); | ||
component = fixture.componentInstance; | ||
inputEl = fixture.debugElement.query(By.css('input')); | ||
directive = inputEl.injector.get(FormatDateDirective); | ||
}); | ||
|
||
it('should create an instance', () => { | ||
expect(directive).toBeTruthy(); | ||
}); | ||
|
||
it('get selectedElement(): should return the current html element', () => { | ||
const selectedElement = inputEl.nativeElement; | ||
expect(directive.selectedElement).toEqual(selectedElement); | ||
}); | ||
|
||
it('onChange(): should call modifyDisplayValue once', () => { | ||
spyOn(directive, 'modifyDisplayValue'); | ||
directive.onChange('2023-01-02'); | ||
expect(directive.modifyDisplayValue).toHaveBeenCalledOnceWith('2023-01-02'); | ||
}); | ||
|
||
it('ngOnInit(): should call modifyDisplayValue once with the initial value', () => { | ||
spyOn(directive, 'modifyDisplayValue'); | ||
directive.ngOnInit(); | ||
expect(directive.modifyDisplayValue).toHaveBeenCalledOnceWith(''); | ||
}); | ||
|
||
describe('modifyDisplayValue()', () => { | ||
it('should remove class date-input__placeholder and set data-date attribute to date provided in the input', () => { | ||
directive.modifyDisplayValue('2023-01-02'); | ||
expect(inputEl.nativeElement.getAttribute('data-date')).toEqual('Jan 02, 2023'); | ||
expect(inputEl.nativeElement.classList.contains('date-input__placeholder')).toBeFalse(); | ||
}); | ||
|
||
it('should add class date-input__placeholder and set data-date attribute to Select date if no name attribute is present', () => { | ||
directive.modifyDisplayValue(''); | ||
expect(inputEl.nativeElement.getAttribute('data-date')).toEqual('Select date'); | ||
expect(inputEl.nativeElement.classList.contains('date-input__placeholder')).toBeTrue(); | ||
}); | ||
|
||
it('should add class date-input__placeholder and set data-date attribute to Select journey date if name attribute is equal to journey date', () => { | ||
inputEl.nativeElement.name = 'journey date'; | ||
directive.modifyDisplayValue(''); | ||
expect(inputEl.nativeElement.getAttribute('data-date')).toEqual('Select journey date'); | ||
expect(inputEl.nativeElement.classList.contains('date-input__placeholder')).toBeTrue(); | ||
}); | ||
}); | ||
}); |