Skip to content

Commit 1d0c4be

Browse files
patrick.flege@rabobank.nlpatrick.flege@rabobank.nl
patrick.flege@rabobank.nl
authored and
patrick.flege@rabobank.nl
committed
Finished project
1 parent fea4ccc commit 1d0c4be

11 files changed

+274
-495
lines changed

cypress/integration/home.test.js

+11-64
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,11 @@
1-
2-
3-
4-
describe('Home Page', () => {
5-
6-
beforeEach(() => {
7-
8-
cy.fixture('courses.json').as("coursesJSON");
9-
10-
cy.server();
11-
12-
cy.route('/api/courses', "@coursesJSON").as("courses");
13-
14-
cy.visit('/');
15-
16-
});
17-
18-
it('should display a list of courses', () => {
19-
20-
cy.contains("All Courses");
21-
22-
cy.wait('@courses');
23-
24-
cy.get("mat-card").should("have.length", 9);
25-
26-
});
27-
28-
it('should display the advanced courses', () => {
29-
30-
cy.get('.mat-tab-label').should("have.length", 2);
31-
32-
cy.get('.mat-tab-label').last().click();
33-
34-
cy.get('.mat-tab-body-active .mat-card-title').its('length').should('be.gt', 1);
35-
36-
cy.get('.mat-tab-body-active .mat-card-title').first()
37-
.should('contain', "Angular Security Course");
38-
39-
});
40-
41-
42-
});
43-
44-
45-
46-
47-
48-
49-
50-
51-
52-
53-
54-
55-
56-
57-
58-
59-
60-
61-
62-
63-
64-
1+
describe("Homepage", () => {
2+
it("should display all courses", () => {
3+
//cy.fixture('courses.json').as('courses');
4+
cy.intercept('GET', 'localhost:9000/api/courses',{
5+
fixture: 'courses.json'
6+
})
7+
cy.visit('/');
8+
cy.contains('All Courses')
9+
//cy.get('mat-card').should('have.length', 9)
10+
})
11+
})

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
"lint": "ng lint",
1212
"cypress:open": "cypress open",
1313
"cypress:run": "cypress run",
14-
"build:prod": "ng build --prod",
14+
"build:prod": "ng build --optimization",
1515
"start:prod": "http-server ./dist -a localhost -p 4200",
1616
"build-and-start:prod": "run-s build:prod start:prod",
1717
"e2e": "start-server-and-test build-and-start:prod http://localhost:4200 cypress:run"
18-
},
18+
19+
},
1920
"private": true,
2021
"dependencies": {
2122
"@angular/animations": "15.0.2",
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,61 @@
1-
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
1+
import {ComponentFixture, TestBed, waitForAsync} from '@angular/core/testing';
22
import {CoursesCardListComponent} from './courses-card-list.component';
3-
import {CoursesModule} from '../courses.module';
4-
import {COURSES} from '../../../../server/db-data';
5-
import {DebugElement} from '@angular/core';
6-
import {By} from '@angular/platform-browser';
7-
import {sortCoursesBySeqNo} from '../home/sort-course-by-seq';
8-
import {Course} from '../model/course';
9-
import {setupCourses} from '../common/setup-test-data';
3+
import {CommonModule} from "@angular/common";
4+
import {MatDialogModule} from "@angular/material/dialog";
5+
import {DebugElement} from "@angular/core";
6+
import {setupCourses} from "../common/setup-test-data";
7+
import {By} from "@angular/platform-browser";
8+
import {RouterTestingModule} from "@angular/router/testing";
109

1110

1211
describe('CoursesCardListComponent', () => {
1312

14-
let component: CoursesCardListComponent;
15-
let fixture: ComponentFixture<CoursesCardListComponent>;
16-
let el: DebugElement;
13+
let component: CoursesCardListComponent;
14+
let fixture: ComponentFixture<CoursesCardListComponent>;
15+
let el: DebugElement;
1716

18-
beforeEach(waitForAsync(() => {
19-
TestBed.configureTestingModule({
20-
imports: [CoursesModule]
21-
})
17+
beforeEach(waitForAsync (()=> {
18+
TestBed.configureTestingModule(
19+
{
20+
imports: [CommonModule, MatDialogModule, RouterTestingModule]
21+
}
22+
)
2223
.compileComponents()
23-
.then(() => {
24-
25-
fixture = TestBed.createComponent(CoursesCardListComponent);
26-
component = fixture.componentInstance;
27-
el = fixture.debugElement;
28-
29-
});
24+
.then(() => {
25+
fixture = TestBed.createComponent(CoursesCardListComponent);
26+
component = fixture.componentInstance;
27+
el = fixture.debugElement;
28+
})
3029
}));
3130

3231
it('should create the component', () => {
33-
3432
expect(component).toBeTruthy();
35-
3633
});
3734

3835
it('should display the course list', () => {
36+
component.courses = setupCourses();
3937

40-
component.courses = setupCourses();
41-
42-
fixture.detectChanges();
43-
44-
const cards = el.queryAll(By.css(".course-card"));
45-
46-
expect(cards).toBeTruthy("Could not find cards");
47-
expect(cards.length).toBe(12, "Unexpected number of courses");
38+
fixture.detectChanges();
4839

40+
console.log(el.nativeElement.outerHTML);
41+
const res = el.queryAll(By.css('.course-card'))
42+
expect(res.length).toEqual(12);
4943
});
5044

5145
it('should display the first course', () => {
52-
5346
component.courses = setupCourses();
5447

5548
fixture.detectChanges();
5649

5750
const course = component.courses[0];
5851

59-
const card = el.query(By.css(".course-card:first-child")),
60-
title = card.query(By.css("mat-card-title")),
61-
image = card.query(By.css("img"));
62-
63-
expect(card).toBeTruthy("Could not find course card");
52+
const card = el.query(By.css('.course-card:first-child'));
53+
const title = card.query(By.css('mat-card-title'));
54+
const image = card.query(By.css('img'));
6455

6556
expect(title.nativeElement.textContent).toBe(course.titles.description);
66-
6757
expect(image.nativeElement.src).toBe(course.iconUrl);
68-
6958
});
70-
7159
});
7260

7361

src/app/courses/courses.module.ts

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {MatMomentDateModule} from '@angular/material-moment-adapter';
2828
import {ReactiveFormsModule} from '@angular/forms';
2929
import {CoursesService} from './services/courses.service';
3030
import {CourseResolver} from './services/course.resolver';
31+
import {HttpClientTestingModule} from "@angular/common/http/testing";
3132

3233
@NgModule({
3334
declarations: [
@@ -39,6 +40,7 @@ import {CourseResolver} from './services/course.resolver';
3940
],
4041
imports: [
4142
CommonModule,
43+
HttpClientTestingModule,
4244
MatMenuModule,
4345
MatButtonModule,
4446
MatToolbarModule,

src/app/courses/home/async-examples.spec.ts

+44-79
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,102 @@
11
import {fakeAsync, flush, flushMicrotasks, tick} from '@angular/core/testing';
2-
import {of} from 'rxjs';
3-
import {delay} from 'rxjs/operators';
2+
import {of} from "rxjs";
3+
import {delay} from "rxjs/operators";
44

55

6-
describe('Async Testing Examples', () => {
7-
8-
it('Asynchronous test example with Jasmine done()', (done: DoneFn) => {
96

10-
let test = false;
11-
12-
setTimeout(() => {
7+
describe('Async Testing Examples', () => {
138

14-
console.log('running assertions');
9+
it('Asynchronous test example with Jasmine done()', (done) => {
1510

16-
test = true;
11+
let flag = false;
1712

18-
expect(test).toBeTruthy();
13+
setTimeout(()=>{
14+
flag = true;
1915

20-
done();
16+
console.log('Am in timeout-callback right now!');
2117

22-
}, 1000);
18+
expect(flag).toBe(true);
2319

20+
done();
21+
}, 500)
2422
});
2523

2624

2725
it('Asynchronous test example - setTimeout()', fakeAsync(() => {
2826

29-
let test = false;
30-
31-
setTimeout(() => {
32-
});
33-
34-
setTimeout(() => {
35-
36-
console.log('running assertions setTimeout()');
37-
38-
test = true;
39-
40-
}, 1000);
41-
42-
flush();
43-
44-
expect(test).toBeTruthy();
27+
let test = false;
4528

29+
setTimeout(() => {
30+
console.log('In async timeout');
31+
test = true;
32+
}, 100000000);
33+
flush();
34+
expect(test).toBe(true)
4635
}));
4736

4837

4938
it('Asynchronous test example - plain Promise', fakeAsync(() => {
39+
let test = false;
5040

51-
let test = false;
52-
53-
console.log('Creating promise');
54-
55-
Promise.resolve().then(() => {
41+
console.log('Outside promise');
5642

57-
console.log('Promise first then() evaluated successfully');
43+
Promise.resolve().then(()=>{
44+
console.log('Inside promise')
45+
test = true;
46+
})
5847

59-
return Promise.resolve();
60-
})
61-
.then(() => {
62-
63-
console.log('Promise second then() evaluated successfully');
64-
65-
test = true;
66-
67-
});
68-
69-
flushMicrotasks();
70-
71-
console.log('Running test assertions');
72-
73-
expect(test).toBeTruthy();
48+
flushMicrotasks();
7449

50+
expect(test).toBe(true);
7551
}));
7652

7753

7854
it('Asynchronous test example - Promises + setTimeout()', fakeAsync(() => {
7955

80-
let counter = 0;
81-
82-
Promise.resolve()
83-
.then(() => {
56+
let counter = 0;
8457

85-
counter+=10;
58+
console.log('Outside async - operations');
8659

87-
setTimeout(() => {
60+
Promise.resolve().then(() => {
8861

89-
counter += 1;
62+
console.log('In Promise');
9063

91-
}, 1000);
64+
counter += 10;
9265

93-
});
94-
95-
expect(counter).toBe(0);
66+
setTimeout(() => {
67+
console.log('In timeout');
68+
counter += 1;
69+
}, 1000);
70+
});
9671

97-
flushMicrotasks();
72+
expect(counter).toBe(0);
9873

99-
expect(counter).toBe(10);
74+
flushMicrotasks()
10075

101-
tick(500);
76+
expect(counter).toBe(10);
10277

103-
expect(counter).toBe(10);
78+
tick(500);
10479

105-
tick(500);
80+
expect(counter).toBe(10);
10681

107-
expect(counter).toBe(11);
82+
tick(500);
10883

84+
expect(counter).toBe(11);
10985
}));
11086

11187
it('Asynchronous test example - Observables', fakeAsync(() => {
112-
11388
let test = false;
11489

115-
console.log('Creating Observable');
116-
11790
const test$ = of(test).pipe(delay(1000));
11891

11992
test$.subscribe(() => {
120-
121-
test = true;
122-
93+
test = true;
12394
});
12495

12596
tick(1000);
12697

127-
console.log('Running test assertions');
128-
12998
expect(test).toBe(true);
130-
131-
13299
}));
133-
134-
135100
});
136101

137102

0 commit comments

Comments
 (0)