Skip to content

Commit

Permalink
NAS-132767: Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
denysbutenko committed Dec 5, 2024
1 parent c370f4e commit 063dadb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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',
}),
}));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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}`,
Expand Down

0 comments on commit 063dadb

Please sign in to comment.