Skip to content

Commit d78d8df

Browse files
slavmanvalorkin
authored andcommitted
feat(tests): add alert spec (#1336)
1 parent ec524fe commit d78d8df

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

src/spec/ng-bootstrap/alert.spec.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/* tslint:disable:max-classes-per-file max-file-line-count component-class-suffix */
2+
/**
3+
* @copyright Angular ng-bootstrap team
4+
*/
5+
import { TestBed, ComponentFixture, inject } from '@angular/core/testing';
6+
import { createGenericTestComponent } from './test/common';
7+
import { Component } from '@angular/core';
8+
import { AlertModule, AlertComponent, AlertConfig } from '../../alert';
9+
10+
const createTestComponent = (html: string) =>
11+
createGenericTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;
12+
13+
function getAlertElement(element: HTMLElement): HTMLDivElement {
14+
return element.querySelector('.alert') as HTMLDivElement;
15+
}
16+
17+
function getCloseButton(element: HTMLElement): HTMLButtonElement {
18+
return element.querySelector('button') as HTMLButtonElement;
19+
}
20+
21+
describe('ngb-alert', () => {
22+
23+
beforeEach(
24+
() => { TestBed.configureTestingModule({declarations: [TestComponent], imports: [AlertModule.forRoot()]}); });
25+
26+
it('should initialize inputs with default values', () => {
27+
const defaultConfig = new AlertConfig();
28+
const alertCmp = new AlertComponent(new AlertConfig());
29+
30+
expect(alertCmp.dismissible).toBe(defaultConfig.dismissible);
31+
expect(alertCmp.type).toBe(defaultConfig.type);
32+
});
33+
34+
it('should allow specifying alert type', () => {
35+
const fixture = createTestComponent('<alert type="success">Cool!</alert>');
36+
const alertEl = getAlertElement(fixture.nativeElement);
37+
38+
expect(alertEl.getAttribute('role')).toEqual('alert');
39+
expect(alertEl).toHaveCssClass('alert-success');
40+
});
41+
42+
it('should render close button when dismissible', () => {
43+
const fixture = createTestComponent('<alert [dismissible]="true">Watch out!</alert>');
44+
45+
expect(getCloseButton(getAlertElement(fixture.nativeElement))).toBeTruthy();
46+
});
47+
48+
it('should render close button only if dismissible', () => {
49+
const fixture = createTestComponent(`<alert [dismissible]="false">Don't close!</alert>`);
50+
expect(getCloseButton(getAlertElement(fixture.nativeElement))).toBeFalsy();
51+
});
52+
53+
describe('Custom config', () => {
54+
let config: AlertConfig;
55+
56+
beforeEach(() => { TestBed.configureTestingModule({imports: [AlertModule.forRoot()]}); });
57+
58+
beforeEach(inject([AlertConfig], (c: AlertConfig) => {
59+
config = c;
60+
config.dismissible = false;
61+
config.type = 'success';
62+
}));
63+
64+
it('should initialize inputs with provided config', () => {
65+
const fixture = TestBed.createComponent(AlertComponent);
66+
fixture.detectChanges();
67+
68+
const alert = fixture.componentInstance;
69+
expect(alert.dismissible).toBe(config.dismissible);
70+
expect(alert.type).toBe(config.type);
71+
});
72+
});
73+
74+
describe('Custom config as provider', () => {
75+
let config = new AlertConfig();
76+
config.dismissible = false;
77+
config.type = 'success';
78+
79+
beforeEach(() => {
80+
TestBed.configureTestingModule(
81+
{imports: [AlertModule.forRoot()], providers: [{provide: AlertConfig, useValue: config}]});
82+
});
83+
84+
it('should initialize inputs with provided config as provider', () => {
85+
const fixture = TestBed.createComponent(AlertComponent);
86+
fixture.detectChanges();
87+
88+
const alert = fixture.componentInstance;
89+
expect(alert.dismissible).toBe(config.dismissible);
90+
expect(alert.type).toBe(config.type);
91+
});
92+
});
93+
});
94+
95+
@Component({selector: 'test-cmp', template: '', entryComponents: [AlertComponent]})
96+
class TestComponent {
97+
public name: string = 'World';
98+
public closed: boolean = false;
99+
}

0 commit comments

Comments
 (0)