Skip to content

Commit 617fb29

Browse files
committed
Fixes unit test for UsageComponent
1 parent 388fec1 commit 617fb29

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<app-top-header section="Usage"></app-top-header>
22
<app-readme-to-html
3-
class="usage-{{ page }}"
4-
MDFile="./assets/Markdown Files/{{ page }}.md">
3+
class="usage-{{ page }}"
4+
MDFile="./assets/Markdown Files/{{ page }}.md">
55
</app-readme-to-html>

src/app/component/usage/usage.component.spec.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { ComponentFixture, TestBed } from '@angular/core/testing';
22

33
import { UsageComponent } from './usage.component';
4+
import { ActivatedRoute } from '@angular/router';
5+
import { of } from 'rxjs';
46

57
describe('UsageComponent', () => {
68
let component: UsageComponent;
@@ -12,13 +14,28 @@ describe('UsageComponent', () => {
1214
}).compileComponents();
1315
});
1416

15-
beforeEach(() => {
17+
it('should create', () => {
18+
TestBed.overrideProvider(ActivatedRoute, {
19+
useValue: { params: of({}) },
20+
});
21+
1622
fixture = TestBed.createComponent(UsageComponent);
1723
component = fixture.componentInstance;
1824
fixture.detectChanges();
19-
});
2025

21-
it('should create', () => {
2226
expect(component).toBeTruthy();
27+
expect(component.page).toBe('USAGE');
28+
});
29+
30+
it('should load page', () => {
31+
TestBed.overrideProvider(ActivatedRoute, {
32+
useValue: { params: of({ page: 'test-page' }) },
33+
});
34+
35+
fixture = TestBed.createComponent(UsageComponent);
36+
component = fixture.componentInstance;
37+
fixture.detectChanges();
38+
39+
expect(component.page).toBe('test-page');
2340
});
2441
});

src/app/component/usage/usage.component.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ export class UsageComponent implements OnInit {
1111
constructor(private route: ActivatedRoute) {}
1212

1313
ngOnInit() {
14-
this.route.params.subscribe(params => {
15-
let page = params['page'];
16-
if (page.match(/^[\w.-]+$/)) {
17-
this.page = page;
18-
}
19-
});
14+
if (this.route && this.route.params) {
15+
this.route.params.subscribe(params => {
16+
let page = params['page'];
17+
// CWE-79 - sanitize input
18+
if (page.match(/^[\w.-]+$/)) {
19+
this.page = page;
20+
}
21+
});
22+
}
2023
}
2124
}

0 commit comments

Comments
 (0)