Skip to content

Commit 94eb729

Browse files
authored
Merge pull request trungvose#79 from darxx/Unit-testing
Unit testing
2 parents b18d733 + 796405a commit 94eb729

File tree

15 files changed

+241
-25
lines changed

15 files changed

+241
-25
lines changed

frontend/karma.conf.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,25 @@ module.exports = function (config) {
2525
colors: true,
2626
logLevel: config.LOG_INFO,
2727
autoWatch: true,
28-
browsers: ['Chrome'],
28+
browsers: ['ChromeHeadless'],
2929
singleRun: false,
30-
restartOnFileChange: true
30+
restartOnFileChange: true,
31+
customLaunchers: {
32+
HeadlessChrome: {
33+
base: 'ChromeHeadless',
34+
flags: [
35+
'--disable-web-security',
36+
'--disable-setuid-sandbox',
37+
'--no-sandbox',
38+
'--headless',
39+
'--disable-gpu',
40+
'--disable-translate',
41+
'--disable-extensions',
42+
'--proxy-server=\'direct://\'',
43+
'--proxy-bypass-list=*',
44+
'--blockDomainsExcept',
45+
]
46+
}
47+
},
3148
});
3249
};

frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"start": "ng serve",
77
"build": "ng build --prod=true --sourceMap=true",
88
"test": "ng test",
9+
"test-cover": "ng test --code-coverage --source-map=false",
910
"lint": "ng lint",
1011
"lint:fix": "ng lint --fix=true",
1112
"e2e": "ng e2e",
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {TestBed} from '@angular/core/testing';
2+
import {AppRoutingModule} from '@trungk18/app-routing.module';
3+
import {RouterTestingModule} from '@angular/router/testing';
4+
5+
describe('AppRoutingModule', () => {
6+
let module: AppRoutingModule;
7+
8+
beforeEach(() => {
9+
TestBed.configureTestingModule({
10+
imports: [AppRoutingModule, RouterTestingModule]
11+
});
12+
module = TestBed.inject(AppRoutingModule);
13+
});
14+
15+
it('should have App Routing Module', () => {
16+
expect(module).toBeTruthy();
17+
});
18+
});
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { AppComponent } from '@trungk18/app.component';
2+
import { NavigationEnd } from '@angular/router';
3+
import { environment } from '../environments/environment';
4+
5+
describe('AppComponent', () => {
6+
let component: AppComponent;
7+
8+
const router: any = {
9+
events: {
10+
subscribe: jasmine.createSpy('subscribe')
11+
}
12+
};
13+
const projectQuery: any = {};
14+
const changeDetectorRef: any = {
15+
detectChanges: jasmine.createSpy('detectChanges')
16+
};
17+
const projectService: any = {
18+
setLoading: jasmine.createSpy('setLoading').and.callThrough()
19+
};
20+
const googleAnalyticsService: any = {
21+
sendPageView: jasmine.createSpy('sendPageView').and.callThrough()
22+
};
23+
24+
beforeEach(() => {
25+
environment.production = true;
26+
component = new AppComponent(
27+
router,
28+
projectQuery,
29+
changeDetectorRef,
30+
projectService,
31+
googleAnalyticsService
32+
);
33+
});
34+
it('should be able to set Loading', () => {
35+
expect(router.events.subscribe).toHaveBeenCalled();
36+
expect(projectService.setLoading).toHaveBeenCalledWith(true);
37+
});
38+
it('should be able to make ng After View Init', () => {
39+
component.ngAfterViewInit();
40+
expect(changeDetectorRef.detectChanges).toHaveBeenCalled();
41+
});
42+
it('should be able to handle Google Analytics', () => {
43+
component.handleGoogleAnalytics( new NavigationEnd(1, '/', '/'));
44+
45+
expect(googleAnalyticsService.sendPageView).toHaveBeenCalled();
46+
});
47+
it('should not be able to handle Google Analytics', () => {
48+
googleAnalyticsService.sendPageView.calls.reset();
49+
component.handleGoogleAnalytics({ });
50+
51+
expect(googleAnalyticsService.sendPageView).not.toHaveBeenCalled();
52+
});
53+
54+
});

frontend/src/app/app.component.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,16 @@ export class AppComponent implements AfterViewInit {
2020
private _googleAnalytics: GoogleAnalyticsService
2121
) {
2222
this._projectService.setLoading(true);
23+
2324
if (environment.production) {
24-
this.handleGoogleAnalytics();
25+
this.router.events.subscribe(this.handleGoogleAnalytics);
2526
}
2627
}
2728

