|
1 |
| -import { TestBed } from '@angular/core/testing'; |
| 1 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
2 | 2 | import { ButtonComponent } from './button.component';
|
| 3 | +import { CommonModule } from '@angular/common'; |
3 | 4 |
|
4 | 5 | describe('ButtonComponent', () => {
|
| 6 | + let fixture: ComponentFixture<ButtonComponent>; |
| 7 | + let component: ButtonComponent; |
| 8 | + let buttonEl: HTMLButtonElement; |
| 9 | + |
5 | 10 | beforeEach(async () => {
|
6 | 11 | await TestBed.configureTestingModule({
|
7 |
| - imports: [ButtonComponent], |
| 12 | + imports: [CommonModule, ButtonComponent], |
8 | 13 | }).compileComponents();
|
| 14 | + |
| 15 | + fixture = TestBed.createComponent(ButtonComponent); |
| 16 | + component = fixture.componentInstance; |
| 17 | + buttonEl = fixture.nativeElement.querySelector('[data-testid="button"]'); |
9 | 18 | });
|
10 | 19 |
|
11 | 20 | it('should create component', () => {
|
12 |
| - const fixture = TestBed.createComponent(ButtonComponent); |
13 |
| - const component = fixture.componentInstance; |
14 |
| - |
15 | 21 | expect(component).toBeTruthy();
|
16 | 22 | });
|
17 | 23 |
|
18 | 24 | it('should apply default type "primary"', () => {
|
19 |
| - const fixture = TestBed.createComponent(ButtonComponent); |
20 |
| - const buttonEl = fixture.nativeElement.querySelector( |
21 |
| - '[data-testid="button"]' |
22 |
| - ); |
23 |
| - |
24 | 25 | fixture.detectChanges();
|
25 | 26 |
|
26 | 27 | expect(buttonEl.classList).toContain('primary');
|
27 | 28 | });
|
28 | 29 |
|
29 | 30 | it('should apply the correct type when type input is set', () => {
|
30 |
| - const fixture = TestBed.createComponent(ButtonComponent); |
31 | 31 | fixture.componentRef.setInput('type', 'danger');
|
32 | 32 |
|
33 | 33 | fixture.detectChanges();
|
34 | 34 |
|
35 |
| - const buttonEl = fixture.nativeElement.querySelector( |
36 |
| - '[data-testid="button"]' |
37 |
| - ); |
38 |
| - |
39 |
| - fixture.detectChanges(); |
40 |
| - |
41 | 35 | expect(buttonEl.classList).toContain('danger');
|
42 | 36 | expect(buttonEl.classList).not.toContain('primary');
|
43 | 37 | });
|
44 | 38 |
|
45 | 39 | it('should set the button as disabled when disabled is true', () => {
|
46 |
| - const fixture = TestBed.createComponent(ButtonComponent); |
47 | 40 | fixture.componentRef.setInput('disabled', true);
|
48 |
| - const button = fixture.nativeElement.querySelector( |
49 |
| - '[data-testid="button"]' |
50 |
| - ); |
51 | 41 |
|
52 | 42 | fixture.detectChanges();
|
53 | 43 |
|
54 |
| - expect(button.hasAttribute('disabled')).toBe(true); |
| 44 | + expect(buttonEl.hasAttribute('disabled')).toBe(true); |
55 | 45 | });
|
56 | 46 |
|
57 | 47 | it('should not disable the button when disabled is false', () => {
|
58 |
| - const fixture = TestBed.createComponent(ButtonComponent); |
59 | 48 | fixture.componentRef.setInput('disabled', false);
|
60 |
| - const button = fixture.nativeElement.querySelector( |
61 |
| - '[data-testid="button"]' |
62 |
| - ); |
63 | 49 |
|
64 | 50 | fixture.detectChanges();
|
65 | 51 |
|
66 |
| - expect(button.hasAttribute('disabled')).toBe(false); |
| 52 | + expect(buttonEl.hasAttribute('disabled')).toBe(false); |
67 | 53 | });
|
68 | 54 | });
|
0 commit comments