-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(TeacherProjectService): Extract CreateComponentService
- Loading branch information
1 parent
55b3c24
commit c00fa00
Showing
9 changed files
with
130 additions
and
83 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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { CreateComponentService } from '../../assets/wise5/services/createComponentService'; | ||
import { TeacherProjectService } from '../../assets/wise5/services/teacherProjectService'; | ||
import { StudentTeacherCommonServicesModule } from '../student-teacher-common-services.module'; | ||
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http'; | ||
import { ComponentServiceLookupService } from '../../assets/wise5/services/componentServiceLookupService'; | ||
import { Node } from '../../assets/wise5/common/Node'; | ||
|
||
let componentServiceLookupService: ComponentServiceLookupService; | ||
let projectService: TeacherProjectService; | ||
let service: CreateComponentService; | ||
let node; | ||
describe('CreateComponentService', () => { | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [StudentTeacherCommonServicesModule], | ||
providers: [ | ||
CreateComponentService, | ||
TeacherProjectService, | ||
provideHttpClient(withInterceptorsFromDi()) | ||
] | ||
}); | ||
node = new Node(); | ||
componentServiceLookupService = TestBed.inject(ComponentServiceLookupService); | ||
projectService = TestBed.inject(TeacherProjectService); | ||
service = TestBed.inject(CreateComponentService); | ||
spyOn(projectService, 'getNode').and.returnValue(node); | ||
}); | ||
create_NullInsertAfterComponentId(); | ||
create_NonNullInsertAfterComponentId(); | ||
}); | ||
|
||
function create_NullInsertAfterComponentId() { | ||
describe('create() without insertAfterComponentId', () => { | ||
it('should add the new component at the beginning', () => { | ||
const component1 = service.create('node1', 'HTML'); | ||
expect(component1.type).toEqual('HTML'); | ||
expectNodeComponentTypes(['HTML']); | ||
const component2 = service.create('node1', 'OpenResponse'); | ||
expect(component2.type).toEqual('OpenResponse'); | ||
expectNodeComponentTypes(['OpenResponse', 'HTML']); | ||
}); | ||
}); | ||
} | ||
|
||
function create_NonNullInsertAfterComponentId() { | ||
describe('create() with insertAfterComponentId', () => { | ||
it('should add the new component after the insertAfterComponentId', () => { | ||
const component1 = service.create('node1', 'HTML'); | ||
expect(component1.type).toEqual('HTML'); | ||
expectNodeComponentTypes(['HTML']); | ||
const component2 = service.create('node1', 'OpenResponse', component1.id); | ||
expect(component2.type).toEqual('OpenResponse'); | ||
expectNodeComponentTypes(['HTML', 'OpenResponse']); | ||
}); | ||
}); | ||
} | ||
|
||
function expectNodeComponentTypes(types: string[]) { | ||
expect(node.components.map((c) => c.type)).toEqual(types); | ||
} |
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
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
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
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
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
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,45 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { TeacherProjectService } from './teacherProjectService'; | ||
import { ComponentServiceLookupService } from './componentServiceLookupService'; | ||
import { Node } from '../common/Node'; | ||
|
||
@Injectable() | ||
export class CreateComponentService { | ||
constructor( | ||
private componentServiceLookupService: ComponentServiceLookupService, | ||
private projectService: TeacherProjectService | ||
) {} | ||
|
||
/** | ||
* Create a new component | ||
* @param nodeId the node id to create the component in | ||
* @param componentType the component type | ||
* @param insertAfterComponentId Insert the new component after the given | ||
* component id. If this argument is null, we will place the new component | ||
* in the first position. | ||
*/ | ||
create(nodeId: string, componentType: string, insertAfterComponentId: string = null): any { | ||
const node = this.projectService.getNode(nodeId); | ||
const service = this.componentServiceLookupService.getService(componentType); | ||
const component = service.createComponent(); | ||
if (service.componentHasWork(component)) { | ||
if (node.showSaveButton == false) { | ||
if (this.projectService.doesAnyComponentInNodeShowSubmitButton(node.id)) { | ||
component.showSaveButton = true; | ||
} else { | ||
node.showSaveButton = true; | ||
} | ||
} | ||
} | ||
this.addComponentToNode(node, component, insertAfterComponentId); | ||
return component; | ||
} | ||
|
||
private addComponentToNode(node: Node, component: any, insertAfterComponentId: string): void { | ||
const insertPosition = | ||
insertAfterComponentId == null | ||
? 0 | ||
: node.components.findIndex((c) => c.id === insertAfterComponentId) + 1; | ||
node.components.splice(insertPosition, 0, component); | ||
} | ||
} |
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
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