Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion projects/stream-chat-angular/src/lib/chat-client.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,28 @@ describe('ChatClientService', () => {
await service.init(apiKey, userId, userToken);
});

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

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

expect(spy).toHaveBeenCalledWith(undefined);
expect(userSpy).toHaveBeenCalledWith(mockCurrentUser());
expect(mockChatClient.connectUser).toHaveBeenCalledWith(
{ id: userId },
userToken
);
});

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

expect(StreamChat.getInstance).toHaveBeenCalledWith(apiKey);
const spy = jasmine.createSpy();
service.appSettings$.subscribe(spy);
Expand All @@ -38,6 +59,8 @@ describe('ChatClientService', () => {

expect(spy).toHaveBeenCalledWith(undefined);
expect(userSpy).toHaveBeenCalledWith(mockCurrentUser());
expect(mockChatClient.connectUser).not.toHaveBeenCalled();
expect(mockChatClient.setGuestUser).toHaveBeenCalledWith({ id: userId });
});

it('should disconnect user', async () => {
Expand Down
9 changes: 6 additions & 3 deletions projects/stream-chat-angular/src/lib/chat-client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,22 @@ 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
* @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)
*/
async init(
apiKey: string,
userOrId: string | OwnUserResponse<T> | UserResponse<T>,
userTokenOrProvider: TokenOrProvider
userTokenOrProvider: TokenOrProvider | 'guest'
): ConnectAPIResponse<T> {
this.chatClient = StreamChat.getInstance<T>(apiKey);
this.chatClient.devToken;
let result;
await this.ngZone.runOutsideAngular(async () => {
const user = typeof userOrId === 'string' ? { id: userOrId } : userOrId;
result = await this.chatClient.connectUser(user, userTokenOrProvider);
result =
userTokenOrProvider === 'guest'
? await this.chatClient.setGuestUser(user)
: await this.chatClient.connectUser(user, userTokenOrProvider);
this.userSubject.next(this.chatClient.user);
this.chatClient.setUserAgent(
`stream-chat-angular-${version}-${this.chatClient.getUserAgent()}`
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 @@ -275,6 +275,7 @@ export type MockStreamChatClient = {
appSettings$: Subject<AppSettings>;
user: UserResponse;
connectUser: jasmine.Spy;
setGuestUser: jasmine.Spy;
on: (name: EventTypes, handler: () => {}) => { unsubscribe: () => void };
handleEvent: (name: EventTypes, event: Event) => void;
flagMessage: jasmine.Spy;
Expand All @@ -290,6 +291,7 @@ export const mockStreamChatClient = (): MockStreamChatClient => {
const eventHandlers: { [key: string]: Function } = {};
/* eslint-disable jasmine/no-unsafe-spy */
const connectUser = jasmine.createSpy();
const setGuestUser = jasmine.createSpy();
const flagMessage = jasmine.createSpy();
const setUserAgent = jasmine.createSpy();
const queryUsers = jasmine.createSpy();
Expand Down Expand Up @@ -347,6 +349,7 @@ export const mockStreamChatClient = (): MockStreamChatClient => {
getAppSettings,
appSettings$,
queryChannels,
setGuestUser,
};
};

Expand Down