Skip to content

Commit

Permalink
Merge pull request #71 from rajnishdargan/7.0.0
Browse files Browse the repository at this point in the history
Issue #IQ-674 fix: Enhancing the web-component to accept the config from Parent App
  • Loading branch information
sajeshkayyath authored Feb 12, 2024
2 parents ecb8116 + e92bb84 commit 676553b
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export class SectionPlayerComponent implements OnChanges, AfterViewInit {
if (changes && Object.values(changes)[0].firstChange) {
this.subscribeToEvents();
}
this.viewerService.sectionConfig = this.sectionConfig;
this.setConfig();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { QumlLibraryService } from '../../quml-library.service';
import { UtilService } from '../../util-service';
import { QuestionCursor } from '../../quml-question-cursor.service';
import { of, throwError } from 'rxjs';
import { TransformationService } from '../transformation-service/transformation.service';

describe('ViewerService', () => {
class MockQuestionCursor {
Expand Down Expand Up @@ -205,17 +206,32 @@ describe('ViewerService', () => {
service.identifiers = ['do_123', 'do_124'];
spyOn(service.questionCursor, 'getQuestion').and.returnValue(of([{ id: 'do_123' }, { id: 'do_124' }] as any));
spyOn(service.qumlQuestionEvent, 'emit');
service.sectionConfig = mockData.playerConfig;
service.getQuestion();
expect(service.qumlQuestionEvent.emit).toHaveBeenCalled();
expect(service.questionCursor.getQuestion).toHaveBeenCalled();
});

it('should emit transformed question metadata if question body is available', () => {
const service = TestBed.inject(ViewerService);
const sectionChildren = [{ identifier: '1', body: 'Question 1' }];
const fetchedQuestionData = { questions: [{ identifier: '1', body: 'Question 1' }], count: 1 };
spyOn(service.transformationService, 'getTransformedQuestionMetadata').and.returnValue(fetchedQuestionData);
spyOn(service.qumlQuestionEvent, 'emit').and.callFake(() => {});
service.threshold = 1;
service.identifiers = ['1'];
service.sectionConfig = {metadata:{children: sectionChildren}};
service.getQuestion();
expect(service.qumlQuestionEvent.emit).toHaveBeenCalledWith(fetchedQuestionData);
});

it('should call getQuestion and return the error', () => {
const service = TestBed.inject(ViewerService);
const qumlLibraryService = TestBed.inject(QumlLibraryService);
service.identifiers = ['do_123', 'do_124'];
spyOn(service.questionCursor, 'getQuestion').and.returnValue(throwError('Error'));
spyOn(service.qumlQuestionEvent, 'emit');
service.sectionConfig = mockData.playerConfig;
service.getQuestion();
expect(service.qumlQuestionEvent.emit).toHaveBeenCalled();
expect(service.questionCursor.getQuestion).toHaveBeenCalled();
Expand All @@ -226,18 +242,54 @@ describe('ViewerService', () => {
const qumlLibraryService = TestBed.inject(QumlLibraryService);
service.identifiers = [];
spyOn(service.questionCursor, 'getQuestion');
service.sectionConfig = mockData.playerConfig;
service.getQuestion();
expect(service.questionCursor.getQuestion).not.toHaveBeenCalled();
});

it('should return available questions if sectionChildren is not empty', () => {
const service = TestBed.inject(ViewerService);
const sectionChildren = [{ identifier: '1', body: 'Question 1' }, { identifier: '2', body: 'Question 2' }];
const questionIdArr = ['1', '2'];
service.getSectionQuestionData(sectionChildren, questionIdArr).subscribe(result => {
expect(result.questions.length).toBe(2);
expect(result.count).toBe(2);
});
});

it('should return questionsIdNotHavingCompleteData if sectionChildren is empty', () => {
const service = TestBed.inject(ViewerService);
const sectionChildren = [];
const questionIdArr = ['1', '2'];
spyOn(service, 'fetchIncompleteQuestionsData').and.returnValue(of({questions: [{ identifier: '1', body: 'Question 1' }, { identifier: '2', body: 'Question 2' }], count: 2}))
service.getSectionQuestionData(sectionChildren, questionIdArr).subscribe(result => {
expect(result.questions.length).toBe(2);
});
});

it('should fetch incomplete questions data and return combined questions', () => {
const service = TestBed.inject(ViewerService);
const availableQuestions = [{ identifier: '1', body: 'Question 1' }];
const questionsIdNotHavingCompleteData = ['2'];
const questionData = { identifier: '2', body: 'Question 2' }
spyOn(service.questionCursor, 'getQuestions').and.returnValue(of([{ questions: [questionData], count: 1 }] as any))
service.fetchIncompleteQuestionsData(availableQuestions, questionsIdNotHavingCompleteData).subscribe(result => {
expect(result.questions.length).toBe(2);
expect(result.count).toBe(2);
});
});

it('should call getQuestions', () => {
const service = TestBed.inject(ViewerService);
service.parentIdentifier = 'do_555';
service.identifiers = ['do_123', 'do_124'];
spyOn(service.questionCursor, 'getQuestions').and.returnValue(of([{ id: 'do_123' }] as any));
spyOn(service.qumlQuestionEvent, 'emit');
service.parentIdentifier = 'do_21348431528472576011';
service.identifiers = ['do_21348431559137689613', 'do_21348431640099225615'];
spyOn(service, 'getSectionQuestionData').and.returnValue(of([{ id: 'do_21348431559137689613' }] as any));
const getTransformedQuestionMetadata = TestBed.inject(TransformationService);
spyOn(getTransformedQuestionMetadata, 'getTransformedQuestionMetadata').and.returnValue({questions: [{ id: 'do_21348431559137689613' }], count: 1})
spyOn(service.qumlQuestionEvent, 'emit').and.callFake(() => {});
service.sectionConfig = mockData.playerConfig;
service.getQuestions(0, 1)
expect(service.questionCursor.getQuestions).toHaveBeenCalled();
expect(service.getSectionQuestionData).toHaveBeenCalled();
expect(service.qumlQuestionEvent.emit).toHaveBeenCalled();
});

Expand All @@ -246,10 +298,11 @@ describe('ViewerService', () => {
service.parentIdentifier = 'do_555';
service.identifiers = ['do_123', 'do_124'];
service.threshold = 3;
spyOn(service.questionCursor, 'getQuestions').and.returnValue(throwError('Error'));
spyOn(service, 'getSectionQuestionData').and.returnValue(throwError('Error'));
spyOn(service.qumlQuestionEvent, 'emit');
service.sectionConfig = mockData.playerConfig;
service.getQuestions()
expect(service.questionCursor.getQuestions).toHaveBeenCalled();
expect(service.getSectionQuestionData).toHaveBeenCalled();
expect(service.qumlQuestionEvent.emit).toHaveBeenCalled();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { TransformationService } from '../transformation-service/transformation.
import { eventName, TelemetryType } from '../../telemetry-constants';
import { QuestionCursor } from '../../quml-question-cursor.service';
import * as _ from 'lodash-es';
import { forkJoin } from 'rxjs';
import { forkJoin, of } from 'rxjs';
import { switchMap } from 'rxjs/operators';

@Injectable({
providedIn: 'root'
Expand Down Expand Up @@ -35,7 +36,7 @@ export class ViewerService {
questionSetId: string;
parentIdentifier: string;
sectionQuestions = [];

sectionConfig:any;
constructor(
public qumlLibraryService: QumlLibraryService,
public utilService: UtilService,
Expand Down Expand Up @@ -231,19 +232,54 @@ export class ViewerService {
this.qumlLibraryService.error(stacktrace, { err: errorCode, errtype: errorType });
}

getSectionQuestionData(sectionChildren, questionIdArr) {
const availableQuestions = [];
let questionsIdNotHavingCompleteData = [];
if (_.isEmpty(sectionChildren)) {
questionsIdNotHavingCompleteData = questionIdArr;
} else {
const foundQuestions = sectionChildren.filter(child => questionIdArr.includes(child.identifier));
for (const question of foundQuestions) {
if (_.has(question, 'body')) {
availableQuestions.push(question);
} else {
questionsIdNotHavingCompleteData.push(question.identifier);
}
}
}

if (!_.isEmpty(questionsIdNotHavingCompleteData)) {
return this.fetchIncompleteQuestionsData(availableQuestions, questionsIdNotHavingCompleteData);
} else {
const allQuestions$ = of({ questions: availableQuestions, count: availableQuestions.length });
return allQuestions$;
}
}

fetchIncompleteQuestionsData(availableQuestions, questionsIdNotHavingCompleteData) {
return this.questionCursor.getQuestions(questionsIdNotHavingCompleteData, this.parentIdentifier).pipe(
switchMap((questionData: any) => {
const fetchedQuestions = questionData.questions;
const allQuestions = _.concat(availableQuestions, fetchedQuestions);
return of({ questions: allQuestions, count: allQuestions.length });
})
);
}


getQuestions(currentIndex?: number, index?: number) {
const sectionChildren = this.sectionConfig?.metadata?.children;
let indentifersForQuestions;
if (currentIndex !== undefined && index) {
indentifersForQuestions = this.identifiers.splice(currentIndex, index);
} else if (!currentIndex && !index) {
indentifersForQuestions = this.identifiers.splice(0, this.threshold);
}
if (!_.isEmpty(indentifersForQuestions)) {
const requests = [];
let requests: any;
const chunkArray = _.chunk(indentifersForQuestions, 10);
_.forEach(chunkArray, (value) => {
requests.push(this.questionCursor.getQuestions(value, this.parentIdentifier));
requests = this.getSectionQuestionData(sectionChildren, value)
});
forkJoin(requests).subscribe(questions => {
_.forEach(questions, (value) => {
Expand All @@ -259,15 +295,26 @@ export class ViewerService {
}

getQuestion() {
const sectionChildren = this.sectionConfig?.metadata?.children;
if (this.identifiers.length) {
let questionIdentifier = this.identifiers.splice(0, this.threshold);
this.questionCursor.getQuestion(questionIdentifier[0]).subscribe((question) => {
this.qumlQuestionEvent.emit(question);
}, (error) => {
this.qumlQuestionEvent.emit({
error: error
const fetchedQuestion = _.find(sectionChildren, (question) => _.includes(questionIdentifier, question.identifier));

if (_.has(fetchedQuestion, 'body')) {
const fetchedQuestionData = {questions: [fetchedQuestion], count: 1 };
const transformedquestionsList = this.transformationService.getTransformedQuestionMetadata(fetchedQuestionData);
this.qumlQuestionEvent.emit(transformedquestionsList);
} else {
this.questionCursor.getQuestion(questionIdentifier[0]).subscribe((question) => {
const fetchedQuestionData = question;
const transformedquestionsList = this.transformationService.getTransformedQuestionMetadata(fetchedQuestionData);
this.qumlQuestionEvent.emit(transformedquestionsList);
}, (error) => {
this.qumlQuestionEvent.emit({
error: error
});
});
});
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion web-component/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@project-sunbird/sunbird-quml-player-web-component",
"version": "3.0.2",
"version": "3.0.3",
"description": "The web component package for the sunbird QuML player",
"main": "sunbird-quml-player.js",
"scripts": {
Expand Down
Loading

0 comments on commit 676553b

Please sign in to comment.