Skip to content

Commit

Permalink
refactor(UtilService): Move connected component functions #1347 (#1348)
Browse files Browse the repository at this point in the history
  • Loading branch information
hirokiterashima authored Jul 20, 2023
1 parent 46723fa commit 64baf04
Show file tree
Hide file tree
Showing 18 changed files with 123 additions and 153 deletions.
22 changes: 12 additions & 10 deletions src/app/services/graphService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function createComponentState(studentData: any, isSubmit: boolean = false) {

function createComponentContent(series: any[] = [], xAxis: any = {}, yAxis: any = {}) {
return {
connectedComponents: [],
series: series,
xAxis: xAxis,
yAxis: yAxis
Expand Down Expand Up @@ -205,16 +206,17 @@ function hasSubmitComponentState() {
}

function canEdit() {
function expectCanEdit(component: any, expectedResult: boolean) {
expect(service.canEdit(component)).toEqual(expectedResult);
}
it('should check if the student can edit the graph when they can not edit', () => {
const multipleSeries = [createSingleSeries([], false), createSingleSeries([], false)];
expectCanEdit(createComponentContent(multipleSeries), false);
});
it('should check if the student can edit the graph when they can edit', () => {
const multipleSeries = [createSingleSeries([], false), createSingleSeries([], true)];
expectCanEdit(createComponentContent(multipleSeries), true);
describe('canEdit()', () => {
it('should return false when they can not edit', () => {
const multipleSeries = [createSingleSeries([], false), createSingleSeries([], false)];
const content = createComponentContent(multipleSeries);
content.connectedComponents = [{ type: 'showWork' }];
expect(service.canEdit(content)).toEqual(false);
});
it('should return true when they can edit', () => {
const multipleSeries = [createSingleSeries([], false), createSingleSeries([], true)];
expect(service.canEdit(createComponentContent(multipleSeries))).toEqual(true);
});
});
}

Expand Down
24 changes: 9 additions & 15 deletions src/app/services/labelService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import { ConfigService } from '../../assets/wise5/services/configService';
import { ProjectService } from '../../assets/wise5/services/projectService';
import { StudentAssetService } from '../../assets/wise5/services/studentAssetService';
import { TagService } from '../../assets/wise5/services/tagService';
import { UtilService } from '../../assets/wise5/services/utilService';
import { LabelService } from '../../assets/wise5/components/label/labelService';
import { TestBed } from '@angular/core/testing';
import { SessionService } from '../../assets/wise5/services/sessionService';

let service: LabelService;
let utilService: UtilService;
let label1: any;
let label2: any;
let label1Text: any = 'Label 1';
Expand Down Expand Up @@ -46,12 +44,10 @@ describe('LabelService', () => {
ProjectService,
SessionService,
StudentAssetService,
TagService,
UtilService
TagService
]
});
service = TestBed.get(LabelService);
utilService = TestBed.get(UtilService);
service = TestBed.inject(LabelService);
label1 = createLabel(label1Text, label1PointX, label1PointY, label1TextX, label1TextY, color1);
label2 = createLabel(label2Text, label2PointX, label2PointY, label2TextX, label2TextY, color2);
});
Expand Down Expand Up @@ -233,15 +229,13 @@ function componentStateHasLabel() {
}

function canEdit() {
it(`should check if the component can be edited when it does not have a show work connected
component`, () => {
spyOn(utilService, 'hasShowWorkConnectedComponent').and.returnValue(false);
expect(service.canEdit({})).toEqual(true);
});
it(`should check if the component can be edited when it does have a show work connected
component`, () => {
spyOn(utilService, 'hasShowWorkConnectedComponent').and.returnValue(true);
expect(service.canEdit({})).toEqual(false);
describe('canEdit()', () => {
it(`should return true when it does not have a show work connected component`, () => {
expect(service.canEdit({})).toEqual(true);
});
it(`should return false when it does have a show work connected component`, () => {
expect(service.canEdit({ connectedComponents: [{ type: 'showWork' }] })).toEqual(false);
});
});
}

Expand Down
18 changes: 18 additions & 0 deletions src/assets/wise5/common/ComponentContent.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ComponentContent, hasConnectedComponent } from './ComponentContent';

describe('ComponentContent', () => {
describe('hasConnectedComponent()', () => {
it('returns false when content does not have any connected components', () => {
const content = {} as ComponentContent;
expect(hasConnectedComponent(content, 'importWork')).toBeFalse();
});
it('returns false when content does not have the specified connected component type', () => {
const content = { connectedComponents: [{ type: 'showWork' }] } as ComponentContent;
expect(hasConnectedComponent(content, 'importWork')).toBeFalse();
});
it('returns true when content has the specified connected component type', () => {
const content = { connectedComponents: [{ type: 'importWork' }] } as ComponentContent;
expect(hasConnectedComponent(content, 'importWork')).toBeTrue();
});
});
});
10 changes: 10 additions & 0 deletions src/assets/wise5/common/ComponentContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@ export interface ComponentContent {
showSubmitButton?: boolean;
type: string;
}

export function hasConnectedComponent(
content: ComponentContent,
connectedComponentType: 'importWork' | 'showWork'
): boolean {
return (
content.connectedComponents != null &&
content.connectedComponents.some((c) => c.type === connectedComponentType)
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { NodeService } from '../../../services/nodeService';
import { NotebookService } from '../../../services/notebookService';
import { StudentAssetService } from '../../../services/studentAssetService';
import { StudentDataService } from '../../../services/studentDataService';
import { UtilService } from '../../../services/utilService';
import { ComponentStudent } from '../../component-student.component';
import { ComponentService } from '../../componentService';
import { AnimationService } from '../animationService';
import { hasConnectedComponent } from '../../../common/ComponentContent';

@Component({
selector: 'animation-student',
Expand Down Expand Up @@ -52,8 +52,7 @@ export class AnimationStudent extends ComponentStudent {
protected NodeService: NodeService,
protected NotebookService: NotebookService,
protected StudentAssetService: StudentAssetService,
protected StudentDataService: StudentDataService,
protected UtilService: UtilService
protected StudentDataService: StudentDataService
) {
super(
AnnotationService,
Expand All @@ -77,7 +76,7 @@ export class AnimationStudent extends ComponentStudent {
this.svgId = this.AnimationService.getSvgId(domIdEnding);
this.initializeCoordinates();

if (this.UtilService.hasShowWorkConnectedComponent(this.componentContent)) {
if (hasConnectedComponent(this.componentContent, 'showWork')) {
this.handleConnectedComponents();
} else if (
this.AnimationService.componentStateHasStudentWork(this.componentState, this.componentContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { NodeService } from '../../../services/nodeService';
import { NotebookService } from '../../../services/notebookService';
import { StudentAssetService } from '../../../services/studentAssetService';
import { StudentDataService } from '../../../services/studentDataService';
import { UtilService } from '../../../services/utilService';
import { ComponentStudent } from '../../component-student.component';
import { ComponentService } from '../../componentService';
import { AudioOscillatorService } from '../audioOscillatorService';
import { hasConnectedComponent } from '../../../common/ComponentContent';

@Component({
selector: 'audio-oscillator-student',
Expand Down Expand Up @@ -71,8 +71,7 @@ export class AudioOscillatorStudent extends ComponentStudent {
protected NodeService: NodeService,
protected NotebookService: NotebookService,
protected StudentAssetService: StudentAssetService,
protected StudentDataService: StudentDataService,
protected UtilService: UtilService
protected StudentDataService: StudentDataService
) {
super(
AnnotationService,
Expand All @@ -97,7 +96,7 @@ export class AudioOscillatorStudent extends ComponentStudent {
this.setButtonTextToPlay();
this.setParametersFromComponentContent();

if (this.UtilService.hasShowWorkConnectedComponent(this.componentContent)) {
if (hasConnectedComponent(this.componentContent, 'showWork')) {
this.handleConnectedComponents();
} else if (
this.AudioOscillatorService.componentStateHasStudentWork(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import { NotebookService } from '../../../services/notebookService';
import { ProjectService } from '../../../services/projectService';
import { StudentAssetService } from '../../../services/studentAssetService';
import { StudentDataService } from '../../../services/studentDataService';
import { UtilService } from '../../../services/utilService';
import { ComponentStudent } from '../../component-student.component';
import { ComponentService } from '../../componentService';
import { ConceptMapService } from '../conceptMapService';
import { DialogWithCloseComponent } from '../../../directives/dialog-with-close/dialog-with-close.component';
import { copy } from '../../../common/object/object';
import { convertToPNGFile } from '../../../common/canvas/canvas';
import { hasConnectedComponent } from '../../../common/ComponentContent';

@Component({
selector: 'concept-map-student',
Expand Down Expand Up @@ -75,8 +75,7 @@ export class ConceptMapStudent extends ComponentStudent {
protected NotebookService: NotebookService,
private ProjectService: ProjectService,
protected StudentAssetService: StudentAssetService,
protected StudentDataService: StudentDataService,
protected UtilService: UtilService
protected StudentDataService: StudentDataService
) {
super(
AnnotationService,
Expand Down Expand Up @@ -156,7 +155,7 @@ export class ConceptMapStudent extends ComponentStudent {

initializeSVG(): void {
this.setupSVG();
if (this.UtilService.hasShowWorkConnectedComponent(this.componentContent)) {
if (hasConnectedComponent(this.componentContent, 'showWork')) {
this.handleConnectedComponents();
} else if (this.componentStateHasStudentWork(this.componentState, this.componentContent)) {
this.componentState = this.ProjectService.injectAssetPaths(this.componentState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { StudentTeacherCommonServicesModule } from '../../../../../app/student-t
import { Component } from '../../../common/Component';
import { ProjectService } from '../../../services/projectService';
import { StudentDataService } from '../../../services/studentDataService';
import { UtilService } from '../../../services/utilService';
import { DrawService } from '../drawService';
import { DrawStudent } from './draw-student.component';

Expand Down Expand Up @@ -87,9 +86,8 @@ function createCanvasObject(type: string): any {
function initializeStudentData() {
it('should initialize student data when there is a show work connected component', () => {
component.componentContent.connectedComponents = [
{ nodeId: 'node', componentId: 'component1' }
{ nodeId: 'node', componentId: 'component1', type: 'showWork' }
];
spyOn(TestBed.inject(UtilService), 'hasShowWorkConnectedComponent').and.returnValue(true);
spyOn(
TestBed.inject(StudentDataService),
'getLatestComponentStateByNodeIdAndComponentId'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { NotebookService } from '../../../services/notebookService';
import { ProjectService } from '../../../services/projectService';
import { StudentAssetService } from '../../../services/studentAssetService';
import { StudentDataService } from '../../../services/studentDataService';
import { UtilService } from '../../../services/utilService';
import { ComponentStudent } from '../../component-student.component';
import { ComponentService } from '../../componentService';
import { DrawService } from '../drawService';
import { MatDialog } from '@angular/material/dialog';
import { copy } from '../../../common/object/object';
import { convertToPNGFile } from '../../../common/canvas/canvas';
import { hasConnectedComponent } from '../../../common/ComponentContent';

@Component({
selector: 'draw-student',
Expand Down Expand Up @@ -41,8 +41,7 @@ export class DrawStudent extends ComponentStudent {
protected NotebookService: NotebookService,
private ProjectService: ProjectService,
protected StudentAssetService: StudentAssetService,
protected StudentDataService: StudentDataService,
protected UtilService: UtilService
protected StudentDataService: StudentDataService
) {
super(
AnnotationService,
Expand Down Expand Up @@ -113,7 +112,7 @@ export class DrawStudent extends ComponentStudent {
}

initializeStudentData(): void {
if (this.UtilService.hasShowWorkConnectedComponent(this.componentContent)) {
if (hasConnectedComponent(this.componentContent, 'showWork')) {
this.handleConnectedComponents();
} else if (
this.DrawService.componentStateHasStudentWork(this.componentState, this.componentContent)
Expand Down
12 changes: 4 additions & 8 deletions src/assets/wise5/components/graph/graphService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import * as html2canvas from 'html2canvas';
import { Injectable } from '@angular/core';
import { ComponentService } from '../componentService';
import { StudentAssetService } from '../../services/studentAssetService';
import { UtilService } from '../../services/utilService';
import { ConfigService } from '../../services/configService';
import { HttpClient } from '@angular/common/http';
import { convertToPNGFile } from '../../common/canvas/canvas';
import { hasConnectedComponent } from '../../common/ComponentContent';

@Injectable()
export class GraphService extends ComponentService {
Expand All @@ -16,8 +16,7 @@ export class GraphService extends ComponentService {
constructor(
private configService: ConfigService,
private http: HttpClient,
private StudentAssetService: StudentAssetService,
protected UtilService: UtilService
private StudentAssetService: StudentAssetService
) {
super();
}
Expand Down Expand Up @@ -128,17 +127,14 @@ export class GraphService extends ComponentService {
* @param component The component content.
* @return Whether the student can perform any work on this component.
*/
canEdit(component: any) {
canEdit(component: any): boolean {
const series = component.series;
for (const singleSeries of series) {
if (singleSeries.canEdit) {
return true;
}
}
if (this.UtilService.hasImportWorkConnectedComponent(component)) {
return true;
}
return false;
return hasConnectedComponent(component, 'importWork');
}

hasSeriesData(studentData: any) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { ConfigService } from '../../../services/configService';
import { NodeService } from '../../../services/nodeService';
import { NotebookService } from '../../../services/notebookService';
import { StudentDataService } from '../../../services/studentDataService';
import { UtilService } from '../../../services/utilService';
import { ComponentStudent } from '../../component-student.component';
import { ComponentService } from '../../componentService';
import { LabelService } from '../labelService';
import { StudentAssetService } from '../../../services/studentAssetService';
import { MatDialog } from '@angular/material/dialog';
import { convertToPNGFile } from '../../../common/canvas/canvas';
import { wordWrap } from '../../../common/string/string';
import { hasConnectedComponent } from '../../../common/ComponentContent';

@Component({
selector: 'label-student',
Expand Down Expand Up @@ -55,8 +55,7 @@ export class LabelStudent extends ComponentStudent {
protected NodeService: NodeService,
protected NotebookService: NotebookService,
protected StudentAssetService: StudentAssetService,
protected StudentDataService: StudentDataService,
protected UtilService: UtilService
protected StudentDataService: StudentDataService
) {
super(
AnnotationService,
Expand Down Expand Up @@ -287,7 +286,7 @@ export class LabelStudent extends ComponentStudent {
}

initializeStudentWork(componentContent: any, componentState: any): void {
if (this.UtilService.hasShowWorkConnectedComponent(componentContent)) {
if (hasConnectedComponent(componentContent, 'showWork')) {
this.handleConnectedComponents();
} else if (this.LabelService.componentStateHasStudentWork(componentState, componentContent)) {
this.setStudentWork(componentState);
Expand Down
11 changes: 4 additions & 7 deletions src/assets/wise5/components/label/labelService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import SVG from 'svg.js';
import { ComponentService } from '../componentService';
import { StudentAssetService } from '../../services/studentAssetService';
import { Injectable } from '@angular/core';
import { UtilService } from '../../services/utilService';
import { convertToPNGFile } from '../../common/canvas/canvas';
import { wordWrap } from '../../common/string/string';
import { hasConnectedComponent } from '../../common/ComponentContent';

@Injectable()
export class LabelService extends ComponentService {
Expand All @@ -16,10 +16,7 @@ export class LabelService extends ComponentService {
circleZIndex: number = 2;
defaultTextBackgroundColor: string = 'blue';

constructor(
private StudentAssetService: StudentAssetService,
protected UtilService: UtilService
) {
constructor(private StudentAssetService: StudentAssetService) {
super();
}

Expand Down Expand Up @@ -83,8 +80,8 @@ export class LabelService extends ComponentService {
* @param component The component content.
* @return Whether the student can perform any work on this component.
*/
canEdit(component: any) {
return !this.UtilService.hasShowWorkConnectedComponent(component);
canEdit(component: any): boolean {
return !hasConnectedComponent(component, 'showWork');
}

componentStateHasStudentWork(componentState: any, componentContent: any): boolean {
Expand Down
Loading

0 comments on commit 64baf04

Please sign in to comment.