nickname-manager.component.spec.ts 10.1 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
import { async, ComponentFixture, inject, TestBed } from '@angular/core/testing';
4
import { DomSanitizer } from '@angular/platform-browser';
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 10
import { InfiniteScrollModule } from 'ngx-infinite-scroll';
import { MarkdownService, MarkedOptions } from 'ngx-markdown';
11 12 13
import { JustafewPipeMock } from '../../../../_mocks/_pipes/JustafewPipeMock';
import { TranslatePipeMock } from '../../../../_mocks/_pipes/TranslatePipeMock';
import { TranslateServiceMock } from '../../../../_mocks/_services/TranslateServiceMock';
14
import { jwtOptionsFactory } from '../../../lib/jwt.factory';
15 16 17
import { ConnectionMockService } from '../../../service/connection/connection.mock.service';
import { ConnectionService } from '../../../service/connection/connection.service';
import { FooterBarService } from '../../../service/footer-bar/footer-bar.service';
18 19
import { QuizMockService } from '../../../service/quiz/quiz-mock.service';
import { QuizService } from '../../../service/quiz/quiz.service';
20 21
import { SettingsService } from '../../../service/settings/settings.service';
import { SharedService } from '../../../service/shared/shared.service';
22 23
import { StorageService } from '../../../service/storage/storage.service';
import { StorageServiceMock } from '../../../service/storage/storage.service.mock';
24
import { NicknameManagerComponent } from './nickname-manager.component';
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
25 26 27 28

describe('NicknameManagerComponent', () => {
  let component: NicknameManagerComponent;
  let fixture: ComponentFixture<NicknameManagerComponent>;
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

  const nicknames = {
    disney: [
      'Donald Duck',
      'Daisy Duck',
      'Tarzan',
      'Simba',
      'Elsa',
      'Anna',
      'Kuzco',
      'Arielle',
      'Jasmin',
      'Mulan',
      'Pluto',
      'Nemo',
      'Buzz Lightyear',
      'Woody',
      'Lightning McQueen',
      'Tinkerbell',
      'Peter Pan',
      'Cinderella',
      'Dagobert Duck',
      'Goofy',
    ],
53 54 55 56 57 58 59 60
    science: [],
    fantasy: [],
    literature: [],
    mythology: [],
    actor: [],
    politics: [],
    turing_award: [],
    emojis: [],
61
  };
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
62 63 64

  beforeEach(async(() => {
    TestBed.configureTestingModule({
65
      imports: [
66 67 68 69 70 71
        RouterTestingModule, HttpClientTestingModule, JwtModule.forRoot({
          jwtOptionsProvider: {
            provide: JWT_OPTIONS,
            useFactory: jwtOptionsFactory,
            deps: [PLATFORM_ID],
          },
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
72
        }), InfiniteScrollModule,
73 74
      ],
      providers: [
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
75 76 77 78
        MarkdownService, {
          provide: MarkedOptions,
          useValue: {},
        },
79
        RxStompService,
80
        {
81 82 83
          provide: StorageService,
          useClass: StorageServiceMock,
        }, {
84 85
          provide: QuizService,
          useClass: QuizMockService,
86 87 88
        }, FooterBarService, SettingsService, {
          provide: ConnectionService,
          useClass: ConnectionMockService,
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
89 90 91 92
        }, SharedService, {
          provide: TranslateService,
          useClass: TranslateServiceMock,
        },
93
      ],
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
94
      declarations: [NicknameManagerComponent, TranslatePipeMock, JustafewPipeMock],
95
    }).compileComponents();
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
96 97
  }));

98
  beforeEach(async(() => {
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
99 100
    fixture = TestBed.createComponent(NicknameManagerComponent);
    component = fixture.componentInstance;
101
    component.availableNicks = JSON.parse(JSON.stringify(nicknames));
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
102
    fixture.detectChanges();
103
  }));
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
104

105 106
  beforeEach(inject([QuizService], (quizService: QuizService) => {
    quizService.quiz.sessionConfig.nicks.selectedNicks.splice(0, quizService.quiz.sessionConfig.nicks.selectedNicks.length);
107
  }));
108

109
  it('should be created', async(() => {
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
110
    expect(component).toBeTruthy();
111
  }));
112 113 114 115 116 117 118 119 120 121

  it('should contain a TYPE reference', async(() => {
    expect(NicknameManagerComponent.TYPE).toEqual('NicknameManagerComponent');
  }));

  describe('filterForKeyword', () => {

    afterEach(() => {
      const removeFilterEvent = <any>{ target: { value: '' } };
      component.filterForKeyword(removeFilterEvent);
122
      expect(component.availableNicks.disney.length).toEqual(nicknames.disney.length);
123 124 125 126 127 128 129 130 131 132 133 134 135 136
    });

    it('should filter the nicknames for a given keyword', () => {
      const value = 'Tarzan';
      const event = <any>{ target: { value } };

      component.filterForKeyword(event);
      expect(component.availableNicks.disney.length).toEqual(1);
      expect(component.availableNicks.disney[0]).toEqual(value);
    });
  });

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

137 138
    it('should sanitize the html input', inject([DomSanitizer], (sanitizer: DomSanitizer) => {
      const markup = '<div><span>TestMarkup</span></div>';
139

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
140
      spyOn(sanitizer, 'bypassSecurityTrustHtml').and.callFake(value => value);
141
      component.sanitizeHTML(markup);
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
142
      expect(sanitizer.bypassSecurityTrustHtml).toHaveBeenCalled();
143
    }));
144 145 146 147 148 149
  });

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

    it('should list all available categories', () => {
      const categories = component.availableNickCategories();
150
      expect(categories)
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
151 152
      .toEqual(
        jasmine.arrayWithExactContents(['disney', 'science', 'fantasy', 'literature', 'mythology', 'actor', 'politics', 'turing_award', 'emojis']));
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    });
  });

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

    it('should set a given category as active', () => {
      component.toggleSelectedCategory('disney');
      expect(component.selectedCategory).toEqual('disney');
    });

    it('should not set an invalid category as active', () => {
      component.toggleSelectedCategory('notexisting');
      expect(component.selectedCategory).toEqual('');
    });
  });

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

