Skip to content

Commit

Permalink
Merge pull request #450 from GetStream/448
Browse files Browse the repository at this point in the history
448
  • Loading branch information
szuperaz authored Jul 12, 2023
2 parents 639d15e + 054ccec commit de42e08
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
13 changes: 13 additions & 0 deletions projects/stream-chat-angular/src/lib/chat-client.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ describe('ChatClientService', () => {
expect(mockChatClient.setGuestUser).toHaveBeenCalledWith({ id: userId });
});

it('should connect user - anonymous user', async () => {
mockChatClient.connectUser.calls.reset();
await service.init(apiKey, undefined, 'anonymous');

expect(StreamChat.getInstance).toHaveBeenCalledWith(apiKey, undefined);
const spy = jasmine.createSpy();
service.appSettings$.subscribe(spy);

expect(spy).toHaveBeenCalledWith(undefined);
expect(mockChatClient.connectUser).not.toHaveBeenCalled();
expect(mockChatClient.connectAnonymousUser).toHaveBeenCalledWith();
});

it(`should notify if connection wasn't successful`, async () => {
const notificationService = TestBed.inject(NotificationService);
const spy = jasmine.createSpy();
Expand Down
24 changes: 16 additions & 8 deletions projects/stream-chat-angular/src/lib/chat-client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,18 @@ export class ChatClientService<
/**
* Creates a [`StreamChat`](https://github.com/GetStream/stream-chat-js/blob/668b3e5521339f4e14fc657834531b4c8bf8176b/src/client.ts#L124) instance using the provided `apiKey`, and connects a user with the given meta data and token. More info about [connecting users](https://getstream.io/chat/docs/javascript/init_and_users/?language=javascript) can be found in the platform documentation.
* @param apiKey
* @param userOrId
* @param userTokenOrProvider You can provide a token, or the keyword 'guest' to connect as [guest user](https://getstream.io/chat/docs/javascript/authless_users/?language=javascript#guest-users)
* @param userOrId you can emit this for anonymous logins
* @param userTokenOrProvider You can provide:<ul>
* <li> a token, </li>
* <li> the keyword 'guest' to connect as [guest user](https://getstream.io/chat/docs/javascript/authless_users/?language=javascript#guest-users) </li>
* <li> the keyword 'anonymous' to connect as [anonymous user](https://getstream.io/chat/docs/javascript/authless_users/?language=javascript#anonymous-users) </li>
* </ul>
* @param clientOptions Setting to provide to the Stream client instance
*/
async init(
apiKey: string,
userOrId: string | OwnUserResponse<T> | UserResponse<T>,
userTokenOrProvider: TokenOrProvider | 'guest',
userOrId: string | OwnUserResponse<T> | UserResponse<T> | undefined,
userTokenOrProvider: TokenOrProvider | 'anonymous' | 'guest',
clientOptions?: StreamChatOptions
): ConnectAPIResponse<T> {
this.chatClient = StreamChat.getInstance<T>(apiKey, clientOptions);
Expand All @@ -99,10 +103,14 @@ export class ChatClientService<
await this.ngZone.runOutsideAngular(async () => {
const user = typeof userOrId === 'string' ? { id: userOrId } : userOrId;
try {
result =
userTokenOrProvider === 'guest'
? await this.chatClient.setGuestUser(user)
: await this.chatClient.connectUser(user, userTokenOrProvider);
result = await (
{
guest: () => this.chatClient.setGuestUser(user!),
anonymous: () => this.chatClient.connectAnonymousUser(),
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
}[`${userTokenOrProvider}`] ??
(() => this.chatClient.connectUser(user!, userTokenOrProvider))
)();
} catch (error) {
this.notificationService.addPermanentNotification(
'streamChat.Error connecting to chat, refresh the page to try again.',
Expand Down
3 changes: 3 additions & 0 deletions projects/stream-chat-angular/src/lib/mocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ export type MockStreamChatClient = {
getAppSettings: jasmine.Spy;
disconnectUser: jasmine.Spy;
queryChannels: jasmine.Spy;
connectAnonymousUser: jasmine.Spy;
};

export const mockStreamChatClient = (): MockStreamChatClient => {
Expand Down Expand Up @@ -338,6 +339,7 @@ export const mockStreamChatClient = (): MockStreamChatClient => {
const getUserAgent = jasmine
.createSpy()
.and.returnValue('stream-chat-javascript-client-browser-2.2.2');
const connectAnonymousUser = jasmine.createSpy();
/* eslint-enable jasmine/no-unsafe-spy */
const user = mockCurrentUser();
const on = (name: EventTypes | Function, handler: () => {}) => {
Expand Down Expand Up @@ -374,6 +376,7 @@ export const mockStreamChatClient = (): MockStreamChatClient => {
appSettings$,
queryChannels,
setGuestUser,
connectAnonymousUser,
};
};

Expand Down

0 comments on commit de42e08

Please sign in to comment.