Skip to content

Commit dc3a797

Browse files
authored
Merge pull request trungvose#80 from darxx/Unit-tests-after-Angular-11
Unit tests after angular 11
2 parents f2ad283 + ea90637 commit dc3a797

38 files changed

+368
-98
lines changed

frontend/karma.conf.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ module.exports = function (config) {
1616
clearContext: false // leave Jasmine Spec Runner output visible in browser
1717
},
1818
coverageReporter: {
19-
dir: require('path').join(__dirname, './coverage/frontend'),
19+
dir: require('path').join(__dirname, './coverage'),
2020
reports: ['html', 'lcovonly', 'text-summary'],
21-
fixWebpackSourcePaths: true
21+
fixWebpackSourcePaths: true,
22+
subdir: 'frontend',
23+
type: 'html'
2224
},
2325
reporters: ['progress', 'kjhtml'],
2426
port: 9876,
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { AutofocusDirective } from '@trungk18/core/directives/autofocus.directive';
2+
import { fakeAsync, tick } from '@angular/core/testing';
3+
4+
describe('AutofocusDirective', () => {
5+
let component: any;
6+
const elementRef: any = {
7+
nativeElement: {
8+
focus: jasmine.createSpy('nativeElement')
9+
}
10+
};
11+
12+
beforeEach(() => {
13+
component = new AutofocusDirective(
14+
elementRef
15+
);
16+
});
17+
18+
it('should be able to make ng After Content Init', fakeAsync(() => {
19+
component.ngAfterContentInit();
20+
tick(10);
21+
expect(component.enable).toBe(true);
22+
expect(elementRef.nativeElement.focus).toHaveBeenCalled();
23+
}));
24+
it('should be able to make ng After Content Init and destroy', fakeAsync(() => {
25+
component.ngAfterContentInit();
26+
tick(10);
27+
component.ngOnDestroy();
28+
expect(component.timer).toEqual(null);
29+
}));
30+
});

frontend/src/app/core/directives/autofocus.directive.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@ export class AutofocusDirective implements AfterContentInit, OnDestroy {
1717
this.timer = null;
1818
}
1919

20-
setDefaultValue() {
21-
if (this.enable === false) {
22-
return;
23-
}
24-
this.enable = true;
25-
}
26-
2720
public ngAfterContentInit(): void {
2821
this.setDefaultValue();
2922
if (this.enable) {
@@ -35,6 +28,13 @@ export class AutofocusDirective implements AfterContentInit, OnDestroy {
3528
this.stopFocusWorkflow();
3629
}
3730

31+
private setDefaultValue() {
32+
if (this.enable === false) {
33+
return;
34+
}
35+
this.enable = true;
36+
}
37+
3838
private startFocusWorkflow(): void {
3939
if (this.timer) {
4040
return;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { NoWhitespaceValidator } from './no-whitespace.validator';
2+
import { FormControl } from '@angular/forms';
3+
4+
describe('NoWhitespaceValidator', () => {
5+
const validator = NoWhitespaceValidator();
6+
7+
it('should be able to return no whitespace validator null value', () => {
8+
const formControl = new FormControl();
9+
formControl.setValue('Test');
10+
11+
expect(validator(formControl)).toEqual(null);
12+
});
13+
it('should be able to return no whitespace validator whitespace value', () => {
14+
const formControl = new FormControl();
15+
formControl.setValue('');
16+
17+
expect(validator(formControl).whitespace).toEqual('value is only whitespace');
18+
});
19+
it('should be able to return no whitespace validator on number validation', () => {
20+
const formControl = new FormControl();
21+
formControl.setValue(5);
22+
23+
expect(validator(formControl)).toEqual(null);
24+
});
25+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { JComment } from '@trungk18/interface/comment';
2+
3+
describe('JComment', () => {
4+
let testClass: any;
5+
6+
beforeEach(() => {
7+
testClass = new JComment(
8+
'',
9+
{
10+
id: '',
11+
name: '',
12+
email: '',
13+
avatarUrl: '',
14+
createdAt: '',
15+
updatedAt: '',
16+
issueIds: ['']
17+
}
18+
);
19+
});
20+
21+
it('Should have interfaces ', () => {
22+
expect(Object.keys(testClass).length).toEqual(5);
23+
expect(Object.keys(testClass.user).length).toEqual(7);
24+
});
25+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {AvatarComponent} from '@trungk18/jira-control/avatar/avatar.component';
2+
3+
describe('AvatarComponent', () => {
4+
let component: AvatarComponent;
5+
beforeEach(() => {
6+
component = new AvatarComponent();
7+
});
8+
9+
it('should be able to get styles', () => {
10+
expect(component.style.width).toEqual('12px');
11+
expect(component.style.height).toEqual('12px');
12+
});
13+
});
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import { Component, OnInit, Input } from '@angular/core';
1+
import { Component, Input } from '@angular/core';
22

33
@Component({
44
selector: 'breadcrumbs',
55
templateUrl: './breadcrumbs.component.html',
66
styleUrls: ['./breadcrumbs.component.scss']
77
})
8-
export class BreadcrumbsComponent implements OnInit {
8+
export class BreadcrumbsComponent {
99
@Input() items: string[] = [];
1010
constructor() {}
1111

12-
ngOnInit(): void {}
1312
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { ButtonComponent } from '@trungk18/jira-control/button/button.component';
2+
3+
describe('ButtonComponent', () => {
4+
let component: ButtonComponent;
5+
6+
beforeEach(() => {
7+
component = new ButtonComponent();
8+
});
9+
10+
it('should be able to create', () => {
11+
expect(component).toBeTruthy();
12+
});
13+
});
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { Component, Input, OnInit } from '@angular/core';
1+
import { Component, Input } from '@angular/core';
22

33
@Component({
44
selector: 'j-button',
55
templateUrl: './button.component.html',
66
styleUrls: ['./button.component.scss']
77
})
8-
export class ButtonComponent implements OnInit {
8+
export class ButtonComponent {
99
@Input() type = 'button';
1010
@Input() className = 'btn-primary';
1111
@Input() icon: string;
@@ -16,6 +16,4 @@ export class ButtonComponent implements OnInit {
1616

1717
constructor() {}
1818

19-
ngOnInit(): void {}
20-
2119
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { InputComponent } from '@trungk18/jira-control/input/input.component';
2+
3+
describe('InputComponent', () => {
4+
let component: InputComponent;
5+
6+
beforeEach(() => {
7+
component = new InputComponent();
8+
});
9+
10+
it('should be able to init', () => {
11+
component.ngOnInit();
12+
expect(component.control).toBeTruthy();
13+
});
14+
it('should be able to get icon size', () => {
15+
expect(component.iconContainerWidth).toEqual(32);
16+
});
17+
it('should be able to get return is Show Clear Button', () => {
18+
expect(typeof component.isShowClearButton).toEqual('undefined');
19+
});
20+
it('should be able to clear control', () => {
21+
component.ngOnInit();
22+
spyOn(component.control, 'patchValue').and.callThrough();
23+
component.clear();
24+
expect(component.control.patchValue).toHaveBeenCalled();
25+
});
26+
});

0 commit comments

Comments
 (0)