-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* create generic builder
- Loading branch information
1 parent
887af1c
commit 1b4ed92
Showing
2 changed files
with
73 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ import { ViewRolePrivilegesModalComponent } from './view-role-privileges-modal/v | |
import { CourseService } from '../../../services/course.service'; | ||
import { InstructorService } from '../../../services/instructor.service'; | ||
import { SimpleModalService } from '../../../services/simple-modal.service'; | ||
import { instructorBuilder } from '../../../test-helpers/generic-builder'; | ||
import { createMockNgbModalRef } from '../../../test-helpers/mock-ngb-modal-ref'; | ||
import { Course, Instructor, InstructorPermissionRole, JoinState } from '../../../types/api-output'; | ||
import { InstructorCreateRequest } from '../../../types/api-request'; | ||
|
@@ -41,26 +42,19 @@ const testCourse: Course = { | |
deletionTimestamp: 1000, | ||
}; | ||
|
||
const testInstructor1: Instructor = { | ||
courseId: 'exampleId', | ||
email: '[email protected]', | ||
joinState: JoinState.JOINED, | ||
name: 'Instructor 1', | ||
}; | ||
const testInstructor1: Instructor = instructorBuilder.email('[email protected]').name('Instructor 1').build(); | ||
|
||
const testInstructor2: Instructor = { | ||
courseId: 'exampleId', | ||
email: '[email protected]', | ||
joinState: JoinState.NOT_JOINED, | ||
name: 'Instructor 2', | ||
}; | ||
const testInstructor2 = instructorBuilder | ||
.email('[email protected]') | ||
.joinState(JoinState.NOT_JOINED) | ||
.name('Instructor 2') | ||
.build(); | ||
|
||
const testInstructor3: Instructor = { | ||
courseId: 'exampleId', | ||
email: '[email protected]', | ||
joinState: JoinState.NOT_JOINED, | ||
name: 'Instructor 3', | ||
}; | ||
const testInstructor3 = instructorBuilder | ||
.email('[email protected]') | ||
.joinState(JoinState.NOT_JOINED) | ||
.name('Instructor 3') | ||
.build(); | ||
|
||
const emptyInstructorPanel: InstructorEditPanel = { | ||
googleId: '', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Course, Instructor, JoinState } from '../types/api-output'; | ||
|
||
type GenericBuilder<T> = { | ||
[K in keyof T]: (value: T[K]) => GenericBuilder<T>; | ||
} & { build(): T }; | ||
|
||
/** | ||
* A generic builder function that creates a builder object for constructing objects with specified initial values. | ||
* | ||
* @template T - The type of object being constructed. | ||
* @param initialValues - The initial values for the object being constructed. | ||
* @returns A generic builder object. | ||
* @example | ||
* // Create a course builder with initial values. | ||
* const courseBuilder = createBuilder<Course>({ | ||
* courseId: 'exampleId', | ||
* courseName: '', | ||
* timeZone: '', | ||
* institute: '', | ||
* creationTimestamp: 0, | ||
* deletionTimestamp: 0, | ||
* }); | ||
* | ||
* // Usage of builder: | ||
* const myCourse = courseBuilder | ||
* .courseName('Introduction to TypeScript') | ||
* .timeZone('UTC+0') | ||
* .institute('My University') | ||
* .creationTimestamp(Date.now()) | ||
* .build(); | ||
*/ | ||
export function createBuilder<T extends object>(initialValues: T): GenericBuilder<T> { | ||
const builder: any = {}; | ||
|
||
(Object.keys(initialValues) as (keyof T)[]).forEach((key) => { | ||
builder[key] = (value: T[keyof T]) => { | ||
initialValues[key] = value; | ||
return builder; | ||
}; | ||
}); | ||
|
||
builder.build = () => ({ ...initialValues }); | ||
|
||
return builder; | ||
} | ||
|
||
export const courseBuilder = createBuilder<Course>({ | ||
courseId: 'exampleId', | ||
courseName: '', | ||
timeZone: '', | ||
institute: '', | ||
creationTimestamp: 0, | ||
deletionTimestamp: 0, | ||
}); | ||
|
||
export const instructorBuilder = createBuilder<Instructor>({ | ||
courseId: 'exampleId', | ||
email: '', | ||
name: '', | ||
joinState: JoinState.JOINED, | ||
}); |