mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-10-08 14:51:32 +00:00
[AAE-1954] Search Unit test (#5503)
* [AAE-1954] Search Unit test * * fixed comments
This commit is contained in:
@@ -16,44 +16,42 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { SearchDateRangeComponent } from './search-date-range.component';
|
import { SearchDateRangeComponent } from './search-date-range.component';
|
||||||
import { of } from 'rxjs';
|
import { MomentDateAdapter, setupTestBed } from '@alfresco/adf-core';
|
||||||
import { MomentDateAdapter } from '@alfresco/adf-core';
|
import { DateAdapter } from '@angular/material/core';
|
||||||
|
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||||
|
import { ContentTestingModule } from '../../../testing/content.testing.module';
|
||||||
|
|
||||||
declare let moment: any;
|
declare let moment: any;
|
||||||
|
|
||||||
describe('SearchDateRangeComponent', () => {
|
describe('SearchDateRangeComponent', () => {
|
||||||
describe('component class', () => {
|
let fixture: ComponentFixture<SearchDateRangeComponent>;
|
||||||
|
|
||||||
let component: SearchDateRangeComponent;
|
let component: SearchDateRangeComponent;
|
||||||
|
let adapter: MomentDateAdapter;
|
||||||
const fromDate = '2016-10-16';
|
const fromDate = '2016-10-16';
|
||||||
const toDate = '2017-10-16';
|
const toDate = '2017-10-16';
|
||||||
const localeFixture = 'it';
|
|
||||||
const dateFormatFixture = 'DD-MMM-YY';
|
const dateFormatFixture = 'DD-MMM-YY';
|
||||||
|
|
||||||
const buildAdapter = (): MomentDateAdapter => {
|
setupTestBed({
|
||||||
const dateAdapter = new MomentDateAdapter();
|
imports: [
|
||||||
dateAdapter.overrideDisplayFormat = null;
|
ContentTestingModule
|
||||||
return dateAdapter;
|
]
|
||||||
};
|
});
|
||||||
|
|
||||||
const buildUserPreferences = (): any => {
|
|
||||||
const userPreferences = {
|
|
||||||
userPreferenceStatus: { LOCALE: localeFixture },
|
|
||||||
select: (property) => {
|
|
||||||
return of(userPreferences.userPreferenceStatus[property]);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
return userPreferences;
|
|
||||||
};
|
|
||||||
|
|
||||||
const theDateAdapter = <any> buildAdapter();
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
component = new SearchDateRangeComponent(theDateAdapter, buildUserPreferences());
|
fixture = TestBed.createComponent(SearchDateRangeComponent);
|
||||||
|
adapter = fixture.debugElement.injector.get(DateAdapter);
|
||||||
|
component = fixture.componentInstance;
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => fixture.destroy());
|
||||||
|
|
||||||
|
it('should use moment adapter', () => {
|
||||||
|
expect(adapter instanceof MomentDateAdapter).toBe(true);
|
||||||
|
expect(component.datePickerDateFormat).toBe('DD/MM/YYYY');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should setup form elements on init', () => {
|
it('should setup form elements on init', () => {
|
||||||
component.ngOnInit();
|
fixture.detectChanges();
|
||||||
expect(component.from).toBeDefined();
|
expect(component.from).toBeDefined();
|
||||||
expect(component.to).toBeDefined();
|
expect(component.to).toBeDefined();
|
||||||
expect(component.form).toBeDefined();
|
expect(component.form).toBeDefined();
|
||||||
@@ -61,13 +59,13 @@ describe('SearchDateRangeComponent', () => {
|
|||||||
|
|
||||||
it('should setup the format of the date from configuration', () => {
|
it('should setup the format of the date from configuration', () => {
|
||||||
component.settings = { field: 'cm:created', dateFormat: dateFormatFixture };
|
component.settings = { field: 'cm:created', dateFormat: dateFormatFixture };
|
||||||
component.ngOnInit();
|
fixture.detectChanges();
|
||||||
expect(theDateAdapter.overrideDisplayFormat).toBe(dateFormatFixture);
|
expect(adapter.overrideDisplayFormat).toBe(dateFormatFixture);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should setup form control with formatted valid date on change', () => {
|
it('should setup form control with formatted valid date on change', () => {
|
||||||
component.settings = { field: 'cm:created', dateFormat: dateFormatFixture };
|
component.settings = { field: 'cm:created', dateFormat: dateFormatFixture };
|
||||||
component.ngOnInit();
|
fixture.detectChanges();
|
||||||
|
|
||||||
const inputString = '20-feb-18';
|
const inputString = '20-feb-18';
|
||||||
const momentFromInput = moment(inputString, dateFormatFixture);
|
const momentFromInput = moment(inputString, dateFormatFixture);
|
||||||
@@ -79,7 +77,7 @@ describe('SearchDateRangeComponent', () => {
|
|||||||
|
|
||||||
it('should NOT setup form control with invalid date on change', () => {
|
it('should NOT setup form control with invalid date on change', () => {
|
||||||
component.settings = { field: 'cm:created', dateFormat: dateFormatFixture };
|
component.settings = { field: 'cm:created', dateFormat: dateFormatFixture };
|
||||||
component.ngOnInit();
|
fixture.detectChanges();
|
||||||
|
|
||||||
const inputString = '20.f.18';
|
const inputString = '20.f.18';
|
||||||
const momentFromInput = moment(inputString, dateFormatFixture);
|
const momentFromInput = moment(inputString, dateFormatFixture);
|
||||||
@@ -90,7 +88,7 @@ describe('SearchDateRangeComponent', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should reset form', () => {
|
it('should reset form', () => {
|
||||||
component.ngOnInit();
|
fixture.detectChanges();
|
||||||
component.form.setValue({ from: fromDate, to: toDate });
|
component.form.setValue({ from: fromDate, to: toDate });
|
||||||
|
|
||||||
expect(component.from.value).toEqual(fromDate);
|
expect(component.from.value).toEqual(fromDate);
|
||||||
@@ -117,7 +115,7 @@ describe('SearchDateRangeComponent', () => {
|
|||||||
|
|
||||||
spyOn(context, 'update').and.stub();
|
spyOn(context, 'update').and.stub();
|
||||||
|
|
||||||
component.ngOnInit();
|
fixture.detectChanges();
|
||||||
component.reset();
|
component.reset();
|
||||||
|
|
||||||
expect(context.queryFragments.createdDateRange).toEqual('');
|
expect(context.queryFragments.createdDateRange).toEqual('');
|
||||||
@@ -137,7 +135,7 @@ describe('SearchDateRangeComponent', () => {
|
|||||||
|
|
||||||
spyOn(context, 'update').and.stub();
|
spyOn(context, 'update').and.stub();
|
||||||
|
|
||||||
component.ngOnInit();
|
fixture.detectChanges();
|
||||||
component.apply({
|
component.apply({
|
||||||
from: fromDate,
|
from: fromDate,
|
||||||
to: toDate
|
to: toDate
|
||||||
@@ -150,5 +148,27 @@ describe('SearchDateRangeComponent', () => {
|
|||||||
expect(context.queryFragments[component.id]).toEqual(expectedQuery);
|
expect(context.queryFragments[component.id]).toEqual(expectedQuery);
|
||||||
expect(context.update).toHaveBeenCalled();
|
expect(context.update).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
it('should show date-format error when Invalid found', async(() => {
|
||||||
|
fixture.detectChanges();
|
||||||
|
const input = fixture.debugElement.nativeElement.querySelector('[data-automation-id="date-range-from-input"');
|
||||||
|
input.value = '10-05-18';
|
||||||
|
input.dispatchEvent(new Event('focusout'));
|
||||||
|
fixture.detectChanges();
|
||||||
|
fixture.whenStable().then(() => {
|
||||||
|
expect(component.getFromValidationMessage()).toEqual('SEARCH.FILTER.VALIDATION.INVALID-DATE');
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
|
||||||
|
it('should not show date-format error when valid found', async(() => {
|
||||||
|
fixture.detectChanges();
|
||||||
|
const input = fixture.debugElement.nativeElement.querySelector('[data-automation-id="date-range-from-input"');
|
||||||
|
input.value = '10/10/2018';
|
||||||
|
input.dispatchEvent(new Event('focusout'));
|
||||||
|
fixture.detectChanges();
|
||||||
|
fixture.whenStable().then(() => {
|
||||||
|
fixture.detectChanges();
|
||||||
|
expect(component.getFromValidationMessage()).toBeFalsy();
|
||||||
|
});
|
||||||
|
}));
|
||||||
});
|
});
|
||||||
|
@@ -17,13 +17,23 @@
|
|||||||
|
|
||||||
import { MatSliderChange } from '@angular/material';
|
import { MatSliderChange } from '@angular/material';
|
||||||
import { SearchSliderComponent } from './search-slider.component';
|
import { SearchSliderComponent } from './search-slider.component';
|
||||||
|
import { setupTestBed } from '@alfresco/adf-core';
|
||||||
|
import { ContentTestingModule } from '../../../testing/content.testing.module';
|
||||||
|
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
describe('SearchSliderComponent', () => {
|
describe('SearchSliderComponent', () => {
|
||||||
|
let fixture: ComponentFixture<SearchSliderComponent>;
|
||||||
let component: SearchSliderComponent;
|
let component: SearchSliderComponent;
|
||||||
|
|
||||||
|
setupTestBed({
|
||||||
|
imports: [
|
||||||
|
ContentTestingModule
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
component = new SearchSliderComponent();
|
fixture = TestBed.createComponent(SearchSliderComponent);
|
||||||
|
component = fixture.componentInstance;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should setup slider from settings', () => {
|
it('should setup slider from settings', () => {
|
||||||
@@ -35,7 +45,7 @@ describe('SearchSliderComponent', () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
component.settings = settings;
|
component.settings = settings;
|
||||||
component.ngOnInit();
|
fixture.detectChanges();
|
||||||
|
|
||||||
expect(component.min).toEqual(settings.min);
|
expect(component.min).toEqual(settings.min);
|
||||||
expect(component.max).toEqual(settings.max);
|
expect(component.max).toEqual(settings.max);
|
||||||
@@ -64,10 +74,13 @@ describe('SearchSliderComponent', () => {
|
|||||||
component.settings = { field: 'cm:content.size' };
|
component.settings = { field: 'cm:content.size' };
|
||||||
|
|
||||||
component.onChangedHandler(<MatSliderChange> { value: 10 });
|
component.onChangedHandler(<MatSliderChange> { value: 10 });
|
||||||
|
fixture.detectChanges();
|
||||||
const expectedQuery = 'cm:content.size:[0 TO 10]';
|
expect(context.queryFragments[component.id]).toEqual('cm:content.size:[0 TO 10]');
|
||||||
expect(context.queryFragments[component.id]).toEqual(expectedQuery);
|
|
||||||
expect(context.update).toHaveBeenCalled();
|
expect(context.update).toHaveBeenCalled();
|
||||||
|
|
||||||
|
component.onChangedHandler(<MatSliderChange> { value: 20 });
|
||||||
|
fixture.detectChanges();
|
||||||
|
expect(context.queryFragments[component.id]).toEqual('cm:content.size:[0 TO 20]');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should reset the value for query builder', () => {
|
it('should reset the value for query builder', () => {
|
||||||
@@ -88,14 +101,13 @@ describe('SearchSliderComponent', () => {
|
|||||||
component.context = context;
|
component.context = context;
|
||||||
component.value = 20;
|
component.value = 20;
|
||||||
component.id = 'slider';
|
component.id = 'slider';
|
||||||
component.ngOnInit();
|
|
||||||
|
|
||||||
spyOn(context, 'update').and.stub();
|
spyOn(context, 'update').and.stub();
|
||||||
|
fixture.detectChanges();
|
||||||
|
|
||||||
component.reset();
|
component.reset();
|
||||||
|
|
||||||
expect(component.value).toBe(settings.min);
|
expect(component.value).toBe(settings.min);
|
||||||
expect(context.queryFragments['slider']).toBe('');
|
expect(context.queryFragments[component.id]).toBe('');
|
||||||
expect(context.update).toHaveBeenCalled();
|
expect(context.update).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -117,9 +129,8 @@ describe('SearchSliderComponent', () => {
|
|||||||
component.context = context;
|
component.context = context;
|
||||||
component.value = 20;
|
component.value = 20;
|
||||||
component.id = 'slider';
|
component.id = 'slider';
|
||||||
component.ngOnInit();
|
|
||||||
|
|
||||||
spyOn(context, 'update').and.stub();
|
spyOn(context, 'update').and.stub();
|
||||||
|
fixture.detectChanges();
|
||||||
|
|
||||||
component.reset();
|
component.reset();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user