From 063dadba9f3ed39dcc067b17063f44a72b3c24e1 Mon Sep 17 00:00:00 2001 From: Denys Butenko Date: Thu, 5 Dec 2024 18:36:27 +0700 Subject: [PATCH] NAS-132767: Add unit test --- .../cloudsync-what-and-when.component.spec.ts | 26 +++++++++++++++++++ .../cloudsync-what-and-when.component.ts | 18 ++++++++++--- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/app/pages/data-protection/cloudsync/cloudsync-wizard/steps/cloudsync-what-and-when/cloudsync-what-and-when.component.spec.ts b/src/app/pages/data-protection/cloudsync/cloudsync-wizard/steps/cloudsync-what-and-when/cloudsync-what-and-when.component.spec.ts index 263e936cd03..56c6ebf0ab5 100644 --- a/src/app/pages/data-protection/cloudsync/cloudsync-wizard/steps/cloudsync-what-and-when/cloudsync-what-and-when.component.spec.ts +++ b/src/app/pages/data-protection/cloudsync/cloudsync-wizard/steps/cloudsync-what-and-when/cloudsync-what-and-when.component.spec.ts @@ -9,6 +9,8 @@ import { of } from 'rxjs'; import { mockCall, mockApi } from 'app/core/testing/utils/mock-api.utils'; import { mockAuth } from 'app/core/testing/utils/mock-auth.utils'; import { DialogService } from 'app/modules/dialog/dialog.service'; +import { IxInputHarness } from 'app/modules/forms/ix-forms/components/ix-input/ix-input.harness'; +import { IxSelectHarness } from 'app/modules/forms/ix-forms/components/ix-select/ix-select.harness'; import { IxFormHarness } from 'app/modules/forms/ix-forms/testing/ix-form.harness'; import { ChainedRef } from 'app/modules/slide-ins/chained-component-ref'; import { SlideInRef } from 'app/modules/slide-ins/slide-in-ref'; @@ -132,4 +134,28 @@ describe('CloudSyncWhatAndWhenComponent', () => { }); expect(chainedRef.swap).toHaveBeenCalledWith(CloudSyncFormComponent, true); }); + + it('checks payload when use invalid s3 credentials', async () => { + const bucketSelect = await loader.getHarness(IxSelectHarness.with({ label: 'Bucket' })); + expect(await bucketSelect.getValue()).toBe(''); + + spectator.component.isCredentialInvalid$.next(true); + spectator.detectChanges(); + + const bucketInput = await loader.getHarness(IxInputHarness.with({ label: 'Bucket' })); + await bucketInput.setValue('selected'); + + expect(spectator.component.getPayload()).toEqual(expect.objectContaining({ + attributes: expect.objectContaining({ + bucket: 'selected', + }), + })); + + await bucketInput.setValue('test-bucket'); + expect(spectator.component.getPayload()).toEqual(expect.objectContaining({ + attributes: expect.objectContaining({ + bucket: 'test-bucket', + }), + })); + }); }); diff --git a/src/app/pages/data-protection/cloudsync/cloudsync-wizard/steps/cloudsync-what-and-when/cloudsync-what-and-when.component.ts b/src/app/pages/data-protection/cloudsync/cloudsync-wizard/steps/cloudsync-what-and-when/cloudsync-what-and-when.component.ts index 334439d9322..e46ce7a526e 100644 --- a/src/app/pages/data-protection/cloudsync/cloudsync-wizard/steps/cloudsync-what-and-when/cloudsync-what-and-when.component.ts +++ b/src/app/pages/data-protection/cloudsync/cloudsync-wizard/steps/cloudsync-what-and-when/cloudsync-what-and-when.component.ts @@ -12,6 +12,7 @@ import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy'; import { TranslateService, TranslateModule } from '@ngx-translate/core'; import { find, findIndex, isArray } from 'lodash-es'; import { + BehaviorSubject, EMPTY, Observable, catchError, combineLatest, filter, map, merge, of, tap, } from 'rxjs'; @@ -118,6 +119,7 @@ export class CloudSyncWhatAndWhenComponent implements OnInit, OnChanges { bwlimit: [[] as string[]], }); + isCredentialInvalid$ = new BehaviorSubject(false); credentials: CloudSyncCredential[] = []; providers: CloudSyncProvider[] = []; bucketPlaceholder: string = helptextCloudSync.bucket_placeholder; @@ -440,6 +442,16 @@ export class CloudSyncWhatAndWhenComponent implements OnInit, OnChanges { } }); + this.isCredentialInvalid$.pipe(untilDestroyed(this)).subscribe((value) => { + if (value) { + this.form.controls.bucket_input.enable(); + this.form.controls.bucket.disable(); + } else { + this.form.controls.bucket_input.disable(); + this.form.controls.bucket.enable(); + } + }); + this.form.controls.bucket.valueChanges.pipe( filter((selectedOption) => selectedOption === newOption), untilDestroyed(this), @@ -481,13 +493,11 @@ export class CloudSyncWhatAndWhenComponent implements OnInit, OnChanges { }); } this.bucketOptions$ = of(bucketOptions); - this.form.controls.bucket.enable(); - this.form.controls.bucket_input.disable(); + this.isCredentialInvalid$.next(false); this.cdr.markForCheck(); }, error: (error: ApiError) => { - this.form.controls.bucket.disable(); - this.form.controls.bucket_input.enable(); + this.isCredentialInvalid$.next(true); this.dialog.closeAllDialogs(); this.dialog.confirm({ title: error.extra ? (error.extra as { excerpt: string }).excerpt : `${this.translate.instant('Error: ')}${error.error}`,