1 import { LOCALE_ID, TRANSLATIONS, TRANSLATIONS_FORMAT } from '@angular/core';
2 import { async, ComponentFixture, TestBed } from '@angular/core/testing';
3 import { AbstractControl } from '@angular/forms';
4 import { By } from '@angular/platform-browser';
6 import { I18n } from '@ngx-translate/i18n-polyfill';
7 import * as _ from 'lodash';
9 import { TableActionsComponent } from '../app/shared/datatable/table-actions/table-actions.component';
10 import { CdFormGroup } from '../app/shared/forms/cd-form-group';
11 import { Permission } from '../app/shared/models/permissions';
14 PrometheusNotification,
15 PrometheusNotificationAlert
16 } from '../app/shared/models/prometheus-alerts';
17 import { _DEV_ } from '../unit-test-configuration';
19 export function configureTestBed(configuration, useOldMethod?) {
20 if (_DEV_ && !useOldMethod) {
21 const resetTestingModule = TestBed.resetTestingModule;
24 TestBed.resetTestingModule();
25 TestBed.configureTestingModule(configuration);
26 // prevent Angular from resetting testing module
27 TestBed.resetTestingModule = () => TestBed;
33 TestBed.resetTestingModule = resetTestingModule;
36 beforeEach(async(() => {
37 TestBed.configureTestingModule(configuration);
42 export class PermissionHelper {
43 tableActions: TableActionsComponent;
44 permission: Permission;
45 getTableActionComponent: () => TableActionsComponent;
47 constructor(permission: Permission, getTableActionComponent: () => TableActionsComponent) {
48 this.permission = permission;
49 this.getTableActionComponent = getTableActionComponent;
52 setPermissionsAndGetActions(
53 createPerm: number | boolean,
54 updatePerm: number | boolean,
55 deletePerm: number | boolean
56 ): TableActionsComponent {
57 this.permission.create = Boolean(createPerm);
58 this.permission.update = Boolean(updatePerm);
59 this.permission.delete = Boolean(deletePerm);
60 this.tableActions = this.getTableActionComponent();
61 return this.tableActions;
74 singleExecuting?: any; // uses 'single' if not defined
75 multiple?: any; // uses 'empty' if not defined
78 // 'multiple selections'
81 _.isUndefined(multiple) ? empty : multiple
84 // 'select executing item'
85 [{ cdExecuting: 'someAction' }],
87 _.isUndefined(singleExecuting) ? single : singleExecuting
89 this.testScenario([{}], fn, single); // 'select non-executing item'
90 this.testScenario([], fn, empty); // 'no selection'
93 private testScenario(selection: object[], fn: () => any, expected: any) {
94 this.setSelection(selection);
95 expect(fn()).toBe(expected);
98 setSelection(selection: object[]) {
99 this.tableActions.selection.selected = selection;
100 this.tableActions.selection.update();
104 export class FormHelper {
107 constructor(form: CdFormGroup) {
112 * Changes multiple values in multiple controls
114 setMultipleValues(values: { [controlName: string]: any }, markAsDirty?: boolean) {
115 Object.keys(values).forEach((key) => {
116 this.setValue(key, values[key], markAsDirty);
121 * Changes the value of a control
123 setValue(control: AbstractControl | string, value: any, markAsDirty?: boolean): AbstractControl {
124 control = this.getControl(control);
126 control.markAsDirty();
128 control.setValue(value);
132 private getControl(control: AbstractControl | string): AbstractControl {
133 if (typeof control === 'string') {
134 return this.form.get(control);
140 * Change the value of the control and expect the control to be valid afterwards.
142 expectValidChange(control: AbstractControl | string, value: any, markAsDirty?: boolean) {
143 this.expectValid(this.setValue(control, value, markAsDirty));
147 * Expect that the given control is valid.
149 expectValid(control: AbstractControl | string) {
150 // 'isValid' would be false for disabled controls
151 expect(this.getControl(control).errors).toBe(null);
155 * Change the value of the control and expect a specific error.
158 control: AbstractControl | string,
161 markAsDirty?: boolean
163 this.expectError(this.setValue(control, value, markAsDirty), error);
167 * Expect a specific error for the given control.
169 expectError(control: AbstractControl | string, error: string) {
170 expect(this.getControl(control).hasError(error)).toBeTruthy();
174 export class FixtureHelper {
175 fixture: ComponentFixture<any>;
177 constructor(fixture: ComponentFixture<any>) {
178 this.fixture = fixture;
182 * Expect a list of id elements to be visible or not.
184 expectIdElementsVisible(ids: string[], visibility: boolean) {
185 ids.forEach((css) => {
186 this.expectElementVisible(`#${css}`, visibility);
191 * Expect a specific element to be visible or not.
193 expectElementVisible(css: string, visibility: boolean) {
194 expect(Boolean(this.getElementByCss(css))).toBe(visibility);
197 expectFormFieldToBe(css: string, value: string) {
198 const props = this.getElementByCss(css).properties;
199 expect(props['value'] || props['checked'].toString()).toBe(value);
202 clickElement(css: string) {
203 this.getElementByCss(css).triggerEventHandler('click', null);
204 this.fixture.detectChanges();
207 getText(css: string) {
208 const e = this.getElementByCss(css);
209 return e ? e.nativeElement.textContent.trim() : null;
212 getElementByCss(css: string) {
213 this.fixture.detectChanges();
214 return this.fixture.debugElement.query(By.css(css));
218 export class PrometheusHelper {
219 createAlert(name, state = 'active', timeMultiplier = 1) {
227 summary: `${name} is ${state}`
229 generatorURL: `http://${name}`,
230 startsAt: new Date(new Date('2022-02-22').getTime() * timeMultiplier).toString()
231 } as PrometheusAlert;
234 createNotificationAlert(name, status = 'firing') {
241 summary: `${name} is ${status}`
243 generatorURL: `http://${name}`
244 } as PrometheusNotificationAlert;
247 createNotification(alertNumber = 1, status = 'firing') {
249 for (let i = 0; i < alertNumber; i++) {
250 alerts.push(this.createNotificationAlert('alert' + i, status));
252 return { alerts, status } as PrometheusNotification;
256 return `<a href="${url}" target="_blank"><i class="fa fa-line-chart"></i></a>`;
260 const XLIFF = `<?xml version="1.0" encoding="UTF-8" ?>
261 <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
262 <file source-language="en" datatype="plaintext" original="ng2.template">
269 const i18nProviders = [
270 { provide: TRANSLATIONS_FORMAT, useValue: 'xlf' },
271 { provide: TRANSLATIONS, useValue: XLIFF },
272 { provide: LOCALE_ID, useValue: 'en' },
276 export { i18nProviders };