28-
handleGoogleAnalytics() {
29-
this.router.events.subscribe((event) => {
30-
if (event instanceof NavigationEnd) {
31-
this._googleAnalytics.sendPageView(event.urlAfterRedirects);
32-
}
33-
});
29+
handleGoogleAnalytics = (event: any): void => {
30+
if (event instanceof NavigationEnd) {
31+
this._googleAnalytics.sendPageView(event.urlAfterRedirects);
32+
}
3433
}
3534

3635
ngAfterViewInit() {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { AppModule } from '@trungk18/app.module';
2+
import { TestBed } from '@angular/core/testing';
3+
4+
describe('AppModule', () => {
5+
let module: AppModule;
6+
7+
beforeEach(() => {
8+
TestBed.configureTestingModule({
9+
imports: [AppModule]
10+
});
11+
module = TestBed.inject(AppModule);
12+
});
13+
14+
it('should have app module', () => {
15+
expect(module).toBeTruthy();
16+
});
17+
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {GoogleAnalyticsService} from '@trungk18/core/services/google-analytics.service';
2+
3+
describe('GoogleAnalyticsService', () => {
4+
let service: GoogleAnalyticsService;
5+
beforeEach(() => {
6+
service = new GoogleAnalyticsService();
7+
8+
});
9+
10+
it('should be able to sendEvent', () => {
11+
service.gtag = jasmine.createSpy('gtag').and.returnValue(true);
12+
service.sendEvent(
13+
'',
14+
'',
15+
'',
16+
null
17+
);
18+
expect(service.gtag).toHaveBeenCalled();
19+
});
20+
it('should not be able to sendEvent', () => {
21+
service.gtag = false;
22+
23+
expect(service.sendEvent(
24+
'',
25+
'',
26+
'',
27+
null
28+
)).toEqual();
29+
});
30+
it('should be able to sendPageView', () => {
31+
service.gtag = jasmine.createSpy('gtag').and.returnValue(true);
32+
service.sendPageView(
33+
''
34+
);
35+
expect(service.gtag).toHaveBeenCalled();
36+
});
37+
it('should not be able to sendPageView', () => {
38+
service.gtag = false;
39+
40+
expect(service.sendPageView(
41+
''
42+
)).toEqual();
43+
});
44+
});

frontend/src/app/core/services/google-analytics.service.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,33 @@ const GOOGLE_ANALYTICS_ID = 'UA-80363801-4';
55
providedIn: 'root'
66
})
77
export class GoogleAnalyticsService {
8+
gtag: any;
89
constructor() {
10+
if (typeof gtag !== 'undefined') {
11+
this.gtag = gtag;
12+
}
913
}
1014

11-
public sendEvent(
15+
public sendEvent = (
1216
eventName: string,
1317
eventCategory: string,
1418
eventLabel: string = null,
1519
eventValue: number = null
16-
) {
17-
if (!gtag) {
20+
) => {
21+
if (!this.gtag) {
1822
return;
1923
}
20-
gtag('event', eventName, {
24+
this.gtag('event', eventName, {
2125
event_category: eventCategory,
2226
event_label: eventLabel,
2327
value: eventValue
2428
});
2529
}
2630

2731
public sendPageView(url: string) {
28-
if (!gtag) {
32+
if (!this.gtag) {
2933
return;
3034
}
31-
gtag('config', GOOGLE_ANALYTICS_ID, { page_path: url });
35+
this.gtag('config', GOOGLE_ANALYTICS_ID, { page_path: url });
3236
}
3337
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { TestBed } from '@angular/core/testing';
2+
import { JiraControlModule } from '@trungk18/jira-control/jira-control.module';
3+
import { ReactiveFormsModule } from '@angular/forms';
4+
import { CommonModule } from '@angular/common';
5+
6+
describe('JiraControlModule', () => {
7+
let module: JiraControlModule;
8+
9+
beforeEach(() => {
10+
TestBed.configureTestingModule({
11+
imports: [
12+
CommonModule,
13+
JiraControlModule,
14+
ReactiveFormsModule
15+
]
16+
});
17+
module = TestBed.inject(JiraControlModule);
18+
});
19+
20+
it('should have Jira Control Module', () => {
21+
expect(module).toBeTruthy();
22+
});
23+
});

frontend/src/app/project/components/issues/issue-card/issue-card.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';
1+
import {Component, Input, OnChanges, OnInit, SimpleChanges} from '@angular/core';
22
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
33
import { JIssue } from '@trungk18/interface/issue';
44
import { IssuePriorityIcon } from '@trungk18/interface/issue-priority-icon';
@@ -14,7 +14,7 @@ import { IssueModalComponent } from '../issue-modal/issue-modal.component';
1414
styleUrls: ['./issue-card.component.scss']
1515
})
1616
@UntilDestroy()
17-
export class IssueCardComponent implements OnChanges {
17+
export class IssueCardComponent implements OnChanges, OnInit {
1818
@Input() issue: JIssue;
1919
assignees: JUser[];
2020
issueTypeIcon: string;

0 commit comments

Comments
 (0)