// SPDX-License-Identifier: Apache-2.0 // Copyright (c) 2018 Georgi Marinov // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // import { TestBed, waitForAsync } from '@angular/core/testing'; import { HttpClientModule } from '@angular/common/http'; import { take } from 'rxjs/operators'; import { SorterService } from '../../services/sorter/sorter.service'; import { SorterServiceFactory } from '../../factories/sorter/sorter.service.factory'; import { SorterKind } from '../../enums/sorter-kind.enum'; import { SortOrder } from '../../enums/sort-order.enum'; import { Go } from '../../enums/go.enum'; import { Indexable } from '../../interfaces/indexable'; import { PersistenceService } from '../../services/persistence/persistence.service'; import { UiService } from '../../services/ui/ui.service'; import { ModelModel } from '../../model/model/model.model'; import { MockDataService } from '../mock-data/mock-data.service'; // eslint-disable-next-line max-lines-per-function describe('SorterService', () => { const sorterService: Indexable = { Accomplishments: {} as SorterService, Publications: {} as SorterService, Spectrum: {} as SorterService, Projects: {} as SorterService }; let uiService: UiService; let persistenceService: PersistenceService; let model: ModelModel; beforeEach(waitForAsync(async () => { TestBed.configureTestingModule({ imports: [HttpClientModule], }).compileComponents(); SorterServiceFactory.SorterKindValues.forEach((sorterKind) => { sorterService[SorterKind[sorterKind]] = TestBed.inject(SorterServiceFactory.InjectionToken(sorterKind, uiService = TestBed.inject(UiService), persistenceService = TestBed.inject(PersistenceService) )); }); model = TestBed.inject(ModelModel); await TestBed.inject(MockDataService).getProjects().pipe(take(1)).subscribe((projects: any) => { model.projects = projects; }); })); it('should be created', () => { Object.values(sorterService).forEach((service) => { expect(service).toBeTruthy(); }); }); it('should check next sort and title methods', () => { Object.values(sorterService).forEach((service) => { expect(() => { let readAll; [undefined, Go.Home, Go.Back, Go.Forward].forEach((_) => { readAll = service.nextSort(new MouseEvent('click'), _); readAll = service.nextSortTitle(_); }); }).not.toThrowError(); }); }); it('should check next potential sort field method', () => { Object.values(sorterService).forEach((service) => { expect(() => { let readAll; const debugService = service as any; [undefined, Go.Home, Go.Back, Go.Forward].forEach((_) => { [SortOrder.Ascending, SortOrder.Descending].forEach((sortOrder) => { if ([SorterKind.Accomplishments].includes(service.sorterKind)) { readAll = debugService.nextPotentialSortField(0, sortOrder, _); readAll = debugService.nextPotentialSortField(0, sortOrder, _); readAll = debugService.nextPotentialSortField(0, sortOrder, _); } }); }); }).not.toThrowError(); }); }); it('should check potential sort field to next adjacent one method', () => { Object.values(sorterService).forEach((service) => { expect(() => { let readAll; const debugService = service as any; [undefined, Go.Home, Go.Back, Go.Forward].forEach((_) => { [SortOrder.Ascending, SortOrder.Descending].forEach((sortOrder) => { readAll = debugService.nudgePotentialSortField(0, sortOrder, _); readAll = debugService.nudgePotentialSortField(0, sortOrder, _); readAll = debugService.nudgePotentialSortField(0, sortOrder, _); }); }); }).not.toThrowError(); }); }); it('should check public interface properties', () => { Object.values(sorterService).forEach((service) => { expect(() => { let readAll; const debugService = service as any; service.sorterKind = service.sorterKind; service.sortFieldIndex = service.sortFieldIndex; service.sortOrder = service.sortOrder; readAll = debugService.defaults; readAll = SorterServiceFactory.SorterKindValues; readAll = SorterServiceFactory.providers; }).not.toThrowError(); }); }); it('should check subSortField public interface properties', () => { Object.values(sorterService).forEach((service) => { expect(() => { let readAll; readAll = service.subSortField; readAll = service.subSortField.full; readAll = service.subSortField.sFull; readAll = service.subSortField.indexFull; readAll = service.subSortField.indexOrderFull; [service.sortOrder, SortOrder.Ascending, SortOrder.Descending].forEach( (_) => { readAll = service.subSortField.orderDirection[_]; }); [Go.Home, Go.Back, Go.Forward].forEach((_) => { readAll = service.subSortField.indexNextDirection[_]; }); readAll = service.subSortField.defaults; service.sorterKind = -1 as SorterKind; readAll = service.subSortField.defaults; }).not.toThrowError(); }); }); it('should check private properties', () => { Object.values(sorterService).forEach((service) => { expect(() => { const debugService = service as any; debugService.sortFields = debugService.sortFields; }).not.toThrowError(); }); }); // eslint-disable-next-line max-lines-per-function it('should check the sorted method', () => { Object.values(sorterService).forEach((service) => { expect(() => { let readAll; const debugService = service as any; const sortField = service.sortField(service.sortFieldIndex); const sortOrder = 2 * service.sortOrder - 1; if (service.sorterKind === SorterKind.Projects) { readAll = service.sorted(null as any); readAll = service.sorted(model.projects); readAll = service.sorted(model.projects, sortField); readAll = service.sorted(model.projects, sortField, sortOrder); readAll = debugService.sortFunctional(model.projects[0], model.projects[0], sortField, sortOrder); readAll = debugService.sortFunctional(model.projects[0], model.projects[1], sortField, sortOrder); readAll = debugService.sortFunctional(model.projects[1], model.projects[0], sortField, sortOrder); } else if (service.sorterKind === SorterKind.Spectrum) { readAll = debugService.sortFunctional( ['some', { Count: 1, Significance: 1 }], ['other', { Count: 2, Significance: 2 }], sortField, sortOrder); } }).not.toThrowError(); }); }); it('should check public interface methods', () => { Object.values(sorterService).forEach((service) => { expect(() => { let readAll; readAll = SorterServiceFactory.tokenDescription(SorterKind.Accomplishments); SorterServiceFactory.SorterKindValues.forEach((sorterKind) => { const deps = [uiService, persistenceService]; readAll = SorterServiceFactory.InjectionToken(sorterKind, deps); readAll = SorterServiceFactory.useFactory(sorterKind, deps); }); readAll = service.sortField(1); readAll = service.sortField(0); readAll = service.sortField(-1); }).not.toThrowError(); }); }); it('should check private methods', () => { Object.values(sorterService).forEach((service) => { expect(() => { let readAll; const debugService = service as any; readAll = debugService.clamp(11, 10); readAll = debugService.clamp(-11, 10); readAll = debugService.clamp(11, 0); }).not.toThrowError(); }); }); });