Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development: Improve course client test coverage #6491

Merged
merged 6 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ describe('CourseRegistrationComponent', () => {
title: 'Course A',
} as Course;

const course2 = {
id: 2,
title: 'Course B',
};

beforeEach(() => {
TestBed.configureTestingModule({
imports: [ArtemisTestModule],
Expand Down Expand Up @@ -60,4 +65,12 @@ describe('CourseRegistrationComponent', () => {

expect(component.coursesToSelect).toHaveLength(0);
});

it('should sort registrable courses by title', () => {
findAllForRegistrationStub.mockReturnValue(of(new HttpResponse({ body: [course2, course1] })));

component.loadRegistrableCourses();

expect(component.coursesToSelect).toEqual([course1, course2]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ describe('CourseOverviewComponent', () => {
let jhiWebsocketServiceReceiveStub: jest.SpyInstance;
let jhiWebsocketServiceSubscribeSpy: jest.SpyInstance;
let findOneForDashboardStub: jest.SpyInstance;
let findOneForRegistrationStub: jest.SpyInstance;

let metisConversationService: MetisConversationService;
const course = { id: 1 } as Course;
Expand Down Expand Up @@ -193,6 +194,10 @@ describe('CourseOverviewComponent', () => {
jest.spyOn(teamService, 'teamAssignmentUpdates', 'get').mockResolvedValue(of(new TeamAssignmentPayload()));
// default for findOneForDashboardStub is to return the course
findOneForDashboardStub = jest.spyOn(courseService, 'findOneForDashboard').mockReturnValue(of(new HttpResponse({ body: course1, headers: new HttpHeaders() })));
// default for findOneForRegistrationStub is to return the course as well
findOneForRegistrationStub = jest
.spyOn(courseService, 'findOneForRegistration')
.mockReturnValue(of(new HttpResponse({ body: course1, headers: new HttpHeaders() })));
});
}));

Expand Down Expand Up @@ -258,6 +263,52 @@ describe('CourseOverviewComponent', () => {
expect(subscribeToTeamAssignmentUpdatesStub).toHaveBeenCalledOnce();
});

it('should show an alert when loading the course fails', async () => {
findOneForDashboardStub.mockReturnValue(throwError(new HttpResponse({ status: 404 })));
const alertService = TestBed.inject(AlertService);
const alertServiceSpy = jest.spyOn(alertService, 'addAlert');

component.loadCourse().subscribe(
() => {
throw new Error('should not happen');
},
(error) => {
expect(error).toBeDefined();
},
);

expect(alertServiceSpy).toHaveBeenCalled();
});

it('should return false for canRegisterForCourse if the server returns 403', fakeAsync(() => {
findOneForRegistrationStub.mockReturnValue(throwError(new HttpResponse({ status: 403 })));

// test that canRegisterForCourse subscribe gives false
component.canRegisterForCourse().subscribe((canRegister) => {
expect(canRegister).toBeFalse();
});

// wait for the observable to complete
tick();
}));

it('should throw for unexpected registration responses from the server', fakeAsync(() => {
findOneForRegistrationStub.mockReturnValue(throwError(new HttpResponse({ status: 404 })));

// test that canRegisterForCourse throws
component.canRegisterForCourse().subscribe(
() => {
throw new Error('should not be called');
},
(error) => {
expect(error).toEqual(new HttpResponse({ status: 404 }));
},
);

// wait for the observable to complete
tick();
}));

it('should load the course, even when just calling loadCourse by itself (for refreshing)', () => {
// check that loadCourse already subscribes to the course itself

Expand Down
21 changes: 20 additions & 1 deletion src/test/javascript/spec/service/alert.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ describe('Alert Service Test', () => {
});
service = TestBed.inject(AlertService);
eventManager = TestBed.inject(EventManager);
jest.useFakeTimers();
});

it('should produce a proper alert object and fetch it', () => {
Expand Down Expand Up @@ -89,6 +88,8 @@ describe('Alert Service Test', () => {
});

it('should close an alert on timeout correctly', () => {
jest.useFakeTimers();

const alert = { type: AlertType.INFO, message: 'Hello Jhipster info', onClose: jest.fn() } as AlertCreationProperties;
service.addAlert(alert);

Expand Down Expand Up @@ -299,4 +300,22 @@ describe('Alert Service Test', () => {

flush();
}));

it.each([400, 403, 405, 412])('should not show alerts with skipAlert=true', (statusCode) => {
// GIVEN
const response = new HttpErrorResponse({
url: 'http://localhost:8080/api/foos',
headers: new HttpHeaders().append('app-error', 'Error Message').append('app-params', 'foo'),
status: statusCode,
statusText: 'Some Error',
error: {
status: statusCode,
message: 'error.validation',
skipAlert: true,
},
});
eventManager.broadcast({ name: 'artemisApp.httpError', content: response });
// THEN
expect(service.get()).toBeEmpty();
});
});