-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* AAE-20808 using new GraphQL library * AAE-20808 Code refactoring * AAE-20808 Improving unit tests * AAE-20808 unit test improvement * AAE-20808 Fixed process services storybook build
- Loading branch information
1 parent
06f1699
commit 44321b0
Showing
8 changed files
with
422 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
lib/process-services-cloud/src/lib/services/web-socket.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/*! | ||
* @license | ||
* Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { TestBed } from '@angular/core/testing'; | ||
import { Apollo, gql } from 'apollo-angular'; | ||
import { lastValueFrom, of, Subject } from 'rxjs'; | ||
import { WebSocketService } from './web-socket.service'; | ||
import { SubscriptionOptions } from '@apollo/client/core'; | ||
import { HttpClientTestingModule } from '@angular/common/http/testing'; | ||
import { AuthenticationService, AppConfigService } from '@alfresco/adf-core'; | ||
import { FeaturesServiceToken } from '@alfresco/adf-core/feature-flags'; | ||
|
||
describe('WebSocketService', () => { | ||
let service: WebSocketService; | ||
const onLogoutSubject: Subject<void> = new Subject<void>(); | ||
|
||
const apolloMock = jasmine.createSpyObj('Apollo', ['use', 'createNamed']); | ||
const isOnSpy = jasmine.createSpy('isOn$'); | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [HttpClientTestingModule], | ||
providers: [ | ||
{ | ||
provide: Apollo, | ||
useValue: apolloMock | ||
}, | ||
{ | ||
provide: AppConfigService, | ||
useValue: { | ||
get: () => 'wss://testHost' | ||
} | ||
}, | ||
{ | ||
provide: AuthenticationService, | ||
useValue: { | ||
getToken: () => 'testToken', | ||
onLogout: onLogoutSubject.asObservable() | ||
} | ||
}, | ||
{ | ||
provide: FeaturesServiceToken, | ||
useValue: { | ||
isOn$: isOnSpy | ||
} | ||
} | ||
] | ||
}); | ||
service = TestBed.inject(WebSocketService); | ||
TestBed.inject(FeaturesServiceToken); | ||
apolloMock.use.and.returnValues(undefined, { subscribe: () => of({}) }); | ||
isOnSpy.and.returnValues(of(true)); | ||
}); | ||
|
||
afterEach(() => { | ||
apolloMock.use.calls.reset(); | ||
apolloMock.createNamed.calls.reset(); | ||
}); | ||
|
||
it('should not create a new Apollo client if it is already in use', async () => { | ||
const apolloClientName = 'testClient'; | ||
const subscriptionOptions: SubscriptionOptions = { query: gql(`subscription {testQuery}`) }; | ||
const wsOptions = { apolloClientName, wsUrl: 'testUrl', subscriptionOptions }; | ||
|
||
apolloMock.use.and.returnValues(true, { subscribe: () => of({}) }); | ||
|
||
await lastValueFrom(service.getSubscription(wsOptions)); | ||
|
||
expect(apolloMock.use).toHaveBeenCalledTimes(2); | ||
expect(apolloMock.use).toHaveBeenCalledWith(apolloClientName); | ||
expect(apolloMock.createNamed).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should subscribe to Apollo client if not already in use', async () => { | ||
const apolloClientName = 'testClient'; | ||
const expectedApolloClientName = 'testClient'; | ||
const subscriptionOptions: SubscriptionOptions = { query: gql(`subscription {testQuery}`) }; | ||
const wsOptions = { apolloClientName, wsUrl: 'testUrl', subscriptionOptions }; | ||
|
||
await lastValueFrom(service.getSubscription(wsOptions)); | ||
|
||
expect(apolloMock.use).toHaveBeenCalledWith(expectedApolloClientName); | ||
expect(apolloMock.use).toHaveBeenCalledTimes(2); | ||
expect(apolloMock.createNamed).toHaveBeenCalledTimes(1); | ||
expect(apolloMock.createNamed).toHaveBeenCalledWith(expectedApolloClientName, jasmine.any(Object)); | ||
}); | ||
|
||
it('should create named client with the right authentication token when FF is on', async () => { | ||
let headers = {}; | ||
const expectedHeaders = { Authorization: 'Bearer testToken' }; | ||
const apolloClientName = 'testClient'; | ||
const subscriptionOptions: SubscriptionOptions = { query: gql(`subscription {testQuery}`) }; | ||
const wsOptions = { apolloClientName, wsUrl: 'testUrl', subscriptionOptions }; | ||
apolloMock.createNamed.and.callFake((_, options) => { | ||
headers = options.headers; | ||
}); | ||
|
||
await lastValueFrom(service.getSubscription(wsOptions)); | ||
|
||
expect(apolloMock.use).toHaveBeenCalledTimes(2); | ||
expect(apolloMock.createNamed).toHaveBeenCalled(); | ||
expect(headers).toEqual(expectedHeaders); | ||
}); | ||
|
||
it('should create named client with the right authentication token when FF is off', async () => { | ||
isOnSpy.and.returnValues(of(false)); | ||
let headers = {}; | ||
const expectedHeaders = { 'X-Authorization': 'Bearer testToken' }; | ||
const apolloClientName = 'testClient'; | ||
const subscriptionOptions: SubscriptionOptions = { query: gql(`subscription {testQuery}`) }; | ||
const wsOptions = { apolloClientName, wsUrl: 'testUrl', subscriptionOptions }; | ||
apolloMock.createNamed.and.callFake((_, options) => { | ||
headers = options.headers; | ||
}); | ||
|
||
await lastValueFrom(service.getSubscription(wsOptions)); | ||
|
||
expect(apolloMock.use).toHaveBeenCalledTimes(2); | ||
expect(apolloMock.createNamed).toHaveBeenCalled(); | ||
expect(headers).toEqual(expectedHeaders); | ||
}); | ||
}); |
Oops, something went wrong.