Skip to content

Commit

Permalink
refactor(StudentDataService): Clean up code (#1480)
Browse files Browse the repository at this point in the history
  • Loading branch information
hirokiterashima authored Oct 20, 2023
1 parent 6d32a43 commit eca7ddb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 92 deletions.
45 changes: 0 additions & 45 deletions src/app/services/studentDataService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ describe('StudentDataService', () => {
shouldCheckIsComponentSubmitDirty();
shouldGetLatestComponentStateByNodeIdAndComponentId();
shouldGetLatestSubmitComponentState();
shouldGetStudentWorkByStudentWorkId();
shouldGetComponentStatesByNodeId();
shouldGetComponentStatesByNodeIdAndComponentId();
shouldGetEventsByNodeId();
shouldGetEventsByNodeIdAndComponentId();
shouldGetLatestNodeEnteredEventNodeIdWithExistingNode();
shouldCalculateCanVisitNode();
shouldGetNodeStatusByNodeId();
Expand Down Expand Up @@ -408,31 +406,6 @@ function shouldGetLatestSubmitComponentState() {
});
}

function shouldGetStudentWorkByStudentWorkId() {
it('should get student work by student work id with an id that does not exist', () => {
service.studentData = {
componentStates: [
createComponentState(1, 'node1', 'component1'),
createComponentState(2, 'node2', 'component2'),
createComponentState(3, 'node3', 'component3')
]
};
const componentState = service.getStudentWorkByStudentWorkId(4);
expect(componentState).toEqual(null);
});
it('should get student work by student work id with an id that does exist', () => {
service.studentData = {
componentStates: [
createComponentState(1, 'node1', 'component1'),
createComponentState(2, 'node1', 'component1'),
createComponentState(3, 'node1', 'component1')
]
};
const componentState = service.getStudentWorkByStudentWorkId(2);
expect(componentState.id).toEqual(2);
});
}

function shouldGetComponentStatesByNodeId() {
it('should get component states by node id', () => {
service.studentData = {
Expand Down Expand Up @@ -489,24 +462,6 @@ function shouldGetEventsByNodeId() {
});
}

function shouldGetEventsByNodeIdAndComponentId() {
it('should get events by node id and component id', () => {
service.studentData = {
events: [
createEvent(1, 'node1', 'component1'),
createEvent(2, 'node1', 'component2'),
createEvent(3, 'node2', 'component3'),
createEvent(4, 'node3', 'component4'),
createEvent(5, 'node1', 'component1')
]
};
const events = service.getEventsByNodeIdAndComponentId('node1', 'component1');
expect(events.length).toEqual(2);
expect(events[0].id).toEqual(1);
expect(events[1].id).toEqual(5);
});
}

function shouldGetLatestNodeEnteredEventNodeIdWithExistingNode() {
it('should get latest node entered event node id with existing node', () => {
service.studentData = {
Expand Down
60 changes: 13 additions & 47 deletions src/assets/wise5/services/studentDataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,63 +551,29 @@ export class StudentDataService extends DataService {
return null;
}

getStudentWorkByStudentWorkId(studentWorkId) {
for (const componentState of this.studentData.componentStates) {
if (componentState.id === studentWorkId) {
return componentState;
}
}
return null;
}

getComponentStates() {
getComponentStates(): any[] {
return this.studentData.componentStates;
}

getComponentStatesByNodeId(nodeId) {
const componentStatesByNodeId = [];
for (const componentState of this.studentData.componentStates) {
if (componentState.nodeId === nodeId) {
componentStatesByNodeId.push(componentState);
}
}
return componentStatesByNodeId;
getComponentStatesByNodeId(nodeId: string): any[] {
return this.studentData.componentStates.filter(
(componentState) => componentState.nodeId === nodeId
);
}

getComponentStatesByNodeIdAndComponentId(nodeId, componentId) {
const componentStatesByNodeIdAndComponentId = [];
for (const componentState of this.studentData.componentStates) {
if (componentState.nodeId === nodeId && componentState.componentId === componentId) {
componentStatesByNodeIdAndComponentId.push(componentState);
}
}
return componentStatesByNodeIdAndComponentId;
getComponentStatesByNodeIdAndComponentId(nodeId: string, componentId: string): any[] {
return this.studentData.componentStates.filter(
(componentState) =>
componentState.nodeId === nodeId && componentState.componentId === componentId
);
}

getEvents() {
getEvents(): any[] {
return this.studentData.events;
}

getEventsByNodeId(nodeId) {
const eventsByNodeId = [];
const events = this.studentData.events;
for (const event of events) {
if (event.nodeId === nodeId) {
eventsByNodeId.push(event);
}
}
return eventsByNodeId;
}

getEventsByNodeIdAndComponentId(nodeId, componentId) {
const eventsByNodeId = [];
const events = this.studentData.events;
for (const event of events) {
if (event.nodeId === nodeId && event.componentId === componentId) {
eventsByNodeId.push(event);
}
}
return eventsByNodeId;
getEventsByNodeId(nodeId: string): any[] {
return this.studentData.events.filter((event) => event.nodeId === nodeId);
}

/**
Expand Down

0 comments on commit eca7ddb

Please sign in to comment.