quiz-results.component.spec.ts 10.8 KB
Newer Older
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
1
import { HttpClientTestingModule } from '@angular/common/http/testing';
2
import { PLATFORM_ID } from '@angular/core';
3
import { async, ComponentFixture, inject, TestBed } from '@angular/core/testing';
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
4
import { BrowserDynamicTestingModule } from '@angular/platform-browser-dynamic/testing';
5
import { RouterTestingModule } from '@angular/router/testing';
6
import { JWT_OPTIONS, JwtModule } from '@auth0/angular-jwt';
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
7
import { TranslateService } from '@ngx-translate/core';
8
import { RxStompService } from '@stomp/ng2-stompjs';
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
9
import { SimpleMQ } from 'ng2-simple-mq';
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
10
import { MarkdownService, MarkedOptions } from 'ngx-markdown';
11
import { TranslateServiceMock } from '../../../../_mocks/_services/TranslateServiceMock';
12
import { Attendee } from '../../../lib/attendee/attendee';
13
import { SurveyQuestionEntity } from '../../../lib/entities/question/SurveyQuestionEntity';
14
import { Language } from '../../../lib/enums/enums';
15
import { jwtOptionsFactory } from '../../../lib/jwt.factory';
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
16
import { ServerUnavailableModalComponent } from '../../../modals/server-unavailable-modal/server-unavailable-modal.component';
17 18 19 20 21 22
import { AttendeeMockService } from '../../../service/attendee/attendee.mock.service';
import { AttendeeService } from '../../../service/attendee/attendee.service';
import { ConnectionMockService } from '../../../service/connection/connection.mock.service';
import { ConnectionService } from '../../../service/connection/connection.service';
import { FooterBarService } from '../../../service/footer-bar/footer-bar.service';
import { HeaderLabelService } from '../../../service/header-label/header-label.service';
23
import { I18nService } from '../../../service/i18n/i18n.service';
24
import { QuestionTextService } from '../../../service/question-text/question-text.service';
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
25
import { QuizMockService } from '../../../service/quiz/quiz-mock.service';
26
import { QuizService } from '../../../service/quiz/quiz.service';
27 28
import { SettingsService } from '../../../service/settings/settings.service';
import { SharedService } from '../../../service/shared/shared.service';
29 30
import { StorageService } from '../../../service/storage/storage.service';
import { StorageServiceMock } from '../../../service/storage/storage.service.mock';
31
import { SharedModule } from '../../../shared/shared.module';
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
32
import { VotingQuestionComponent } from '../voting/voting-question/voting-question.component';
33
import { ConfidenceRateComponent } from './confidence-rate/confidence-rate.component';
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
34
import { ProgressBarAnonymousComponent } from './progress-bar/progress-bar-anonymous/progress-bar-anonymous.component';
35 36 37 38 39 40 41 42
import { ProgressBarFreetextComponent } from './progress-bar/progress-bar-freetext/progress-bar-freetext.component';
import { ProgressBarMultipleChoiceComponent } from './progress-bar/progress-bar-multiple-choice/progress-bar-multiple-choice.component';
import { ProgressBarRangedComponent } from './progress-bar/progress-bar-ranged/progress-bar-ranged.component';
import { ProgressBarSingleChoiceComponent } from './progress-bar/progress-bar-single-choice/progress-bar-single-choice.component';
import { ProgressBarSurveyComponent } from './progress-bar/progress-bar-survey/progress-bar-survey.component';
import { ProgressBarComponent } from './progress-bar/progress-bar.component';

import { QuizResultsComponent } from './quiz-results.component';
43
import { ReadingConfirmationProgressComponent } from './reading-confirmation-progress/reading-confirmation-progress.component';
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
44 45 46 47 48 49 50

