Skip to content

Commit

Permalink
feat: Add unit test for anonymous login
Browse files Browse the repository at this point in the history
  • Loading branch information
szuperaz committed Jul 12, 2023
1 parent e1c92c5 commit 77abe91
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 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
9 changes: 5 additions & 4 deletions projects/stream-chat-angular/src/lib/chat-client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ 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 userOrId you can emit this for anonymous logins
* @param userTokenOrProvider You can provide:<ul>
* <li> a token,
* <li> the keyword 'guest' to connect as [guest user](https://getstream.io/chat/docs/javascript/authless_users/?language=javascript#guest-users)
Expand All @@ -93,7 +93,7 @@ export class ChatClientService<
*/
async init(
apiKey: string,
userOrId: string | OwnUserResponse<T> | UserResponse<T>,
userOrId: string | OwnUserResponse<T> | UserResponse<T> | undefined,
userTokenOrProvider: TokenOrProvider | 'anonymous' | 'guest',
clientOptions?: StreamChatOptions
): ConnectAPIResponse<T> {
Expand All @@ -105,10 +105,11 @@ export class ChatClientService<
try {
result = await (
{
guest: () => this.chatClient.setGuestUser(user),
guest: () => this.chatClient.setGuestUser(user!),
anonymous: () => this.chatClient.connectAnonymousUser(),
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
}[`${userTokenOrProvider}`] ??
(() => this.chatClient.connectUser(user, userTokenOrProvider))
(() => this.chatClient.connectUser(user!, userTokenOrProvider))
)();
} catch (error) {
this.notificationService.addPermanentNotification(
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 77abe91

Please sign in to comment.