You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by rl...@apache.org on 2017/11/16 19:45:53 UTC

[37/50] [abbrv] ambari git commit: AMBARI-22455 Ambari Logsearch Web unit tests are sometimes failing. (ababiichuk)

AMBARI-22455 Ambari Logsearch Web unit tests are sometimes failing. (ababiichuk)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/2d81d49f
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/2d81d49f
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/2d81d49f

Branch: refs/heads/branch-feature-AMBARI-20859
Commit: 2d81d49fb3cc5fefbed079e7994cea7b41c5a125
Parents: 1a99da4
Author: ababiichuk <ab...@hortonworks.com>
Authored: Wed Nov 15 17:37:19 2017 +0200
Committer: ababiichuk <ab...@hortonworks.com>
Committed: Wed Nov 15 18:16:16 2017 +0200

----------------------------------------------------------------------
 .../src/app/services/auth.service.spec.ts       | 123 ++++++++++---------
 1 file changed, 62 insertions(+), 61 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/2d81d49f/ambari-logsearch/ambari-logsearch-web/src/app/services/auth.service.spec.ts
----------------------------------------------------------------------
diff --git a/ambari-logsearch/ambari-logsearch-web/src/app/services/auth.service.spec.ts b/ambari-logsearch/ambari-logsearch-web/src/app/services/auth.service.spec.ts
index a465c10..fd5a83e 100644
--- a/ambari-logsearch/ambari-logsearch-web/src/app/services/auth.service.spec.ts
+++ b/ambari-logsearch/ambari-logsearch-web/src/app/services/auth.service.spec.ts
@@ -16,9 +16,12 @@
  * limitations under the License.
  */
 
-import {TestBed, inject} from '@angular/core/testing';
+import {TestBed, inject, async} from '@angular/core/testing';
 import {HttpModule} from '@angular/http';
 import {Observable} from 'rxjs/Observable';
+import 'rxjs/add/operator/first';
+import 'rxjs/add/operator/last';
+import 'rxjs/add/operator/take';
 import {StoreModule} from '@ngrx/store';
 import {AppStateService, appState} from '@app/services/storage/app-state.service';
 import {AuthService} from '@app/services/auth.service';
@@ -26,49 +29,37 @@ import {HttpClientService} from '@app/services/http-client.service';
 
 describe('AuthService', () => {
 
-  let successResponse = {
-    type: 'default',
-    ok: true,
-    url: '/',
-    status: 200,
-    statusText: 'OK',
-    bytesLoaded: 100,
-    totalBytes: 100,
-    headers: null
-  };
-  let errorResponse = {
-    type: 'error',
-    ok: false,
-    url: '/',
-    status: 401,
-    statusText: 'ERROR',
-    bytesLoaded: 100,
-    totalBytes: 100,
-    headers: null
+  const successResponse = {
+      type: 'default',
+      ok: true,
+      url: '/',
+      status: 200,
+      statusText: 'OK',
+      bytesLoaded: 100,
+      totalBytes: 100,
+      headers: null
+    },
+    errorResponse = {
+      type: 'error',
+      ok: false,
+      url: '/',
+      status: 401,
+      statusText: 'ERROR',
+      bytesLoaded: 100,
+      totalBytes: 100,
+      headers: null
+    };
+
+  // Note: We add delay to help the isLoginInProgress test case.
+  let httpServiceStub = {
+    isError: false,
+    postFormData: function () {
+      const isError = this.isError;
+      return Observable.create(observer => observer.next(isError ? errorResponse : successResponse)).delay(1000);
+    }
   };
-  let currentResponse = successResponse;
-  let httpServiceStub;
-  let authService: AuthService;
 
   beforeEach(() => {
-    // Note: We add delay to help the isLoginInProgress test case.
-    httpServiceStub = {
-      postFormData: function () {
-        return Observable.create(observer => {
-          observer.next(currentResponse);
-        }).delay(1000);
-      },
-      post: function () {
-        return Observable.create(observer => {
-          observer.next(currentResponse);
-        }).delay(1000);
-      },
-      get: function () {
-        return Observable.create(observer => {
-          observer.next(currentResponse);
-        }).delay(1000);
-      }
-    };
     TestBed.configureTestingModule({
       imports: [
         HttpModule,
@@ -82,51 +73,61 @@ describe('AuthService', () => {
         {provide: HttpClientService, useValue: httpServiceStub}
       ]
     });
-    authService = TestBed.get(AuthService);
   });
 
   it('should create service', inject([AuthService], (service: AuthService) => {
     expect(service).toBeTruthy();
   }));
 
-  it('should set the isAuthorized state to true in appState when the login is success', inject(
-    [AppStateService],
-    (appStateService: AppStateService) => {
-      currentResponse = successResponse;
+  it('should set the isAuthorized state to true in appState when the login is success', async(inject(
+    [AuthService, AppStateService, HttpClientService],
+    (authService: AuthService, appStateService: AppStateService, httpClientService) => {
+      httpClientService.isError = false;
       authService.login('test', 'test')
         .subscribe(() => {
-          appStateService.getParameter('isAuthorized').subscribe((value) => {
+          appStateService.getParameter('isAuthorized').subscribe((value: Boolean): void => {
             expect(value).toBe(true);
           });
-        }, (value) => {
+        }, value => {
           throw value;
         });
     }
-  ));
+  )));
 
 
-  it('should set the isAuthorized state to false in appState when the login is failed', inject(
-    [AppStateService],
-    (appStateService: AppStateService) => {
-      currentResponse = errorResponse;
+  it('should set the isAuthorized state to false in appState when the login is failed', async(inject(
+    [AuthService, AppStateService, HttpClientService],
+    (authService: AuthService, appStateService: AppStateService, httpClientService) => {
+      httpClientService.isError = true;
       authService.login('test', 'test')
         .subscribe(() => {
-          appStateService.getParameter('isAuthorized').subscribe((value) => {
+          appStateService.getParameter('isAuthorized').subscribe((value: Boolean): void => {
             expect(value).toBe(false);
           });
         });
     }
-  ));
+  )));
 
-  it('should set the isLoginInProgress state to true when the login started.', inject(
-    [AppStateService],
-    (appStateService: AppStateService) => {
-      currentResponse = successResponse;
+  it('should set the isLoginInProgress state to true when the login started', async(inject(
+    [AuthService, AppStateService, HttpClientService],
+    (authService: AuthService, appStateService: AppStateService, httpClientService) => {
+      httpClientService.isError = false;
       authService.login('test', 'test');
-      appStateService.getParameter('isLoginInProgress').subscribe((value) => {
+      appStateService.getParameter('isLoginInProgress').first().subscribe((value: Boolean): void => {
         expect(value).toBe(true);
       });
     }
-  ));
+  )));
+
+  it('should set the isLoginInProgress state to true after the login is success', async(inject(
+    [AuthService, AppStateService, HttpClientService],
+    (authService: AuthService, appStateService: AppStateService, httpClientService) => {
+      httpClientService.isError = false;
+      authService.login('test', 'test');
+      appStateService.getParameter('isLoginInProgress').take(2).last().subscribe((value: Boolean): void => {
+        expect(value).toBe(false);
+      });
+    }
+  )));
 
 });