describe('QuizResultsComponent', () => {
  let component: QuizResultsComponent;
  let fixture: ComponentFixture<QuizResultsComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
51
      imports: [
52 53 54 55 56 57
        SharedModule, RouterTestingModule, JwtModule.forRoot({
          jwtOptionsProvider: {
            provide: JWT_OPTIONS,
            useFactory: jwtOptionsFactory,
            deps: [PLATFORM_ID],
          },
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
58
        }), HttpClientTestingModule,
59 60
      ],
      providers: [
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
61 62 63 64
        MarkdownService, {
          provide: MarkedOptions,
          useValue: {},
        }, RxStompService, {
65 66 67
          provide: StorageService,
          useClass: StorageServiceMock,
        }, TranslateService, FooterBarService, SettingsService, {
68 69 70
          provide: ConnectionService,
          useClass: ConnectionMockService,
        }, {
71
          provide: QuizService,
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
72
          useClass: QuizMockService,
73 74 75
        }, SharedService, {
          provide: AttendeeService,
          useClass: AttendeeMockService,
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
76 77 78 79
        }, HeaderLabelService, I18nService, QuestionTextService, {
          provide: TranslateService,
          useClass: TranslateServiceMock,
        }, SimpleMQ,
80 81 82 83 84 85
      ],
      declarations: [
        ConfidenceRateComponent,
        ProgressBarComponent,
        ProgressBarSingleChoiceComponent,
        ProgressBarMultipleChoiceComponent,
86 87 88 89
        ProgressBarSurveyComponent,
        ProgressBarRangedComponent,
        ProgressBarFreetextComponent,
        ReadingConfirmationProgressComponent,
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
90
        QuizResultsComponent, ProgressBarAnonymousComponent, VotingQuestionComponent, ServerUnavailableModalComponent,
91
      ],
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
92
    }).overrideModule(BrowserDynamicTestingModule, { set: { entryComponents: [ServerUnavailableModalComponent] } }).compileComponents();
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
93 94
  }));

