Skip to content

Commit 77abe91

Browse files
committed
feat: Add unit test for anonymous login
1 parent e1c92c5 commit 77abe91

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

projects/stream-chat-angular/src/lib/chat-client.service.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ describe('ChatClientService', () => {
6363
expect(mockChatClient.setGuestUser).toHaveBeenCalledWith({ id: userId });
6464
});
6565

66+
it('should connect user - anonymous user', async () => {
67+
mockChatClient.connectUser.calls.reset();
68+
await service.init(apiKey, undefined, 'anonymous');
69+
70+
expect(StreamChat.getInstance).toHaveBeenCalledWith(apiKey, undefined);
71+
const spy = jasmine.createSpy();
72+
service.appSettings$.subscribe(spy);
73+
74+
expect(spy).toHaveBeenCalledWith(undefined);
75+
expect(mockChatClient.connectUser).not.toHaveBeenCalled();
76+
expect(mockChatClient.connectAnonymousUser).toHaveBeenCalledWith();
77+
});
78+
6679
it(`should notify if connection wasn't successful`, async () => {
6780
const notificationService = TestBed.inject(NotificationService);
6881
const spy = jasmine.createSpy();

projects/stream-chat-angular/src/lib/chat-client.service.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class ChatClientService<
8383
/**
8484
* 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.
8585
* @param apiKey
86-
* @param userOrId
86+
* @param userOrId you can emit this for anonymous logins
8787
* @param userTokenOrProvider You can provide:<ul>
8888
* <li> a token,
8989
* <li> the keyword 'guest' to connect as [guest user](https://getstream.io/chat/docs/javascript/authless_users/?language=javascript#guest-users)
@@ -93,7 +93,7 @@ export class ChatClientService<
9393
*/
9494
async init(
9595
apiKey: string,
96-
userOrId: string | OwnUserResponse<T> | UserResponse<T>,
96+
userOrId: string | OwnUserResponse<T> | UserResponse<T> | undefined,
9797
userTokenOrProvider: TokenOrProvider | 'anonymous' | 'guest',
9898
clientOptions?: StreamChatOptions
9999
): ConnectAPIResponse<T> {
@@ -105,10 +105,11 @@ export class ChatClientService<
105105
try {
106106
result = await (
107107
{
108-
guest: () => this.chatClient.setGuestUser(user),
108+
guest: () => this.chatClient.setGuestUser(user!),
109109
anonymous: () => this.chatClient.connectAnonymousUser(),
110+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
110111
}[`${userTokenOrProvider}`] ??
111-
(() => this.chatClient.connectUser(user, userTokenOrProvider))
112+
(() => this.chatClient.connectUser(user!, userTokenOrProvider))
112113
)();
113114
} catch (error) {
114115
this.notificationService.addPermanentNotification(

projects/stream-chat-angular/src/lib/mocks/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ export type MockStreamChatClient = {
307307
getAppSettings: jasmine.Spy;
308308
disconnectUser: jasmine.Spy;
309309
queryChannels: jasmine.Spy;
310+
connectAnonymousUser: jasmine.Spy;
310311
};
311312

312313
export const mockStreamChatClient = (): MockStreamChatClient => {
@@ -338,6 +339,7 @@ export const mockStreamChatClient = (): MockStreamChatClient => {
338339
const getUserAgent = jasmine
339340
.createSpy()
340341
.and.returnValue('stream-chat-javascript-client-browser-2.2.2');
342+
const connectAnonymousUser = jasmine.createSpy();
341343
/* eslint-enable jasmine/no-unsafe-spy */
342344
const user = mockCurrentUser();
343345
const on = (name: EventTypes | Function, handler: () => {}) => {
@@ -374,6 +376,7 @@ export const mockStreamChatClient = (): MockStreamChatClient => {
374376
appSettings$,
375377
queryChannels,
376378
setGuestUser,
379+
connectAnonymousUser,
377380
};
378381
};
379382

0 commit comments

Comments
 (0)