Skip to content

Commit

Permalink
NAS-123538: Adding tests to helpers (#8598)
Browse files Browse the repository at this point in the history
  • Loading branch information
undsoft authored Aug 14, 2023
1 parent b9ea785 commit a182817
Show file tree
Hide file tree
Showing 74 changed files with 191 additions and 228 deletions.
1 change: 0 additions & 1 deletion src/app/constants/catalog.constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export const officialCatalog = 'TRUENAS';
export const ixChartApp = 'ix-chart';
export const chartsTrain = 'charts';
export const latestVersion = 'latest';
export const appImagePlaceholder = 'assets/images/truenas_scale_ondark_favicon.png';
5 changes: 0 additions & 5 deletions src/app/helpers/get-unique-id.helper.ts

This file was deleted.

65 changes: 65 additions & 0 deletions src/app/helpers/operators/options.operators.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { lastValueFrom, of } from 'rxjs';
import {
arrayToOptions,
choicesToOptions,
idNameArrayToOptions,
singleArrayToOptions,
} from 'app/helpers/operators/options.operators';

describe('choicesToOptions', () => {
it('converts key-value objects to an array of options', async () => {
const choices = {
key1: 'label1',
key2: 'label2',
};

const options = await lastValueFrom(of(choices).pipe(choicesToOptions()));

expect(options).toEqual([
{ label: 'label1', value: 'key1' },
{ label: 'label2', value: 'key2' },
]);
});
});

describe('arrayToOptions', () => {
it('converts array of labels and values to array of options', async () => {
const array = [['value1', 'label1'], ['value2', 'label2']];

const options = await lastValueFrom(of(array).pipe(arrayToOptions()));

expect(options).toEqual([
{ label: 'label1', value: 'value1' },
{ label: 'label2', value: 'value2' },
]);
});
});

describe('singleArrayToOptions', () => {
it('converts array of strings to an array of options', async () => {
const array = ['value1', 'value2'];

const options = await lastValueFrom(of(array).pipe(singleArrayToOptions()));

expect(options).toEqual([
{ label: 'value1', value: 'value1' },
{ label: 'value2', value: 'value2' },
]);
});
});

describe('idNameArrayToOptions', () => {
it('converts array of objects with id and name to an array of options', async () => {
const array = [
{ id: 1, name: 'name1' },
{ id: 2, name: 'name2' },
];

const options = await lastValueFrom(of(array).pipe(idNameArrayToOptions()));

expect(options).toEqual([
{ label: 'name1', value: 1 },
{ label: 'name2', value: 2 },
]);
});
});
32 changes: 32 additions & 0 deletions src/app/helpers/operators/options.operators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { OperatorFunction } from 'rxjs';
import { map } from 'rxjs/operators';
import { Choices } from 'app/interfaces/choices.interface';
import { MapOption, Option } from 'app/interfaces/option.interface';

/**
* Convert choices to options
* @returns Option[]
*/
export function choicesToOptions(): OperatorFunction<Choices, Option[]> {
return map((choices) => {
return Object.entries(choices).map(([value, label]) => ({ label, value }));
});
}

export function arrayToOptions(): OperatorFunction<MapOption[], Option[]> {
return map((choices) => {
return choices.map(([value, label]) => ({ label, value }));
});
}

export function singleArrayToOptions(): OperatorFunction<(string | number)[], Option[]> {
return map((choices) => {
return choices.map((choice) => ({ label: String(choice), value: choice }));
});
}

export function idNameArrayToOptions(): OperatorFunction<{ id: number; name: string }[], Option[]> {
return map((options) => {
return options.map((option) => ({ label: option.name, value: option.id }));
});
}
File renamed without changes.
23 changes: 23 additions & 0 deletions src/app/helpers/options.helper.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { TranslateService } from '@ngx-translate/core';
import { mapToOptions } from 'app/helpers/options.helper';

describe('mapToOptions', () => {
it('converts JS Map to an array of options while invoking translation on labels', () => {
const translate = {
instant: jest.fn((label) => label) as TranslateService['instant'],
} as TranslateService;

const map = new Map([
['key1', 'label1'],
['key2', 'label2'],
]);

const options = mapToOptions(map, translate);

expect(options).toEqual([
{ label: 'label1', value: 'key1' },
{ label: 'label2', value: 'key2' },
]);
expect(translate.instant).toHaveBeenCalledTimes(2);
});
});
39 changes: 1 addition & 38 deletions src/app/helpers/options.helper.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,10 @@
import { TranslateService } from '@ngx-translate/core';
import { OperatorFunction } from 'rxjs';
import { map } from 'rxjs/operators';
import { Choices } from 'app/interfaces/choices.interface';
import { MapOption, Option } from 'app/interfaces/option.interface';
import { Option } from 'app/interfaces/option.interface';

export function mapToOptions(optionMap: Map<string, string>, translate: TranslateService): Option[] {
return Array.from(optionMap.entries()).map(([value, label]) => ({ label: translate.instant(label), value }));
}

/**
* Convert choices to options
* @returns Option[]
*/
export function choicesToOptions(): OperatorFunction<Choices, Option[]> {
return map((choices) => {
return Object.entries(choices).map(([value, label]) => ({ label, value }));
});
}

export function arrayToOptions(): OperatorFunction<MapOption[], Option[]> {
return map((choices) => {
return choices.map(([value, label]) => ({ label, value }));
});
}

export function singleArrayToOptions(): OperatorFunction<(string | number)[], Option[]> {
return map((choices) => {
return choices.map((choice) => ({ label: String(choice), value: choice }));
});
}

export function idNameArrayToOptions(): OperatorFunction<{ id: number; name: string }[], Option[]> {
return map((options) => {
return options.map((option) => ({ label: option.name, value: option.id }));
});
}

export function tagArrayToOptions(): OperatorFunction<{ tag: number }[], Option[]> {
return map((options) => {
return options.map((option) => ({ label: String(option.tag), value: option.tag }));
});
}

/**
* @usage
* valueToLabel(options)(value)
Expand Down
9 changes: 0 additions & 9 deletions src/app/interfaces/application.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,3 @@ export interface UpgradeSummary {
latest_human_version: string;
upgrade_human_version: string;
}

export enum ApplicationUserEventName {
SwitchTab = 'SwitchTab',
}

export interface ApplicationUserEvent {
name: ApplicationUserEventName;
value: boolean | /* tab index */ number;
}
2 changes: 0 additions & 2 deletions src/app/interfaces/pool.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ export interface UpdatePoolTopology {
spares?: string[];
}