95
  beforeEach(async(inject([QuizService], (quizService: QuizService) => {
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
96 97 98
    fixture = TestBed.createComponent(QuizResultsComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
99

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
100
    quizService.quiz.currentQuestionIndex = 0;
101 102 103 104 105 106 107 108 109 110
  })));

  it(`should be created`, () => {
    expect(component).toBeTruthy();
  });

  it(`should container a TYPE reference`, () => {
    expect(QuizResultsComponent.TYPE).toEqual('QuizResultsComponent');
  });

111
  it(`#showLeaderBoardButton`, inject([QuizService], (quizService: QuizService) => {
112
    expect(quizService.quiz.questionList[quizService.quiz.currentQuestionIndex] instanceof SurveyQuestionEntity).toBeFalsy();
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
113
    expect(component.showLeaderBoardButton(quizService.quiz.currentQuestionIndex)).toBeTruthy();
114
  }));
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
115

116
  it(`#showStopQuizButton`, inject([QuizService, AttendeeService], (quizService: QuizService, attendeeService: AttendeeService) => {
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
117
    quizService.isOwner = true;
118 119
    quizService.currentQuestion().timer = 0;
    attendeeService.addMember(new Attendee({
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
120
      id: '',
121 122 123 124
      name: 'testNickname',
      groupName: 'Default',
      colorCode: '#00000',
      responses: [],
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
125 126
      currentQuizName: '',
      ticket: '',
127 128
    }));

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
129
    expect(component.showStopQuizButton).toBeFalsy();
130
  }));
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
131

132 133 134 135
  it(`#showStopCountdownButton`, inject([QuizService, AttendeeService], (quizService: QuizService, attendeeService: AttendeeService) => {
    quizService.currentQuestion().timer = 10;
    quizService['_isOwner'] = true;
    attendeeService.addMember(new Attendee({
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
136
      id: '',
137 138 139 140
      name: 'testNickname',
      groupName: 'Default',
      colorCode: '#00000',
      responses: [],
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
141 142
      currentQuizName: '',
      ticket: '',
143
    }));
144
    component.countdown = quizService.currentQuestion().timer;
145

146
    expect(component.countdown).toBeTruthy();
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
147
    expect(component.showStopCountdownButton).toBeFalsy();
148
  }));
149

150
  it(`#showStartQuizButton`, inject([QuizService], (quizService: QuizService) => {
151 152
    quizService['_isOwner'] = true;
    quizService.readingConfirmationRequested = true;
153

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
154
    expect(component.showStartQuizButton).toBeFalsy();
155
  }));
156

157 158
  it(`#hideProgressbarCssStyle`, inject([QuizService], (quizService: QuizService) => {
    quizService.readingConfirmationRequested = false;
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
159
    quizService.quiz.currentQuestionIndex = 0;
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
160
    expect(component.hideProgressbarStyle).toBeTruthy();
161
  }));
162

163 164
  it(`#showConfidenceRate`, inject([QuizService, AttendeeService], (quizService: QuizService, attendeeService: AttendeeService) => {
    attendeeService.addMember(new Attendee({
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
165
      id: '',
166 167 168
      name: 'testNickname',
      groupName: 'Default',
      colorCode: '#00000',
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
169
      currentQuizName: '',
170 171
      responses: [
        {
172
          value: '0',
173 174 175 176 177
          confidence: 20,
          readingConfirmation: true,
          responseTime: 10,
        },
      ],
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
178
    }));
179 180
    quizService.quiz.sessionConfig.confidenceSliderEnabled = true;

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
181
    expect(component.showConfidenceRate(0)).toBeFalsy();
182
  }));
183

184
  it(`#modifyVisibleQuestion`, inject([QuestionTextService], async (questionTextService: QuestionTextService) => {
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
185
    spyOn(questionTextService, 'changeMultiple').and.callFake(() => new Promise<void>(resolve => resolve()));
186

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
187
    await component.modifyVisibleQuestion(0);
188 189
    expect(questionTextService.changeMultiple).toHaveBeenCalled();
  }));
190

191 192
  it(`#getConfidenceData`, inject([AttendeeService, I18nService], (attendeeService: AttendeeService, i18nService: I18nService) => {
    attendeeService.addMember(new Attendee({
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
193
      id: '',
194 195 196
      name: 'testNickname',
      groupName: 'Default',
      colorCode: '#00000',
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
197
      currentQuizName: '',
198 199
      responses: [
        {
200
          value: '0',
201 202 203 204 205 206
          confidence: 20,
          readingConfirmation: true,
          responseTime: 10,
        },
      ],
    }));
207

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
208
    const result = component.getConfidenceData(0);
209 210
    expect(result.base).toEqual(1);
    expect(result.absolute).toEqual(1);
211 212 213 214
    if (i18nService.currentLanguage === Language.DE) {
      expect(result.percent).toEqual('20\xa0%');
    } else {
      expect(result.percent).toEqual('20%');
215
    }
216
  }));
217

218 219
  it(`#showReadingConfirmation`, inject([QuizService, AttendeeService], (quizService: QuizService, attendeeService: AttendeeService) => {
    attendeeService.addMember(new Attendee({
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
220
      id: '',
221 222 223
      name: 'testNickname',
      groupName: 'Default',
      colorCode: '#00000',
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
224
      currentQuizName: '',
225 226
      responses: [
        {
227
          value: '0',
228 229 230 231 232
          confidence: 20,
          readingConfirmation: true,
          responseTime: 10,
        },
      ],
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
233
    }));
234 235
    quizService.quiz.sessionConfig.readingConfirmationEnabled = true;

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
236
    expect(component.showReadingConfirmation(0)).toBeFalsy();
237
  }));
238

239
  it(`#showResponseProgress`, inject([QuizService], (quizService: QuizService) => {
240

241
    expect(component.showResponseProgress()).toEqual(quizService.quiz.sessionConfig.showResponseProgress);
242
  }));
243

244 245
  it(`#getReadingConfirmationData`, inject([AttendeeService, I18nService], (attendeeService: AttendeeService, i18nService: I18nService) => {
    attendeeService.addMember(new Attendee({
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
246
      id: '',
247 248 249
      name: 'testNickname',
      groupName: 'Default',
      colorCode: '#00000',
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
250
      currentQuizName: '',
251 252
      responses: [
        {
253
          value: '0',
254 255 256 257 258 259
          confidence: 20,
          readingConfirmation: true,
          responseTime: 10,
        },
      ],
    }));
260

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
261
    const result = component.getReadingConfirmationData(0);
262 263
    expect(result.base).toEqual(1);
    expect(result.absolute).toEqual(1);
264 265 266 267
    if (i18nService.currentLanguage === Language.DE) {
      expect(result.percent).toEqual('100\xa0%'); // \xa0: Non breaking space
    } else {
      expect(result.percent).toEqual('100%');
268
    }
269
  }));
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
270
});