171
    it('should select a given nickname', inject([QuizService], (quizService: QuizService) => {
172
      const nick = 'Tarzan';
173

174
      component.selectNick(nick);
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
175 176
      quizService.quiz.sessionConfig.nicks.selectedNicks.push(nick);
      expect(quizService.quiz.sessionConfig.nicks.selectedNicks).toContain(nick);
177
    }));
178 179 180 181
  });

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

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
182
    it('should return true if a given nickname has been selected', inject([QuizService], (quizService: QuizService) => {
183 184
      const nick = 'Tarzan';
      component.selectNick(nick);
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
185
      quizService.quiz.sessionConfig.nicks.selectedNicks.push(nick);
186 187 188
      const result = component.hasSelectedNick(nick);

      expect(result).toBeTruthy();
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
189
    }));
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

    it('should return false if nickname which is not selected has been given', () => {
      const nick = 'NotExsting';
      const result = component.hasSelectedNick(nick);

      expect(result).toBeFalsy();
    });
  });

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

    it('should return true if a given category has been selected', () => {
      const category = 'disney';
      component.toggleSelectedCategory(category);
      const result = component.hasSelectedCategory(category);

      expect(result).toBeTruthy();
    });

    it('should return false if a category which is not selected has been given', () => {
      const category = 'NotExisting';
      const result = component.hasSelectedCategory(category);

      expect(result).toBeFalsy();
    });
  });

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

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
219
    it('should return true if all nicks of a category have been selected', inject([QuizService], (quizService: QuizService) => {
220
      spyOnProperty(component, 'selectedCategory').and.returnValue('disney');
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
221 222 223 224
      component.availableNicks.disney.forEach(nick => {
        component.selectNick(nick);
        quizService.quiz.sessionConfig.nicks.selectedNicks.push(nick);
      });
225 226

      expect(component.hasSelectedAllNicks()).toBeTruthy();
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
227
    }));
228

229 230
    it('should return false if not all nicks of a category have been selected', () => {
      spyOnProperty(component, 'selectedCategory').and.returnValue('disney');
231

232 233 234 235
      expect(component.availableNicks.disney).toEqual(jasmine.arrayWithExactContents(nicknames.disney));
      component.selectNick(component.availableNicks.disney[0]);
      expect(component.hasSelectedAllNicks()).toBeFalsy();
    });
236 237 238 239
  });

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

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
240
    it('should return the number of selected nicks of a given category', inject([QuizService], (quizService: QuizService) => {
241
      component.selectNick(component.availableNicks.disney[0]);
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
242
      quizService.quiz.sessionConfig.nicks.selectedNicks.push(component.availableNicks.disney[0]);
243 244 245

      expect(component.getNumberOfSelectedNicksOfCategory('disney')).toEqual(1);
      expect(component.getNumberOfSelectedNicksOfCategory('notSelected')).toEqual(0);
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
246
    }));
247 248 249 250 251 252 253 254 255 256 257 258 259
  });

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

    it('should return the number of available nicks for a given category', () => {

      expect(component.getNumberOfAvailableNicksForCategory('disney')).toEqual(nicknames.disney.length);
      expect(component.getNumberOfAvailableNicksForCategory('notSelected')).toEqual(0);
    });
  });

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

260 261 262
    it('should select all nicks of a given category if not all nicks are selected', inject([QuizService], (quizService: QuizService) => {
      spyOn(component, 'hasSelectedAllNicks').and.callFake(() => false);
      spyOnProperty(component, 'selectedCategory').and.returnValue('disney');
263

264
      component.toggleAllNicks();
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
265
      quizService.quiz.sessionConfig.nicks.selectedNicks.push(...component.availableNicks.disney);
266

267 268 269
      const selectedNicksLength = quizService.quiz.sessionConfig.nicks.selectedNicks.length;
      expect(selectedNicksLength).toEqual(nicknames.disney.length);
    }));
270

271 272 273
    it('should deselect all nicks of a given category if all nicks have been selected', inject([QuizService], (quizService: QuizService) => {
      spyOn(component, 'hasSelectedAllNicks').and.callFake(() => true);
      spyOnProperty(component, 'selectedCategory').and.returnValue('disney');
274

275
      component.toggleAllNicks();
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
276 277
      quizService.quiz.sessionConfig.nicks.selectedNicks = [];

278 279 280
      const selectedNicksLength = quizService.quiz.sessionConfig.nicks.selectedNicks.length;
      expect(selectedNicksLength).toEqual(0);
    }));
281
  });
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
282
});