1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { ReactiveFormsModule } from '@angular/forms';
4 import { RouterTestingModule } from '@angular/router/testing';
6 import { NgbActiveModal, NgbTypeaheadModule } from '@ng-bootstrap/ng-bootstrap';
7 import * as _ from 'lodash';
8 import { of } from 'rxjs';
16 } from '../../../../../testing/unit-test-helper';
17 import { SharedModule } from '../../../../shared/shared.module';
18 import { SilenceMatcherModalComponent } from './silence-matcher-modal.component';
20 describe('SilenceMatcherModalComponent', () => {
21 let component: SilenceMatcherModalComponent;
22 let fixture: ComponentFixture<SilenceMatcherModalComponent>;
24 let formH: FormHelper;
25 let fixtureH: FixtureHelper;
26 let prometheus: PrometheusHelper;
29 declarations: [SilenceMatcherModalComponent],
31 HttpClientTestingModule,
37 providers: [NgbActiveModal, i18nProviders]
41 fixture = TestBed.createComponent(SilenceMatcherModalComponent);
42 component = fixture.componentInstance;
44 fixtureH = new FixtureHelper(fixture);
45 formH = new FormHelper(component.form);
46 prometheus = new PrometheusHelper();
49 prometheus.createRule('alert0', 'someSeverity', [prometheus.createAlert('alert0')]),
50 prometheus.createRule('alert1', 'someSeverity', [])
52 fixture.detectChanges();
55 it('should create', () => {
56 expect(component).toBeTruthy();
59 it('should have a name field', () => {
60 formH.expectError('name', 'required');
61 formH.expectValidChange('name', 'alertname');
64 it('should only allow a specific set of name attributes', () => {
65 expect(component.nameAttributes).toEqual(['alertname', 'instance', 'job', 'severity']);
68 it('should autocomplete a list based on the set name', () => {
69 const expectations = {
70 alertname: ['alert0', 'alert1'],
71 instance: ['someInstance'],
73 severity: ['someSeverity']
75 Object.keys(expectations).forEach((key) => {
76 formH.setValue('name', key);
77 expect(component.possibleValues).toEqual(expectations[key]);
81 describe('test rule matching', () => {
82 const expectMatch = (name: string, value: string, helpText: string) => {
83 component.preFillControls({
88 expect(fixtureH.getText('#match-state')).toBe(helpText);
91 it('should match no rule and no alert', () => {
95 'Your matcher seems to match no currently defined rule or active alert.'
99 it('should match a rule with no alert', () => {
100 expectMatch('alertname', 'alert1', 'Matches 1 rule with no active alerts.');
103 it('should match a rule and an alert', () => {
104 expectMatch('alertname', 'alert0', 'Matches 1 rule with 1 active alert.');
107 it('should match multiple rules and an alert', () => {
108 expectMatch('severity', 'someSeverity', 'Matches 2 rules with 1 active alert.');
111 it('should match multiple rules and multiple alerts', () => {
112 component.rules[1].alerts.push(null);
113 expectMatch('severity', 'someSeverity', 'Matches 2 rules with 2 active alerts.');
116 it('should not show match-state if regex is checked', () => {
117 fixtureH.expectElementVisible('#match-state', false);
118 formH.setValue('name', 'severity');
119 formH.setValue('value', 'someSeverity');
120 fixtureH.expectElementVisible('#match-state', true);
121 formH.setValue('isRegex', true);
122 fixtureH.expectElementVisible('#match-state', false);
126 it('should only enable value field if name was set', () => {
127 const value = component.form.get('value');
128 expect(value.disabled).toBeTruthy();
129 formH.setValue('name', component.nameAttributes[0]);
130 expect(value.enabled).toBeTruthy();
131 formH.setValue('name', null);
132 expect(value.disabled).toBeTruthy();
135 it('should have a value field', () => {
136 formH.setValue('name', component.nameAttributes[0]);
137 formH.expectError('value', 'required');
138 formH.expectValidChange('value', 'alert0');
141 it('should test preFillControls', () => {
142 const controlValues = {
147 component.preFillControls(controlValues);
148 expect(component.form.value).toEqual(controlValues);
151 it('should test submit', (done) => {
152 const controlValues = {
157 component.preFillControls(controlValues);
158 component.submitAction.subscribe((resp: object) => {
159 expect(resp).toEqual(controlValues);
162 component.onSubmit();
165 describe('typeahead', () => {
166 let equality: { [key: string]: boolean };
167 let expectations: { [key: string]: string[] };
169 const search = (s: string) => {
170 Object.keys(expectations).forEach((key) => {
171 formH.setValue('name', key);
172 component.search(of(s)).subscribe((result) => {
173 // Expect won't fail the test inside subscribe
174 equality[key] = _.isEqual(result, expectations[key]);
176 expect(equality[key]).toBeTruthy();
188 alertname: ['alert0', 'alert1'],
189 instance: ['someInstance'],
191 severity: ['someSeverity']
195 it('should show all values on name switch', () => {
199 it('should search for "some"', () => {
200 expectations['alertname'] = [];
204 it('should search for "er"', () => {
205 expectations['instance'] = [];
206 expectations['job'] = [];