Skip to content

Commit 3463186

Browse files
committed
test fixes
1 parent 01cad6f commit 3463186

File tree

4 files changed

+21
-28
lines changed

4 files changed

+21
-28
lines changed

jest-global-mocks.ts

Whitespace-only changes.

jest.config.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
module.exports = {
22
preset: 'jest-preset-angular',
3-
globalSetup: 'jest-preset-angular/global-setup',
3+
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
4+
moduleDirectories: ['node_modules', '<rootDir>/src'],
5+
transformIgnorePatterns: ['node_modules/(?!@angular|rjxs|@ngrx)'],
6+
collectCoverage: true,
7+
coverageDirectory: '<rootDir>/coverage',
48
};

setup-jest.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// uncomment for jest-runner extension to work properly
2+
// import 'jest-preset-angular/setup-jest';
3+
import './jest-global-mocks';
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,54 @@
1-
import { TestBed } from '@angular/core/testing';
1+
import { ComponentFixture, TestBed } from '@angular/core/testing';
22
import { ButtonComponent } from './button.component';
3+
import { CommonModule } from '@angular/common';
34

45
describe('ButtonComponent', () => {
6+
let fixture: ComponentFixture<ButtonComponent>;
7+
let component: ButtonComponent;
8+
let buttonEl: HTMLButtonElement;
9+
510
beforeEach(async () => {
611
await TestBed.configureTestingModule({
7-
imports: [ButtonComponent],
12+
imports: [CommonModule, ButtonComponent],
813
}).compileComponents();
14+
15+
fixture = TestBed.createComponent(ButtonComponent);
16+
component = fixture.componentInstance;
17+
buttonEl = fixture.nativeElement.querySelector('[data-testid="button"]');
918
});
1019

1120
it('should create component', () => {
12-
const fixture = TestBed.createComponent(ButtonComponent);
13-
const component = fixture.componentInstance;
14-
1521
expect(component).toBeTruthy();
1622
});
1723

1824
it('should apply default type "primary"', () => {
19-
const fixture = TestBed.createComponent(ButtonComponent);
20-
const buttonEl = fixture.nativeElement.querySelector(
21-
'[data-testid="button"]'
22-
);
23-
2425
fixture.detectChanges();
2526

2627
expect(buttonEl.classList).toContain('primary');
2728
});
2829

2930
it('should apply the correct type when type input is set', () => {
30-
const fixture = TestBed.createComponent(ButtonComponent);
3131
fixture.componentRef.setInput('type', 'danger');
3232

3333
fixture.detectChanges();
3434

35-
const buttonEl = fixture.nativeElement.querySelector(
36-
'[data-testid="button"]'
37-
);
38-
39-
fixture.detectChanges();
40-
4135
expect(buttonEl.classList).toContain('danger');
4236
expect(buttonEl.classList).not.toContain('primary');
4337
});
4438

4539
it('should set the button as disabled when disabled is true', () => {
46-
const fixture = TestBed.createComponent(ButtonComponent);
4740
fixture.componentRef.setInput('disabled', true);
48-
const button = fixture.nativeElement.querySelector(
49-
'[data-testid="button"]'
50-
);
5141

5242
fixture.detectChanges();
5343

54-
expect(button.hasAttribute('disabled')).toBe(true);
44+
expect(buttonEl.hasAttribute('disabled')).toBe(true);
5545
});
5646

5747
it('should not disable the button when disabled is false', () => {
58-
const fixture = TestBed.createComponent(ButtonComponent);
5948
fixture.componentRef.setInput('disabled', false);
60-
const button = fixture.nativeElement.querySelector(
61-
'[data-testid="button"]'
62-
);
6349

6450
fixture.detectChanges();
6551

66-
expect(button.hasAttribute('disabled')).toBe(false);
52+
expect(buttonEl.hasAttribute('disabled')).toBe(false);
6753
});
6854
});

0 commit comments

Comments
 (0)