import { RouterTestingModule } from '@angular/router/testing';
import { NgbActiveModal, NgbTypeaheadModule } from '@ng-bootstrap/ng-bootstrap';
+import * as _ from 'lodash';
+import { of } from 'rxjs';
import {
configureTestBed,
});
component.onSubmit();
});
+
+ describe('typeahead', () => {
+ let equality: { [key: string]: boolean };
+ let expectations: { [key: string]: string[] };
+
+ const search = (s: string) => {
+ Object.keys(expectations).forEach((key) => {
+ formH.setValue('name', key);
+ component.search(of(s)).subscribe((result) => {
+ // Expect won't fail the test inside subscribe
+ equality[key] = _.isEqual(result, expectations[key]);
+ });
+ expect(equality[key]).toBeTruthy();
+ });
+ };
+
+ beforeEach(() => {
+ equality = {
+ alertname: false,
+ instance: false,
+ job: false,
+ severity: false
+ };
+ expectations = {
+ alertname: ['alert0', 'alert1'],
+ instance: ['someInstance'],
+ job: ['someJob'],
+ severity: ['someSeverity']
+ };
+ });
+
+ it('should show all values on name switch', () => {
+ search('');
+ });
+
+ it('should search for "some"', () => {
+ expectations['alertname'] = [];
+ search('some');
+ });
+
+ it('should search for "er"', () => {
+ expectations['instance'] = [];
+ expectations['job'] = [];
+ search('er');
+ });
+ });
});
-import { Component, EventEmitter, Output } from '@angular/core';
+import { Component, EventEmitter, Output, ViewChild } from '@angular/core';
import { FormControl, Validators } from '@angular/forms';
-import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
+import { NgbActiveModal, NgbTypeahead } from '@ng-bootstrap/ng-bootstrap';
import * as _ from 'lodash';
-import { Observable } from 'rxjs';
-import { debounceTime, distinctUntilChanged, map } from 'rxjs/operators';
+import { merge, Observable, Subject } from 'rxjs';
+import { debounceTime, distinctUntilChanged, filter, map } from 'rxjs/operators';
import { CdFormBuilder } from '../../../../shared/forms/cd-form-builder';
import { CdFormGroup } from '../../../../shared/forms/cd-form-group';
styleUrls: ['./silence-matcher-modal.component.scss']
})
export class SilenceMatcherModalComponent {
+ @ViewChild(NgbTypeahead, { static: true })
+ typeahead: NgbTypeahead;
@Output()
submitAction = new EventEmitter();
possibleValues: string[] = [];
matcherMatch: AlertmanagerSilenceMatcherMatch = undefined;
+ // For typeahead usage
+ valueClick = new Subject<string>();
+ valueFocus = new Subject<string>();
+ search = (text$: Observable<string>) => {
+ return merge(
+ text$.pipe(debounceTime(200), distinctUntilChanged()),
+ this.valueFocus,
+ this.valueClick.pipe(filter(() => !this.typeahead.isPopupOpen()))
+ ).pipe(
+ map((term) =>
+ (term === ''
+ ? this.possibleValues
+ : this.possibleValues.filter((v) => v.toLowerCase().indexOf(term.toLowerCase()) > -1)
+ ).slice(0, 10)
+ )
+ );
+ };
+
constructor(
private formBuilder: CdFormBuilder,
private silenceMatcher: PrometheusSilenceMatcherService,
private createForm() {
this.form = this.formBuilder.group({
name: [null, [Validators.required]],
- value: [{ value: null, disabled: true }, [Validators.required]],
+ value: [{ value: '', disabled: true }, [Validators.required]],
isRegex: new FormControl(false)
});
}
this.submitAction.emit(this.form.value);
this.activeModal.close();
}
-
- search = (text$: Observable<string>) => {
- return text$.pipe(
- debounceTime(200),
- distinctUntilChanged(),
- map((term) =>
- this.possibleValues
- .filter((v) => v.toLowerCase().indexOf(term.toLowerCase()) > -1)
- .slice(0, 10)
- )
- );
- };
}