quiz-overview.component.spec.ts 6.8 KB
Newer Older
1
import { HttpClientTestingModule } from '@angular/common/http/testing';
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
2
import { PLATFORM_ID } from '@angular/core';
3 4
import { async, ComponentFixture, inject, TestBed } from '@angular/core/testing';
import { Router } from '@angular/router';
5
import { RouterTestingModule } from '@angular/router/testing';
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
6
import { JWT_OPTIONS, JwtModule } from '@auth0/angular-jwt';
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
7 8
import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
import { TranslateService } from '@ngx-translate/core';
9
import { RxStompService } from '@stomp/ng2-stompjs';
10 11
import { TranslatePipeMock } from '../../../_mocks/_pipes/TranslatePipeMock';
import { TranslateServiceMock } from '../../../_mocks/_services/TranslateServiceMock';
12 13 14 15 16 17
import { DefaultSettings } from '../../lib/default.settings';
import { DefaultAnswerEntity } from '../../lib/entities/answer/DefaultAnswerEntity';
import { SingleChoiceQuestionEntity } from '../../lib/entities/question/SingleChoiceQuestionEntity';
import { QuizEntity } from '../../lib/entities/QuizEntity';
import { SessionConfigurationEntity } from '../../lib/entities/session-configuration/SessionConfigurationEntity';
import { jwtOptionsFactory } from '../../lib/jwt.factory';
18 19 20 21
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';
22 23
import { QuizMockService } from '../../service/quiz/quiz-mock.service';
import { QuizService } from '../../service/quiz/quiz.service';
24 25
import { SettingsService } from '../../service/settings/settings.service';
import { SharedService } from '../../service/shared/shared.service';
26
import { StorageService } from '../../service/storage/storage.service';
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
27
import { StorageServiceMock } from '../../service/storage/storage.service.mock';
28 29
import { TrackingMockService } from '../../service/tracking/tracking.mock.service';
import { TrackingService } from '../../service/tracking/tracking.service';
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
30
import { UserService } from '../../service/user/user.service';
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
31

32
import { QuizOverviewComponent } from './quiz-overview.component';
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
33

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
34
describe('QuizOverviewComponent', () => {
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
35 36
  let component: QuizOverviewComponent;
  let fixture: ComponentFixture<QuizOverviewComponent>;
37

38 39
  const validQuiz = new QuizEntity({
    name: 'validtestquiz',
40
    sessionConfig: DefaultSettings.defaultQuizSettings.sessionConfig,
41
    questionList: [
42
      new SingleChoiceQuestionEntity({
43 44 45 46 47
        questionText: 'testtext',
        timer: 20,
        displayAnswerText: true,
        showOneAnswerPerRow: false,
        answerOptionList: [
48
          new DefaultAnswerEntity({
49 50
            answerText: 'answer1',
            isCorrect: true,
51
          }), new DefaultAnswerEntity({
52 53 54
            answerText: 'answer2',
            isCorrect: false,
          }),
55 56 57 58
        ],
      }),
    ],
  });
59 60
  const invalidQuiz = new QuizEntity({
    name: 'invalidtestquiz',
61
    sessionConfig: new SessionConfigurationEntity(DefaultSettings.defaultQuizSettings.sessionConfig),
62
    questionList: [
63
      new SingleChoiceQuestionEntity({
64
        answerOptionList: [
65
          new DefaultAnswerEntity({
66 67
            answerText: 'answer1',
            isCorrect: true,
68
          }), new DefaultAnswerEntity({
69 70 71
            answerText: 'answer2',
            isCorrect: true,
          }),
72 73 74 75
        ],
      }),
    ],
  });
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
76 77 78

  beforeEach(async(() => {
    TestBed.configureTestingModule({
79
      imports: [
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
80 81 82 83
        JwtModule.forRoot({
          jwtOptionsProvider: {
            provide: JWT_OPTIONS,
            useFactory: jwtOptionsFactory,
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
84
            deps: [PLATFORM_ID, StorageService],
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
85
          },
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
86
        }), RouterTestingModule, HttpClientTestingModule, FontAwesomeModule,
87 88
      ],
      providers: [
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
89 90 91 92 93
        {
          provide: UserService,
          useValue: {
            isAuthorizedFor: () => true,
          },
94
        }, {
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
95 96 97
          provide: StorageService,
          useClass: StorageServiceMock,
        }, HeaderLabelService, {
98 99
          provide: QuizService,
          useClass: QuizMockService,
100 101 102 103 104 105
        }, {
          provide: TrackingService,
          useClass: TrackingMockService,
        }, FooterBarService, SettingsService, {
          provide: ConnectionService,
          useClass: ConnectionMockService,
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
106 107 108
        }, SharedService, {
          provide: TranslateService,
          useClass: TranslateServiceMock,
109
        }, RxStompService,
110
      ],
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
111
      declarations: [QuizOverviewComponent, TranslatePipeMock],
112
    }).compileComponents();
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
113 114
  }));

115
  beforeEach(async(() => {
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
116 117 118
    fixture = TestBed.createComponent(QuizOverviewComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
119
  }));
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
120

121
  it('should be created', async(() => {
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
122
    expect(component).toBeTruthy();
123
  }));
124 125 126 127 128 129
  it('should contain a TYPE reference', async(() => {
    expect(QuizOverviewComponent.TYPE).toEqual('QuizOverviewComponent');
  }));

  describe('#startQuiz', () => {

130
    it('should start the quiz', inject([QuizService, Router], (quizService: QuizService, router: Router) => {
131

132
      const quizName = 'validtestquiz';
133

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
134
      spyOn(router, 'navigate').and.callFake(() => new Promise<boolean>(resolve => {resolve(); }));
135

136
      component.startQuiz(validQuiz).then(() => {
137
        expect(quizService.quiz).toEqual(jasmine.objectContaining(validQuiz));
138 139 140
        expect(router.navigate).toHaveBeenCalledWith(jasmine.arrayWithExactContents(['/quiz', 'flow']));
      });
    }));
141 142 143 144
  });

  describe('#editQuiz', () => {

145
    it('should redirect to the quiz manager', inject([QuizService, Router], (quizService: QuizService, router: Router) => {
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
146
      spyOn(router, 'navigate').and.callFake(() => new Promise<boolean>(resolve => {resolve(); }));
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
147
      component.sessions.splice(0, -1, validQuiz);
148

149
      component.editQuiz(0);
150

151
      expect(quizService.quiz).toEqual(jasmine.objectContaining(validQuiz));
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
152
      expect(router.navigate).toHaveBeenCalledWith(jasmine.arrayWithExactContents(['/quiz', 'manager', 'overview']));
153

154
    }));
155 156 157 158
  });

  describe('#exportQuiz', () => {

159
    it('should generate an export file containing the quiz data', async () => {
160
      component.sessions.push(validQuiz);
161
      const exportData = 'data:text/json;charset=utf-8,' + encodeURIComponent(JSON.stringify(validQuiz));
162

163
      await component.exportQuiz(0, (self, event) => {
164 165 166 167 168 169 170 171 172
        event.preventDefault();
        event.stopImmediatePropagation();
        expect(self.href).toEqual(exportData);
      });
    });
  });

  describe('#deleteQuiz', () => {

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
173
    it('should return null if the quiz does not exist', inject([StorageService], (storageService: StorageService) => {
174 175
      const quizName = 'validtestquiz';

176
      component.deleteQuiz(validQuiz);
177
      storageService.db.Quiz.get(quizName).then(quiz => {
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
178
        expect(quiz).toBe(undefined);
179 180
      });
    }));
181 182
  });

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
183
});