Skip to content

Commit 05db62e

Browse files
committed
Enhance webapp services' test cases organization
1 parent 74dad39 commit 05db62e

File tree

3 files changed

+110
-84
lines changed

3 files changed

+110
-84
lines changed

webapp/src/app/services/tcr-build-info.service.spec.ts

+38-29
Original file line numberDiff line numberDiff line change
@@ -21,41 +21,50 @@ describe('TcrBuildInfoService', () => {
2121
httpMock.verify();
2222
});
2323

24-
it('should be created', () => {
25-
expect(service).toBeTruthy();
26-
});
24+
describe('service instance', () => {
2725

28-
it('should return expected build info when getBuildInfo is called', () => {
29-
const sample: TcrBuildInfo = {
30-
version: "1.0.0",
31-
os: "some-os",
32-
arch: "some-arch",
33-
commit: "abc123",
34-
date: "2024-01-01T00:00:00Z",
35-
author: "some-author",
36-
};
37-
38-
let actual: TcrBuildInfo | undefined;
39-
service.getBuildInfo().subscribe(other => {
40-
actual = other;
26+
it('should be created', () => {
27+
expect(service).toBeTruthy();
4128
});
4229

43-
const req = httpMock.expectOne(`/api/build-info`);
44-
expect(req.request.method).toBe('GET');
45-
expect(req.request.responseType).toEqual('json');
46-
req.flush(sample);
47-
expect(actual).toEqual(sample);
4830
});
4931

50-
it('should return undefined when getBuildInfo receives an error response', () => {
51-
let actual: TcrBuildInfo | undefined;
52-
service.getBuildInfo().subscribe(other => {
53-
actual = other;
32+
describe('getBuildInfo() function', () => {
33+
34+
it('should return build info when called', () => {
35+
const sample: TcrBuildInfo = {
36+
version: "1.0.0",
37+
os: "some-os",
38+
arch: "some-arch",
39+
commit: "abc123",
40+
date: "2024-01-01T00:00:00Z",
41+
author: "some-author",
42+
};
43+
44+
let actual: TcrBuildInfo | undefined;
45+
service.getBuildInfo().subscribe(other => {
46+
actual = other;
47+
});
48+
49+
const req = httpMock.expectOne(`/api/build-info`);
50+
expect(req.request.method).toBe('GET');
51+
expect(req.request.responseType).toEqual('json');
52+
req.flush(sample);
53+
expect(actual).toEqual(sample);
54+
});
55+
56+
it('should return undefined when receiving an error response', () => {
57+
let actual: TcrBuildInfo | undefined;
58+
service.getBuildInfo().subscribe(other => {
59+
actual = other;
60+
});
61+
62+
const req = httpMock.expectOne(`/api/build-info`);
63+
expect(req.request.method).toBe('GET');
64+
req.flush({message: 'Some network error'}, {status: 500, statusText: 'Server Error'});
65+
expect(actual).toBeUndefined();
5466
});
5567

56-
const req = httpMock.expectOne(`/api/build-info`);
57-
expect(req.request.method).toBe('GET');
58-
req.flush({message: 'Some network error'}, {status: 500, statusText: 'Server Error'});
59-
expect(actual).toBeUndefined();
6068
});
69+
6170
});

webapp/src/app/services/tcr-session-info.service.spec.ts

+40-31
Original file line numberDiff line numberDiff line change
@@ -21,44 +21,53 @@ describe('TcrSessionInfoService', () => {
2121
httpMock.verify();
2222
});
2323

24-
it('should be created', () => {
25-
expect(service).toBeTruthy();
24+
describe('service instance', () => {
25+
26+
it('should be created', () => {
27+
expect(service).toBeTruthy();
28+
});
29+
2630
});
2731

28-
it('should return expected session info when getSessionInfo is called', () => {
29-
const sample: TcrSessionInfo = {
30-
baseDir: "/my/base/dir",
31-
commitOnFail: false,
32-
gitAutoPush: false,
33-
language: "java",
34-
messageSuffix: "my-suffix",
35-
toolchain: "gradle",
36-
vcsName: "git",
37-
vcsSession: "my VCS session",
38-
workDir: "/my/work/dir"
39-
};
32+
describe('getSessionInfo() function', () => {
33+
34+
it('should return session info when called', () => {
35+
const sample: TcrSessionInfo = {
36+
baseDir: "/my/base/dir",
37+
commitOnFail: false,
38+
gitAutoPush: false,
39+
language: "java",
40+
messageSuffix: "my-suffix",
41+
toolchain: "gradle",
42+
vcsName: "git",
43+
vcsSession: "my VCS session",
44+
workDir: "/my/work/dir"
45+
};
4046

41-
let actual: TcrSessionInfo | undefined;
42-
service.getSessionInfo().subscribe(other => {
43-
actual = other;
47+
let actual: TcrSessionInfo | undefined;
48+
service.getSessionInfo().subscribe(other => {
49+
actual = other;
50+
});
51+
52+
const req = httpMock.expectOne(`/api/session-info`);
53+
expect(req.request.method).toBe('GET');
54+
expect(req.request.responseType).toEqual('json');
55+
req.flush(sample);
56+
expect(actual).toEqual(sample);
4457
});
4558

46-
const req = httpMock.expectOne(`/api/session-info`);
47-
expect(req.request.method).toBe('GET');
48-
expect(req.request.responseType).toEqual('json');
49-
req.flush(sample);
50-
expect(actual).toEqual(sample);
51-
});
59+
it('should return undefined when receiving an error response', () => {
60+
let actual: TcrSessionInfo | undefined;
61+
service.getSessionInfo().subscribe(other => {
62+
actual = other;
63+
});
5264

53-
it('should return undefined when getSessionInfo receives an error response', () => {
54-
let actual: TcrSessionInfo | undefined;
55-
service.getSessionInfo().subscribe(other => {
56-
actual = other;
65+
const req = httpMock.expectOne(`/api/session-info`);
66+
expect(req.request.method).toBe('GET');
67+
req.flush({message: 'Some network error'}, {status: 500, statusText: 'Server error'});
68+
expect(actual).toBeUndefined();
5769
});
5870

59-
const req = httpMock.expectOne(`/api/session-info`);
60-
expect(req.request.method).toBe('GET');
61-
req.flush({message: 'Some network error'}, {status: 500, statusText: 'Server error'});
62-
expect(actual).toBeUndefined();
6371
});
72+
6473
});

webapp/src/app/services/websocket.service.spec.ts

+32-24
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,43 @@ describe('WebsocketService', () => {
3232
});
3333
});
3434

35-
it('should attempt a websocket connection on create', () => {
36-
const expectedUrl = 'ws://' + window.location.host + '/ws';
37-
expect(fakeSocketCtor).toHaveBeenCalledOnceWith(expectedUrl);
38-
});
35+
describe('service instance', () => {
3936

40-
it('should be able to receive TCR messages', (done) => {
41-
let actual: TcrMessage | undefined;
42-
service.webSocket$.subscribe((msg) => {
43-
actual = msg;
44-
done();
37+
it('should be created', () => {
38+
expect(service).toBeTruthy();
4539
});
46-
fakeSocket.next(sampleMessage);
47-
expect(actual).toBe(sampleMessage);
48-
});
4940

50-
it('should handle websocket errors', () => {
51-
const sampleError = new Error('WebSocket error');
52-
let actual: Error | undefined;
53-
service.webSocket$.asObservable().subscribe({
54-
error: (err) => actual = err,
41+
it('should attempt a websocket connection on create', () => {
42+
const expectedUrl = 'ws://' + window.location.host + '/ws';
43+
expect(fakeSocketCtor).toHaveBeenCalledOnceWith(expectedUrl);
44+
});
45+
46+
it('should be able to forward received TCR messages', (done) => {
47+
let actual: TcrMessage | undefined;
48+
service.webSocket$.subscribe((msg) => {
49+
actual = msg;
50+
done();
51+
});
52+
fakeSocket.next(sampleMessage);
53+
expect(actual).toBe(sampleMessage);
54+
});
55+
56+
it('should handle websocket errors', () => {
57+
const sampleError = new Error('WebSocket error');
58+
let actual: Error | undefined;
59+
service.webSocket$.asObservable().subscribe({
60+
error: (err) => actual = err,
61+
});
62+
fakeSocket.error(sampleError);
63+
expect(actual).toEqual(sampleError);
64+
});
65+
66+
it('should close the websocket on destroy', () => {
67+
spyOn(fakeSocket, 'complete');
68+
service.ngOnDestroy();
69+
expect(fakeSocket.complete).toHaveBeenCalled();
5570
});
56-
fakeSocket.error(sampleError);
57-
expect(actual).toEqual(sampleError);
58-
});
5971

60-
it('should close the websocket on destroy', () => {
61-
spyOn(fakeSocket, 'complete');
62-
service.ngOnDestroy();
63-
expect(fakeSocket.complete).toHaveBeenCalled();
6472
});
6573

6674
});

0 commit comments

Comments
 (0)