From 7a71c20293fa66f48f6cd742d20f3a68c45ee599 Mon Sep 17 00:00:00 2001 From: luisa-beerboom <101706784+luisa-beerboom@users.noreply.github.com> Date: Tue, 26 Sep 2023 09:26:27 +0200 Subject: [PATCH] Add tests for main menu service (#2830) --- .../services/main-menu.service.spec.ts | 48 +++++++++++++++++-- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/client/src/app/site/pages/meetings/services/main-menu.service.spec.ts b/client/src/app/site/pages/meetings/services/main-menu.service.spec.ts index 5387619716..76ea3ba5db 100644 --- a/client/src/app/site/pages/meetings/services/main-menu.service.spec.ts +++ b/client/src/app/site/pages/meetings/services/main-menu.service.spec.ts @@ -1,16 +1,54 @@ import { TestBed } from '@angular/core/testing'; +import { firstValueFrom, skip } from 'rxjs'; -import { MainMenuService } from './main-menu.service'; +import { MainMenuEntry, MainMenuService } from './main-menu.service'; -xdescribe(`MainMenuService`, () => { +const menuEntries: MainMenuEntry[] = [ + { route: `page1`, displayName: `A page`, icon: `add`, weight: 300 }, + { route: `start`, displayName: `Start page`, icon: `start`, weight: 100 }, + { route: `page2`, displayName: `Another page`, icon: `search`, weight: 200 } +]; + +function getNumberOfTogglesPromise(service: MainMenuService, requiredToggleNumber: number): Promise { + return firstValueFrom(service.toggleMenuSubject.pipe(skip(requiredToggleNumber - 1))); +} + +describe(`MainMenuService`, () => { let service: MainMenuService; beforeEach(() => { - TestBed.configureTestingModule({}); + TestBed.configureTestingModule({ + providers: [MainMenuService] + }); + service = TestBed.inject(MainMenuService); }); - it(`should be created`, () => { - expect(service).toBeTruthy(); + it(`test registerEntries method`, () => { + service.registerEntries([menuEntries[0]]); + expect(service.entries).toEqual([menuEntries[0]]); + }); + + it(`test registerEntries method with multiple entries at once`, () => { + service.registerEntries(menuEntries.slice(1)); + expect(service.entries).toEqual(menuEntries.slice(1)); + }); + + it(`test consecutive calls of registerEntries method`, () => { + service.registerEntries(menuEntries.slice(0, 2)); + service.registerEntries([menuEntries[2]]); + expect(service.entries).toEqual(menuEntries.sort((a, b) => a.weight - b.weight)); }); + + for (let i = 1; i <= 5; i++) { + it(`test toggleSubject with ${i} call(s)`, async () => { + const promise1 = getNumberOfTogglesPromise(service, i); + const promise2 = getNumberOfTogglesPromise(service, i + 1); + for (let j = 0; j < i; j++) { + service.toggleMenu(); + } + await expectAsync(promise1).toBeResolved(); + await expectAsync(promise2).toBePending(); + }); + } });