export type UpdatePoolTopologyGroup = keyof UpdatePoolTopology;

export interface PoolAttachParams {
target_vdev?: string;
new_disk?: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
Component, EventEmitter, Input, OnInit, Output,
} from '@angular/core';
import { getUniqueId } from 'app/helpers/get-unique-id.helper';
import { ToolbarMenuOption } from 'app/modules/entity/entity-toolbar/components/toolbar-multimenu/toolbar-menu-option.interface';

@Component({
Expand All @@ -18,7 +17,6 @@ export class ToolbarMultimenuComponent implements OnInit {
allSelected = false;
values: ToolbarMenuOption[] = [];
selectStates: boolean [] = [];
id = getUniqueId();

ngOnInit(): void {
this.selectStates.length = this.options.length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
debounceTime, filter, map, switchMap, take,
} from 'rxjs/operators';
import { allCommands } from 'app/constants/all-commands.constant';
import { choicesToOptions } from 'app/helpers/options.helper';
import { choicesToOptions } from 'app/helpers/operators/options.operators';
import helptext from 'app/helptext/account/user-form';
import { Option } from 'app/interfaces/option.interface';
import { User, UserUpdate } from 'app/interfaces/user.interface';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
BehaviorSubject,
debounceTime, distinctUntilChanged, filter, map, Observable, of, take, tap,
} from 'rxjs';
import { singleArrayToOptions } from 'app/helpers/options.helper';
import { singleArrayToOptions } from 'app/helpers/operators/options.operators';
import helptext from 'app/helptext/apps/apps';
import { AppsFiltersSort } from 'app/interfaces/apps-filters-values.interface';
import { Option } from 'app/interfaces/option.interface';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
catchError, filter, map, switchMap, tap,
} from 'rxjs/operators';
import { JobState } from 'app/enums/job-state.enum';
import { choicesToOptions } from 'app/helpers/options.helper';
import { choicesToOptions } from 'app/helpers/operators/options.operators';
import helptext from 'app/helptext/apps/apps';
import { KubernetesConfig, KubernetesConfigUpdate } from 'app/interfaces/kubernetes-config.interface';
import { WebsocketError } from 'app/interfaces/websocket-error.interface';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
import {
BaseProviderFormComponent,
} from 'app/pages/credentials/backup-credentials/cloud-credentials-form/provider-forms/base-provider-form';
import { DialogService } from 'app/services/dialog.service';
import { ErrorHandlerService } from 'app/services/error-handler.service';
import { WebSocketService } from 'app/services/ws.service';

Expand Down Expand Up @@ -69,7 +68,6 @@ export class OneDriveProviderFormComponent extends BaseProviderFormComponent imp
private errorHandler: ErrorHandlerService,
private formBuilder: FormBuilder,
private ws: WebSocketService,
private dialogService: DialogService,
) {
super();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { TranslateService } from '@ngx-translate/core';
import { BehaviorSubject, Observable, of } from 'rxjs';
import { map, switchMap, tap } from 'rxjs/operators';
import { KeychainCredentialType } from 'app/enums/keychain-credential-type.enum';
import { idNameArrayToOptions } from 'app/helpers/options.helper';
import { idNameArrayToOptions } from 'app/helpers/operators/options.operators';
import { helptextSystemCloudcredentials as helptext } from 'app/helptext/system/cloud-credentials';
import { CloudCredential } from 'app/interfaces/cloud-sync-task.interface';
import { Option } from 'app/interfaces/option.interface';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { TranslateService } from '@ngx-translate/core';
import { Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
import { SshConnectionsSetupMethod } from 'app/enums/ssh-connections-setup-method.enum';
import { idNameArrayToOptions } from 'app/helpers/options.helper';
import { idNameArrayToOptions } from 'app/helpers/operators/options.operators';
import helptext from 'app/helptext/system/ssh-connections';
import {
KeychainCredentialUpdate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { MatDialog } from '@angular/material/dialog';
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
import { TranslateService } from '@ngx-translate/core';
import { CertificateCreateType } from 'app/enums/certificate-create-type.enum';
import { choicesToOptions, idNameArrayToOptions } from 'app/helpers/options.helper';
import { choicesToOptions, idNameArrayToOptions } from 'app/helpers/operators/options.operators';
import { helptextSystemCertificates } from 'app/helptext/system/certificates';
import { Certificate } from 'app/interfaces/certificate.interface';
import { WebsocketError } from 'app/interfaces/websocket-error.interface';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { TranslateService } from '@ngx-translate/core';
import _ from 'lodash';
import { of } from 'rxjs';
import { ExtendedKeyUsageFlag } from 'app/enums/extended-key-usage-flag.enum';
import { choicesToOptions, findLabelsByValue } from 'app/helpers/options.helper';
import { choicesToOptions } from 'app/helpers/operators/options.operators';
import { findLabelsByValue } from 'app/helpers/options.helper';
import { translateOptions } from 'app/helpers/translate.helper';
import { helptextSystemCertificates } from 'app/helptext/system/certificates';
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
certificateKeyLengths,
} from 'app/enums/certificate-digest-algorithm.enum';
import { CertificateKeyType, certificateKeyTypeLabels } from 'app/enums/certificate-key-type.enum';
import { choicesToOptions, idNameArrayToOptions, mapToOptions } from 'app/helpers/options.helper';
import { choicesToOptions, idNameArrayToOptions } from 'app/helpers/operators/options.operators';
import { mapToOptions } from 'app/helpers/options.helper';
import { helptextSystemCertificates } from 'app/helptext/system/certificates';
import { Option } from 'app/interfaces/option.interface';
import { SummaryProvider, SummarySection } from 'app/modules/common/summary/summary.interface';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ChangeDetectionStrategy, Component } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';
import { TranslateService } from '@ngx-translate/core';
import _ from 'lodash';
import { choicesToOptions } from 'app/helpers/options.helper';
import { choicesToOptions } from 'app/helpers/operators/options.operators';
import { helptextSystemCertificates } from 'app/helptext/system/certificates';
import { SummaryProvider, SummarySection } from 'app/modules/common/summary/summary.interface';
import { SystemGeneralService } from 'app/services/system-general.service';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FormBuilder, Validators } from '@angular/forms';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
import { TranslateService } from '@ngx-translate/core';
import { idNameArrayToOptions } from 'app/helpers/options.helper';
import { idNameArrayToOptions } from 'app/helpers/operators/options.operators';
import { helptextSystemCa } from 'app/helptext/system/ca';
import { CertificateAuthoritySignRequest } from 'app/interfaces/certificate-authority.interface';
import { FormErrorHandlerService } from 'app/modules/ix-forms/services/form-error-handler.service';
Expand Down
2 changes: 1 addition & 1 deletion src/app/pages/credentials/kmip/kmip.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { MatDialog } from '@angular/material/dialog';
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
import { TranslateService } from '@ngx-translate/core';
import { forkJoin } from 'rxjs';
import { idNameArrayToOptions } from 'app/helpers/options.helper';
import { idNameArrayToOptions } from 'app/helpers/operators/options.operators';
import { helptextSystemKmip } from 'app/helptext/system/kmip';
import { KmipConfigUpdate } from 'app/interfaces/kmip-config.interface';
import { WebsocketError } from 'app/interfaces/websocket-error.interface';
Expand Down
Loading

0 comments on commit a182817

Please sign in to comment.