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

[#12695] implement createSpyFromClass test-helper #12696

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -2,7 +2,9 @@ import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { CommentVisibilityStateMachine } from '../../../../services/comment-visibility-state-machine';
import { FeedbackResponseCommentService } from '../../../../services/feedback-response-comment.service';
import createSpyFromClass from '../../../../test-helpers/create-spy-from-class';
import { CommentVisibilityType, FeedbackVisibilityType } from '../../../../types/api-output';
import { RichTextEditorModule } from '../../rich-text-editor/rich-text-editor.module';
import { TeammatesCommonModule } from '../../teammates-common/teammates-common.module';
Expand All @@ -17,15 +19,10 @@ describe('CommentRowComponent', () => {
let component: CommentRowComponent;
let fixture: ComponentFixture<CommentRowComponent>;

const spyVisibilityStateMachine: any = {
allowAllApplicableTypesToSee: jest.fn(),
applyVisibilitySettings: jest.fn(),
getVisibilityTypesUnderVisibilityControl: jest.fn(),
};
const spyVisibilityStateMachine = createSpyFromClass(CommentVisibilityStateMachine);

const spyCommentService: any = {
getNewVisibilityStateMachine: jest.fn().mockReturnValue(spyVisibilityStateMachine),
};
const spyCommentService = createSpyFromClass(FeedbackResponseCommentService);
spyCommentService.getNewVisibilityStateMachine.mockReturnValue(spyVisibilityStateMachine);

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { RouterTestingModule } from '@angular/router/testing';
import { of } from 'rxjs';

import { HttpRequestService } from '../../../services/http-request.service';
import createSpyFromClass from '../../../test-helpers/create-spy-from-class';
import { ResourceEndpoints } from '../../../types/api-const';
import { InstructorPermissionSet, InstructorPrivilege, JoinState, Student, Students } from '../../../types/api-output';
import { StudentListRowModel } from '../../components/student-list/student-list.component';
Expand Down Expand Up @@ -55,12 +56,7 @@ describe('InstructorSearchPageComponent', () => {
};

beforeEach(() => {
spyHttpRequestService = {
get: jest.fn(),
post: jest.fn(),
put: jest.fn(),
delete: jest.fn(),
};
spyHttpRequestService = createSpyFromClass(HttpRequestService);
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [
Expand Down
29 changes: 29 additions & 0 deletions src/web/test-helpers/create-spy-from-class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Creates a spy object for a given class, providing spy functions for all of its methods.
*
* @param Class - The class for which to create the spy
* @returns A spy object with spy functions for each method of the provided class.
* @example
* class MyClass {
* method1() {}
* method2() {}
* }
*
* createSpyFromClass(MyClass) returns { method1: jest.fn(), method2: jest.fn() }
*/
export default function createSpyFromClass<T>(Class: new (...args: any[]) => T): Spy<T> {
const spy = {} as Spy<T>;

Object.getOwnPropertyNames(Class.prototype).forEach((methodName) => {
if (typeof Class.prototype[methodName] === 'function') {
spy[methodName as keyof T] = jest.fn();
}
});

return spy;
}

// Helper type for better typing
type Spy<T> = {
[K in keyof T]: jest.Mock<any, any>;
};
Loading