import { BsModalService } from 'ngx-bootstrap/modal';
import { TooltipModule } from 'ngx-bootstrap/tooltip';
import { ToastrModule } from 'ngx-toastr';
-import { of as observableOf } from 'rxjs';
+import { of as observableOf, throwError } from 'rxjs';
import { configureTestBed, FormHelper, i18nProviders } from '../../../../testing/unit-test-helper';
import { RgwUserService } from '../../../shared/api/rgw-user.service';
});
describe('username validation', () => {
- beforeEach(() => {
- spyOn(rgwUserService, 'enumerate').and.returnValue(observableOf(['abc', 'xyz']));
- });
-
it('should validate that username is required', () => {
formHelper.expectErrorChange('uid', '', 'required', true);
});
it('should validate that username is valid', fakeAsync(() => {
+ spyOn(rgwUserService, 'get').and.returnValue(throwError('foo'));
formHelper.setValue('uid', 'ab', true);
tick(500);
formHelper.expectValid('uid');
}));
it('should validate that username is invalid', fakeAsync(() => {
+ spyOn(rgwUserService, 'get').and.returnValue(observableOf({}));
formHelper.setValue('uid', 'abc', true);
tick(500);
formHelper.expectError('uid', 'notUnique');
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
-import { of as observableOf } from 'rxjs';
+import { of as observableOf, throwError } from 'rxjs';
import { configureTestBed } from '../../../testing/unit-test-helper';
import { RgwUserService } from './rgw-user.service';
expect(req.request.method).toBe('DELETE');
});
- it('should call exists with an existent uid', () => {
- spyOn(service, 'enumerate').and.returnValue(observableOf(['foo', 'bar']));
- let result;
+ it('should call exists with an existent uid', (done) => {
+ spyOn(service, 'get').and.returnValue(observableOf({}));
service.exists('foo').subscribe((res) => {
- result = res;
+ expect(res).toBe(true);
+ done();
});
- expect(result).toBe(true);
});
- it('should call exists with a non existent uid', () => {
- spyOn(service, 'enumerate').and.returnValue(observableOf(['foo', 'bar']));
- let result;
+ it('should call exists with a non existent uid', (done) => {
+ spyOn(service, 'get').and.returnValue(throwError('bar'));
service.exists('baz').subscribe((res) => {
- result = res;
+ expect(res).toBe(false);
+ done();
});
- expect(result).toBe(false);
});
});
import * as _ from 'lodash';
import { forkJoin as observableForkJoin, Observable, of as observableOf } from 'rxjs';
-import { mergeMap } from 'rxjs/operators';
+import { catchError, mapTo, mergeMap } from 'rxjs/operators';
import { cdEncode } from '../decorators/cd-encode';
import { ApiModule } from './api.module';
* @return {Observable<boolean>}
*/
exists(uid: string): Observable<boolean> {
- return this.enumerate().pipe(
- mergeMap((resp: string[]) => {
- const index = _.indexOf(resp, uid);
- return observableOf(-1 !== index);
+ return this.get(uid).pipe(
+ mapTo(true),
+ catchError((error: Event) => {
+ if (_.isFunction(error.preventDefault)) {
+ error.preventDefault();
+ }
+ return observableOf(false);
})
);
}