first part random test fix (#3376)

fixing random test executions first part
This commit is contained in:
Eugenio Romano
2018-05-23 01:23:54 +01:00
committed by GitHub
parent df0e530f27
commit 7e67257b8a
16 changed files with 773 additions and 720 deletions

View File

@@ -65,6 +65,7 @@ describe('SearchControlComponent', () => {
let fixtureCustom: ComponentFixture<SimpleSearchTestCustomEmptyComponent>;
let elementCustom: HTMLElement;
let componentCustom: SimpleSearchTestCustomEmptyComponent;
let searchServiceSpy: any;
setupTestBed({
imports: [
@@ -92,6 +93,13 @@ describe('SearchControlComponent', () => {
spyOn(authService, 'isEcmLoggedIn').and.returnValue(true);
component = fixture.componentInstance;
element = fixture.nativeElement;
searchServiceSpy = spyOn(searchService, 'search').and.callThrough();
});
afterEach(() => {
fixture.destroy();
TestBed.resetTestingModule();
});
function typeWordIntoSearchInput(word: string): void {
@@ -108,7 +116,7 @@ describe('SearchControlComponent', () => {
});
it('should emit searchChange when search term input changed', async(() => {
spyOn(searchService, 'search').and.returnValue(
searchServiceSpy.and.returnValue(
Observable.of({ entry: { list: [] } })
);
component.searchChange.subscribe(value => {
@@ -122,7 +130,7 @@ describe('SearchControlComponent', () => {
it('should update FAYT search when user inputs a valid term', async(() => {
typeWordIntoSearchInput('customSearchTerm');
spyOn(component, 'isSearchBarActive').and.returnValue(true);
spyOn(searchService, 'search').and.returnValue(Observable.of(results));
searchServiceSpy.and.returnValue(Observable.of(results));
fixture.detectChanges();
fixture.whenStable().then(() => {
@@ -136,7 +144,7 @@ describe('SearchControlComponent', () => {
it('should NOT update FAYT term when user inputs an empty string as search term ', async(() => {
typeWordIntoSearchInput('');
spyOn(component, 'isSearchBarActive').and.returnValue(true);
spyOn(searchService, 'search').and.returnValue(Observable.of(results));
searchServiceSpy.and.returnValue(Observable.of(results));
fixture.detectChanges();
fixture.whenStable().then(() => {
@@ -146,7 +154,7 @@ describe('SearchControlComponent', () => {
}));
it('should still fire an event when user inputs a search term less than 3 characters', async(() => {
spyOn(searchService, 'search').and.returnValue(Observable.of(results));
searchServiceSpy.and.returnValue(Observable.of(results));
component.searchChange.subscribe(value => {
expect(value).toBe('cu');
@@ -203,13 +211,14 @@ describe('SearchControlComponent', () => {
expect(element.querySelectorAll('input[type="text"]')[0].getAttribute('autocomplete')).toBe('on');
}));
it('should fire a search when a enter key is pressed', async(() => {
xit('should fire a search when a enter key is pressed', (done) => {
component.submit.subscribe((value) => {
expect(value).toBe('TEST');
done();
});
spyOn(component, 'isSearchBarActive').and.returnValue(true);
spyOn(searchService, 'search').and.returnValue(Observable.of(results));
searchServiceSpy.and.returnValue(Observable.of(results));
fixture.detectChanges();
let inputDebugElement = debugElement.query(By.css('#adf-control-input'));
@@ -217,7 +226,7 @@ describe('SearchControlComponent', () => {
let enterKeyEvent: any = new Event('keyup');
enterKeyEvent.keyCode = '13';
inputDebugElement.nativeElement.dispatchEvent(enterKeyEvent);
}));
});
});
describe('autocomplete list', () => {
@@ -227,9 +236,9 @@ describe('SearchControlComponent', () => {
expect(element.querySelector('#autocomplete-search-result-list')).toBeNull();
}));
it('should make autocomplete list control visible when search box has focus and there is a search result', async(() => {
it('should make autocomplete list control visible when search box has focus and there is a search result', (done) => {
spyOn(component, 'isSearchBarActive').and.returnValue(true);
spyOn(searchService, 'search').and.returnValue(Observable.of(results));
searchServiceSpy.and.returnValue(Observable.of(results));
fixture.detectChanges();
typeWordIntoSearchInput('TEST');
@@ -238,12 +247,13 @@ describe('SearchControlComponent', () => {
fixture.detectChanges();
let resultElement: Element = element.querySelector('#autocomplete-search-result-list');
expect(resultElement).not.toBe(null);
done();
});
}));
});
it('should show autocomplete list noe results when search box has focus and there is search result with length 0', async(() => {
it('should show autocomplete list noe results when search box has focus and there is search result with length 0', (done) => {
spyOn(component, 'isSearchBarActive').and.returnValue(true);
spyOn(searchService, 'search').and.returnValue(Observable.of(noResult));
searchServiceSpy.and.returnValue(Observable.of(noResult));
fixture.detectChanges();
typeWordIntoSearchInput('NO RES');
@@ -252,12 +262,13 @@ describe('SearchControlComponent', () => {
fixture.detectChanges();
let noResultElement: Element = element.querySelector('#search_no_result');
expect(noResultElement).not.toBe(null);
done();
});
}));
});
it('should hide autocomplete list results when the search box loses focus', async(() => {
it('should hide autocomplete list results when the search box loses focus', (done) => {
spyOn(component, 'isSearchBarActive').and.returnValue(true);
spyOn(searchService, 'search').and.returnValue(Observable.of(results));
searchServiceSpy.and.returnValue(Observable.of(results));
fixture.detectChanges();
let inputDebugElement = debugElement.query(By.css('#adf-control-input'));
@@ -272,12 +283,13 @@ describe('SearchControlComponent', () => {
fixture.detectChanges();
resultElement = element.querySelector('#autocomplete-search-result-list');
expect(resultElement).not.toBe(null);
done();
});
}));
});
it('should keep autocomplete list control visible when user tabs into results', async(() => {
it('should keep autocomplete list control visible when user tabs into results', (done) => {
spyOn(component, 'isSearchBarActive').and.returnValue(true);
spyOn(searchService, 'search').and.returnValue(Observable.of(results));
searchServiceSpy.and.returnValue(Observable.of(results));
fixture.detectChanges();
let inputDebugElement = debugElement.query(By.css('#adf-control-input'));
@@ -292,12 +304,13 @@ describe('SearchControlComponent', () => {
fixture.detectChanges();
expect(element.querySelector('#autocomplete-search-result-list')).not.toBeNull();
done();
});
}));
});
it('should close the autocomplete when user press ESCAPE', async(() => {
it('should close the autocomplete when user press ESCAPE', (done) => {
spyOn(component, 'isSearchBarActive').and.returnValue(true);
spyOn(searchService, 'search').and.returnValue(Observable.of(results));
searchServiceSpy.and.returnValue(Observable.of(results));
fixture.detectChanges();
let inputDebugElement = debugElement.query(By.css('#adf-control-input'));
@@ -314,13 +327,14 @@ describe('SearchControlComponent', () => {
fixture.detectChanges();
resultElement = <HTMLElement> element.querySelector('#result_option_0');
expect(resultElement).toBeNull();
done();
});
});
}));
});
it('should close the autocomplete when user press ENTER on input', async(() => {
it('should close the autocomplete when user press ENTER on input', (done) => {
spyOn(component, 'isSearchBarActive').and.returnValue(true);
spyOn(searchService, 'search').and.returnValue(Observable.of(results));
searchServiceSpy.and.returnValue(Observable.of(results));
fixture.detectChanges();
let inputDebugElement = debugElement.query(By.css('#adf-control-input'));
@@ -337,13 +351,14 @@ describe('SearchControlComponent', () => {
fixture.detectChanges();
resultElement = <HTMLElement> element.querySelector('#result_option_0');
expect(resultElement).toBeNull();
done();
});
});
}));
});
it('should focus input element when autocomplete list is cancelled', async(() => {
it('should focus input element when autocomplete list is cancelled', (done) => {
spyOn(component, 'isSearchBarActive').and.returnValue(true);
spyOn(searchService, 'search').and.returnValue(Observable.of(results));
searchServiceSpy.and.returnValue(Observable.of(results));
fixture.detectChanges();
let inputDebugElement = debugElement.query(By.css('#adf-control-input'));
@@ -355,11 +370,12 @@ describe('SearchControlComponent', () => {
fixture.whenStable().then(() => {
expect(element.querySelector('#result_name_0')).toBeNull();
expect(document.activeElement.id).toBe(inputDebugElement.nativeElement.id);
done();
});
}));
});
it('should NOT display a autocomplete list control when configured not to', async(() => {
spyOn(searchService, 'search').and.returnValue(Observable.of(results));
it('should NOT display a autocomplete list control when configured not to', (done) => {
searchServiceSpy.and.returnValue(Observable.of(results));
component.liveSearchEnabled = false;
fixture.detectChanges();
@@ -367,11 +383,12 @@ describe('SearchControlComponent', () => {
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(element.querySelector('#autocomplete-search-result-list')).toBeNull();
done();
});
}));
});
it('should select the first item on autocomplete list when ARROW DOWN is pressed on input', async(() => {
spyOn(searchService, 'search').and.returnValue(Observable.of(results));
it('should select the first item on autocomplete list when ARROW DOWN is pressed on input', (done) => {
searchServiceSpy.and.returnValue(Observable.of(results));
fixture.detectChanges();
typeWordIntoSearchInput('TEST');
let inputDebugElement = debugElement.query(By.css('#adf-control-input'));
@@ -383,11 +400,12 @@ describe('SearchControlComponent', () => {
inputDebugElement.triggerEventHandler('keyup.arrowdown', {});
fixture.detectChanges();
expect(document.activeElement.id).toBe('result_option_0');
done();
});
}));
});
it('should select the second item on autocomplete list when ARROW DOWN is pressed on list', async(() => {
spyOn(searchService, 'search').and.returnValue(Observable.of(results));
it('should select the second item on autocomplete list when ARROW DOWN is pressed on list', (done) => {
searchServiceSpy.and.returnValue(Observable.of(results));
fixture.detectChanges();
let inputDebugElement = debugElement.query(By.css('#adf-control-input'));
typeWordIntoSearchInput('TEST');
@@ -404,11 +422,12 @@ describe('SearchControlComponent', () => {
firstElement.triggerEventHandler('keyup.arrowdown', { target: firstElement.nativeElement });
fixture.detectChanges();
expect(document.activeElement.id).toBe('result_option_1');
done();
});
}));
});
it('should focus the input search when ARROW UP is pressed on the first list item', async(() => {
spyOn(searchService, 'search').and.returnValue(Observable.of(results));
it('should focus the input search when ARROW UP is pressed on the first list item', (done) => {
searchServiceSpy.and.returnValue(Observable.of(results));
fixture.detectChanges();
let inputDebugElement = debugElement.query(By.css('#adf-control-input'));
typeWordIntoSearchInput('TEST');
@@ -428,9 +447,10 @@ describe('SearchControlComponent', () => {
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(document.activeElement.id).toBe('adf-control-input');
done();
});
});
}));
});
});
@@ -552,11 +572,12 @@ describe('SearchControlComponent', () => {
describe('option click', () => {
it('should emit a option clicked event when item is clicked', async(() => {
it('should emit a option clicked event when item is clicked', (done) => {
spyOn(component, 'isSearchBarActive').and.returnValue(true);
spyOn(searchService, 'search').and.returnValue(Observable.of(results));
searchServiceSpy.and.returnValue(Observable.of(results));
component.optionClicked.subscribe((item) => {
expect(item.entry.id).toBe('123');
done();
});
fixture.detectChanges();
typeWordIntoSearchInput('TEST');
@@ -564,17 +585,16 @@ describe('SearchControlComponent', () => {
fixture.whenStable().then(() => {
fixture.detectChanges();
let firstOption: DebugElement = debugElement.query(By.css('#result_name_0'));
firstOption.triggerEventHandler('click', null);
firstOption.nativeElement.click();
});
}));
});
it('should set deactivate the search after element is clicked', async(() => {
it('should set deactivate the search after element is clicked', (done) => {
spyOn(component, 'isSearchBarActive').and.returnValue(true);
spyOn(searchService, 'search').and.returnValue(Observable.of(results));
searchServiceSpy.and.returnValue(Observable.of(results));
component.optionClicked.subscribe((item) => {
window.setTimeout(() => {
expect(component.subscriptAnimationState).toBe('inactive');
}, 200);
expect(component.subscriptAnimationState).toBe('inactive');
done();
});
fixture.detectChanges();
@@ -583,16 +603,17 @@ describe('SearchControlComponent', () => {
fixture.whenStable().then(() => {
fixture.detectChanges();
let firstOption: DebugElement = debugElement.query(By.css('#result_name_0'));
firstOption.triggerEventHandler('click', null);
firstOption.nativeElement.click();
});
}));
});
it('should NOT reset the search term after element is clicked', async(() => {
it('should NOT reset the search term after element is clicked', (done) => {
spyOn(component, 'isSearchBarActive').and.returnValue(true);
spyOn(searchService, 'search').and.returnValue(Observable.of(results));
searchServiceSpy.and.returnValue(Observable.of(results));
component.optionClicked.subscribe((item) => {
expect(component.searchTerm).not.toBeFalsy();
expect(component.searchTerm).toBe('TEST');
done();
});
fixture.detectChanges();
typeWordIntoSearchInput('TEST');
@@ -601,9 +622,9 @@ describe('SearchControlComponent', () => {
fixture.whenStable().then(() => {
fixture.detectChanges();
let firstOption: DebugElement = debugElement.query(By.css('#result_name_0'));
firstOption.triggerEventHandler('click', null);
firstOption.nativeElement.click();
});
}));
});
});
describe('SearchControlComponent - No result custom', () => {
@@ -614,11 +635,11 @@ describe('SearchControlComponent', () => {
elementCustom = fixtureCustom.nativeElement;
});
it('should display the custom no results when it is configured', async(() => {
it('should display the custom no results when it is configured', (done) => {
const noResultCustomMessage = 'BANDI IS NOTHING';
spyOn(componentCustom.searchComponent, 'isSearchBarActive').and.returnValue(true);
componentCustom.setCustomMessageForNoResult(noResultCustomMessage);
spyOn(searchService, 'search').and.returnValue(Observable.of(noResult));
searchServiceSpy.and.returnValue(Observable.of(noResult));
fixtureCustom.detectChanges();
let inputDebugElement = fixtureCustom.debugElement.query(By.css('#adf-control-input'));
@@ -630,8 +651,9 @@ describe('SearchControlComponent', () => {
fixtureCustom.whenStable().then(() => {
fixtureCustom.detectChanges();
expect(elementCustom.querySelector('#custom-no-result').textContent).toBe(noResultCustomMessage);
done();
});
}));
});
});
});