[ACS-4985] Updated test cases for SearchFilterTabbedComponent. Added safety checks to component

This commit is contained in:
swapnil.verma
2023-07-27 11:14:48 +05:30
committed by Jatin_Chugh
parent 18586bb760
commit 5ca8856d1c
@@ -16,7 +16,6 @@
*/ */
import { ComponentFixture, TestBed } from '@angular/core/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { TranslateModule } from '@ngx-translate/core'; import { TranslateModule } from '@ngx-translate/core';
import { ContentTestingModule } from '../../../testing/content.testing.module'; import { ContentTestingModule } from '../../../testing/content.testing.module';
import { SearchFilterTabbedComponent } from './search-filter-tabbed.component'; import { SearchFilterTabbedComponent } from './search-filter-tabbed.component';
@@ -26,122 +25,73 @@ describe('SearchFilterTabbedComponent', () => {
let fixture: ComponentFixture<SearchFilterTabbedComponent>; let fixture: ComponentFixture<SearchFilterTabbedComponent>;
beforeEach(async () => { beforeEach(async () => {
TestBed.configureTestingModule({ await TestBed.configureTestingModule({
declarations: [SearchFilterTabbedComponent], declarations: [SearchFilterTabbedComponent],
imports: [ imports: [
TranslateModule.forRoot(), TranslateModule.forRoot(),
ContentTestingModule ContentTestingModule
] ]
}); });
fixture = TestBed.createComponent(SearchFilterTabbedComponent); fixture = TestBed.createComponent(SearchFilterTabbedComponent);
component = fixture.componentInstance; component = fixture.componentInstance;
component.id = 'tabbed'; fixture.detectChanges();
component.context = { });
queryFragments: {
field1: '', it('should emit a single combined query when multiple queries are provided', () => {
field2: '' spyOn(component.queriesCombined, 'emit');
}, component.queries = {
update: jasmine.createSpy() testField1: 'test-query-1',
} as any; testField2: 'test-query-2',
component.settings = { testField3: 'test-query-3'
field: undefined,
hideDefaultAction: true,
displayLabelSeparator: ';',
tabs: [
{
id: 'widget1',
tabDisplayLabel: 'Widget1',
component: {
settings: {
field: 'field1'
}
}
},
{
id: 'widget2',
tabDisplayLabel: 'Widget2',
component: {
settings: {
field: 'field2'
}
}
}
]
}; };
fixture.detectChanges(); fixture.detectChanges();
expect(component.queriesCombined.emit).toHaveBeenCalledWith('test-query-1 AND test-query-2 AND test-query-3');
}); });
function getButtonByDataAutomationId(buttonDataAutomationId: string) { it ('should emit a single combined value to display when multiple display values are provided', () => {
return fixture.debugElement.query(By.css(`[data-automation-id="${buttonDataAutomationId}"]`)).nativeElement; spyOn(component.valuesToDisplayCombined, 'emit');
component.settings = {
field: 'testField1, testField2, testField3',
displayedLabelsByField: {
testField1: 'Field 1',
testField2: 'Field 2',
testField3: 'Field 3'
} }
it('should update displayLabel when inner widgets display label is updated', async () => {
spyOn(component.displayValue$, 'next');
fixture.detectChanges();
component.widgetContainerComponentList.get(0).componentRef.instance.displayValue$.next('test-label-1');
fixture.detectChanges();
await fixture.whenStable();
expect(component.displayValue$.next).toHaveBeenCalledWith('Widget1 test-label-1');
component.widgetContainerComponentList.get(1).componentRef.instance.displayValue$.next('test-label-2');
fixture.detectChanges();
await fixture.whenStable();
expect(component.displayValue$.next).toHaveBeenCalledWith('Widget1 test-label-1; Widget2 test-label-2');
});
it('should call submitValues for inner widgets and update context when apply button is clicked', () => {
component.widgetContainerComponentList.forEach(widget => {
spyOn(widget, 'applyInnerWidget');
});
const resetButton = getButtonByDataAutomationId('tab-apply-btn');
resetButton.dispatchEvent(new Event('click'));
fixture.detectChanges();
component.widgetContainerComponentList.forEach(widget => {
expect(widget.applyInnerWidget).toHaveBeenCalled();
});
expect(component.context.update).toHaveBeenCalled();
});
it('should reset inner widgets when clear button is clicked', () => {
component.widgetContainerComponentList.forEach(widget => {
spyOn(widget, 'resetInnerWidget');
});
const resetButton = getButtonByDataAutomationId('tab-clear-btn');
resetButton.dispatchEvent(new Event('click'));
fixture.detectChanges();
component.widgetContainerComponentList.forEach(widget => {
expect(widget.resetInnerWidget).toHaveBeenCalled();
});
});
it('should have valid value when inner widgets have valid values', () => {
component.widgetContainerComponentList.forEach(widget => {
spyOn(widget, 'hasValueSelected').and.returnValue(true);
});
expect(component.hasValidValue()).toBe(true);
});
it('should set values on init for inner widgets if initial value is provided', () => {
component.widgetContainerComponentList.forEach(widget => {
spyOn(widget, 'setValue');
});
const valueForWidget1 = {
testStringValue: 'test value',
testNumberValue: 28
} }
const valueForWidget2 = { component.valuesToDisplay = {
testBooleanValue: false, testField1: 'test-display-value-1',
testStringValue: 'test value' testField2: 'test-display-value-2',
testField3: 'test-display-value-3'
};
fixture.detectChanges();
expect(component.valuesToDisplayCombined.emit).toHaveBeenCalledWith('FIELD 1: test-display-value-1 FIELD 2: test-display-value-2 FIELD 3: test-display-value-3');
});
it('should emit translated display labels by field when settings are set', () => {
spyOn(component.displayedLabelsByFieldTranslated, 'emit');
component.settings = {
field: 'testField1, testField2, testField3',
displayedLabelsByField: {
testField1: 'Field 1',
testField2: 'Field 2',
testField3: 'Field 3'
} }
component.startValue = {
widget1: valueForWidget1,
widget2: valueForWidget2
} }
component.ngOnInit(); fixture.detectChanges();
component.widgetContainerComponentList.forEach(widget => { const displayedLabelsMap = {
const value = component.startValue[widget.id]; testField1: 'Field 1',
expect(widget.setValue).toHaveBeenCalledWith(value); testField2: 'Field 2',
testField3: 'Field 3'
}
expect(component.displayedLabelsByFieldTranslated.emit).toHaveBeenCalledWith(displayedLabelsMap);
}); });
it('should emit an array of fields when all the fields are provided in a combined string via settings', () => {
spyOn(component.fieldsChanged, 'emit');
component.settings = {
field: 'testField1, testField2, testField3'
};
fixture.detectChanges();
expect(component.fieldsChanged.emit).toHaveBeenCalledWith(['testField1', 'testField2', 'testField3']);
}); });
}); });