11import { ComponentFixture , TestBed , waitForAsync } from '@angular/core/testing' ;
22import { IonicModule } from '@ionic/angular' ;
33import { Router } from '@angular/router' ;
4- import { BehaviorSubject , of } from 'rxjs' ;
4+ import { BehaviorSubject } from 'rxjs' ;
55
66import { QuestionsPage } from './questions.page' ;
77import { QuestionsService } from 'src/app/services/questions.service' ;
@@ -26,7 +26,7 @@ describe('QuestionsPage', () => {
2626 let fixture : ComponentFixture < QuestionsPage > ;
2727
2828 let questionsServiceMock : { questions : BehaviorSubject < Question [ ] > } ;
29- let resultsServiceMock : { results : BehaviorSubject < Results [ ] > ; getPercentById : jasmine . Spy } ;
29+ let resultsServiceMock : { results : BehaviorSubject < Results [ ] > } ;
3030 let routerMock : { navigate : jasmine . Spy } ;
3131
3232 const questions : Question [ ] = [
@@ -39,7 +39,6 @@ describe('QuestionsPage', () => {
3939 questionsServiceMock = { questions : new BehaviorSubject < Question [ ] > ( questions ) } ;
4040 resultsServiceMock = {
4141 results : new BehaviorSubject < Results [ ] > ( [ { id : 1 , correctness : 80 } ] ) ,
42- getPercentById : jasmine . createSpy ( 'getPercentById' ) . and . returnValue ( of ( 80 ) ) ,
4342 } ;
4443 routerMock = { navigate : jasmine . createSpy ( 'navigate' ) } ;
4544
@@ -68,7 +67,36 @@ describe('QuestionsPage', () => {
6867
6968 expect ( component . questions ) . toEqual ( questions ) ;
7069 expect ( component . filteredQuestions ) . toEqual ( questions ) ;
70+ expect ( component . visibleQuestions ) . toEqual ( questions ) ;
7171 expect ( component . results ) . toEqual ( [ { id : 1 , correctness : 80 } ] ) ;
72+ expect ( component . percentById . get ( 1 ) ) . toBe ( 80 ) ;
73+ } ) ;
74+
75+ it ( 'should render only the first batch when there are many questions' , ( ) => {
76+ const manyQuestions = Array . from ( { length : 80 } , ( _ , i ) => makeQuestion ( { id : i + 1 } ) ) ;
77+ questionsServiceMock . questions . next ( manyQuestions ) ;
78+
79+ component . ngOnInit ( ) ;
80+
81+ expect ( component . filteredQuestions . length ) . toBe ( 80 ) ;
82+ expect ( component . visibleQuestions . length ) . toBe ( 30 ) ;
83+ } ) ;
84+ } ) ;
85+
86+ describe ( 'loadMore' , ( ) => {
87+ it ( 'should append the next batch and complete the infinite scroll event' , ( ) => {
88+ const manyQuestions = Array . from ( { length : 80 } , ( _ , i ) => makeQuestion ( { id : i + 1 } ) ) ;
89+ questionsServiceMock . questions . next ( manyQuestions ) ;
90+ component . ngOnInit ( ) ;
91+
92+ const event = { target : { complete : jasmine . createSpy ( 'complete' ) } } ;
93+ component . loadMore ( event as never ) ;
94+
95+ expect ( component . visibleQuestions . length ) . toBe ( 60 ) ;
96+ expect ( event . target . complete ) . toHaveBeenCalled ( ) ;
97+
98+ component . loadMore ( event as never ) ;
99+ expect ( component . visibleQuestions . length ) . toBe ( 80 ) ;
72100 } ) ;
73101 } ) ;
74102
@@ -84,6 +112,7 @@ describe('QuestionsPage', () => {
84112
85113 expect ( component . filteredQuestions . length ) . toBe ( 1 ) ;
86114 expect ( component . filteredQuestions [ 0 ] . id ) . toBe ( 2 ) ;
115+ expect ( component . visibleQuestions ) . toEqual ( component . filteredQuestions ) ;
87116 } ) ;
88117
89118 it ( 'should filter by tag' , ( ) => {
@@ -109,6 +138,7 @@ describe('QuestionsPage', () => {
109138 component . handleInput ( inputEvent ( ' ' ) ) ;
110139
111140 expect ( component . filteredQuestions ) . toEqual ( questions ) ;
141+ expect ( component . visibleQuestions ) . toEqual ( questions ) ;
112142 } ) ;
113143
114144 it ( 'should return an empty list when nothing matches' , ( ) => {
@@ -123,12 +153,11 @@ describe('QuestionsPage', () => {
123153 expect ( component . trackById ( 0 , questions [ 0 ] ) ) . toBe ( 1 ) ;
124154 } ) ;
125155
126- it ( 'getPercentById should delegate to ResultsService' , ( done ) => {
127- component . getPercentById ( 1 ) . subscribe ( percent => {
128- expect ( percent ) . toBe ( 80 ) ;
129- expect ( resultsServiceMock . getPercentById ) . toHaveBeenCalledWith ( 1 ) ;
130- done ( ) ;
131- } ) ;
156+ it ( 'getPercent should return the stored percent or 0' , ( ) => {
157+ component . ngOnInit ( ) ;
158+
159+ expect ( component . getPercent ( 1 ) ) . toBe ( 80 ) ;
160+ expect ( component . getPercent ( 999 ) ) . toBe ( 0 ) ;
132161 } ) ;
133162
134163 it ( 'clickOnQuestion should navigate to question-info with the question id' , ( ) => {
0 commit comments