Skip to content

Commit

Permalink
Add tests for main menu service (#2830)
Browse files Browse the repository at this point in the history
  • Loading branch information
luisa-beerboom authored Sep 26, 2023
1 parent c063560 commit 7a71c20
Showing 1 changed file with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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<void> {
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();
});
}
});

0 comments on commit 7a71c20

Please sign in to comment.