Skip to content

Commit 838d6f0

Browse files
authored
Merge pull request #249 from GetStream/channel-service-quirks-fix
Channel service quirks fix
2 parents 90d8315 + cb72cdd commit 838d6f0

File tree

2 files changed

+96
-19
lines changed

2 files changed

+96
-19
lines changed

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

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ describe('ChannelService', () => {
3434
let init: (
3535
c?: Channel[],
3636
sort?: ChannelSort,
37-
options?: ChannelOptions
38-
) => Promise<void>;
37+
options?: ChannelOptions,
38+
mockChannelQuery?: Function,
39+
shouldSetActiveChannel?: boolean
40+
) => Promise<Channel[]>;
3941
let user: UserResponse;
4042
const filters = { type: 'messaging' };
4143

@@ -65,16 +67,22 @@ describe('ChannelService', () => {
6567
],
6668
});
6769
service = TestBed.inject(ChannelService);
68-
init = async (
70+
init = (
6971
channels?: Channel[],
7072
sort?: ChannelSort,
71-
options?: ChannelOptions
73+
options?: ChannelOptions,
74+
mockChannelQuery?: Function,
75+
shouldSetActiveChannel?: boolean
7276
) => {
73-
mockChatClient.queryChannels.and.returnValue(
74-
channels || generateMockChannels()
75-
);
77+
if (mockChannelQuery) {
78+
mockChannelQuery();
79+
} else {
80+
mockChatClient.queryChannels.and.returnValue(
81+
channels || generateMockChannels()
82+
);
83+
}
7684

77-
await service.init(filters, sort, options);
85+
return service.init(filters, sort, options, shouldSetActiveChannel);
7886
};
7987
});
8088

@@ -122,6 +130,32 @@ describe('ChannelService', () => {
122130
});
123131
});
124132

133+
it('should return the result of the init', async () => {
134+
const expectedResult = generateMockChannels();
135+
const result = await init(expectedResult);
136+
137+
expect(result as any as MockChannel[]).toEqual(expectedResult);
138+
});
139+
140+
it('should return the result of the init - error', async () => {
141+
const error = 'there was an error';
142+
143+
await expectAsync(
144+
init(undefined, undefined, undefined, () =>
145+
mockChatClient.queryChannels.and.rejectWith(error)
146+
)
147+
).toBeRejectedWith(error);
148+
});
149+
150+
it('should not set active channel if #shouldSetActiveChannel is false', async () => {
151+
const activeChannelSpy = jasmine.createSpy();
152+
service.activeChannel$.subscribe(activeChannelSpy);
153+
activeChannelSpy.calls.reset();
154+
await init(undefined, undefined, undefined, undefined, false);
155+
156+
expect(activeChannelSpy).not.toHaveBeenCalled();
157+
});
158+
125159
it('should reset', async () => {
126160
await init();
127161
const messagesSpy = jasmine.createSpy();
@@ -148,6 +182,28 @@ describe('ChannelService', () => {
148182
expect(latestMessagesSpy).toHaveBeenCalledWith({});
149183
});
150184

185+
it('should deselect active channel', async () => {
186+
await init();
187+
const messagesSpy = jasmine.createSpy();
188+
service.activeChannelMessages$.subscribe(messagesSpy);
189+
const activeChannelSpy = jasmine.createSpy();
190+
service.activeChannel$.subscribe(activeChannelSpy);
191+
const messageToQuoteSpy = jasmine.createSpy();
192+
service.messageToQuote$.subscribe(messageToQuoteSpy);
193+
const latestMessagesSpy = jasmine.createSpy();
194+
service.latestMessageDateByUserByChannels$.subscribe(latestMessagesSpy);
195+
messagesSpy.calls.reset();
196+
activeChannelSpy.calls.reset();
197+
messageToQuoteSpy.calls.reset();
198+
latestMessagesSpy.calls.reset();
199+
service.deselectActiveChannel();
200+
201+
expect(messagesSpy).toHaveBeenCalledWith([]);
202+
expect(activeChannelSpy).toHaveBeenCalledWith(undefined);
203+
expect(messageToQuoteSpy).toHaveBeenCalledWith(undefined);
204+
expect(latestMessagesSpy).toHaveBeenCalledWith({});
205+
});
206+
151207
it('should tell if user #hasMoreChannels$', async () => {
152208
await init();
153209
const spy = jasmine.createSpy();

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

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,22 @@ export class ChannelService {
325325
this.messageToQuoteSubject.next(undefined);
326326
}
327327

328+
/**
329+
* Deselects the currently active (if any) channel
330+
*/
331+
deselectActiveChannel() {
332+
const activeChannel = this.activeChannelSubject.getValue();
333+
if (!activeChannel) {
334+
return;
335+
}
336+
this.activeChannelMessagesSubject.next([]);
337+
this.activeChannelSubject.next(undefined);
338+
this.activeParentMessageIdSubject.next(undefined);
339+
this.activeThreadMessagesSubject.next([]);
340+
this.latestMessageDateByUserByChannelsSubject.next({});
341+
this.selectMessageToQuote(undefined);
342+
}
343+
328344
/**
329345
* Sets the given `message` as an active parent message. If `undefined` is provided, it will deleselect the current parent message.
330346
* @param message
@@ -392,11 +408,14 @@ export class ChannelService {
392408
* @param filters
393409
* @param sort
394410
* @param options
411+
* @param shouldSetActiveChannel Decides if the first channel in the result should be made as an active channel, or no channel should be marked as active
412+
* @returns the list of channels found by the query
395413
*/
396414
async init(
397415
filters: ChannelFilters,
398416
sort?: ChannelSort,
399-
options?: ChannelOptions
417+
options?: ChannelOptions,
418+
shouldSetActiveChannel: boolean = true
400419
) {
401420
this.filters = filters;
402421
this.options = options || {
@@ -408,31 +427,27 @@ export class ChannelService {
408427
message_limit: this.messagePageSize,
409428
};
410429
this.sort = sort || { last_message_at: -1, updated_at: -1 };
411-
await this.queryChannels();
430+
const result = await this.queryChannels(shouldSetActiveChannel);
412431
this.chatClientService.notification$.subscribe(
413432
(notification) => void this.handleNotification(notification)
414433
);
434+
return result;
415435
}
416436

417437
/**
418438
* Resets the `activeChannel$`, `channels$` and `activeChannelMessages$` Observables. Useful when disconnecting a chat user, use in combination with [`disconnectUser`](./ChatClientService.mdx/#disconnectuser).
419439
*/
420440
reset() {
421-
this.activeChannelMessagesSubject.next([]);
422-
this.activeChannelSubject.next(undefined);
423-
this.activeParentMessageIdSubject.next(undefined);
424-
this.activeThreadMessagesSubject.next([]);
441+
this.deselectActiveChannel();
425442
this.channelsSubject.next(undefined);
426-
this.latestMessageDateByUserByChannelsSubject.next({});
427-
this.selectMessageToQuote(undefined);
428443
}
429444

430445
/**
431446
* Loads the next page of channels. The page size can be set in the [query option](https://getstream.io/chat/docs/javascript/query_channels/?language=javascript#query-options) object.
432447
*/
433448
async loadMoreChannels() {
434449
this.options!.offset = this.channels.length!;
435-
await this.queryChannels();
450+
await this.queryChannels(false);
436451
}
437452

438453
/**
@@ -948,7 +963,7 @@ export class ChannelService {
948963
this.activeChannelSubscriptions = [];
949964
}
950965

951-
private async queryChannels() {
966+
private async queryChannels(shouldSetActiveChannel: boolean) {
952967
try {
953968
const channels = await this.chatClientService.chatClient.queryChannels(
954969
this.filters!,
@@ -958,12 +973,18 @@ export class ChannelService {
958973
channels.forEach((c) => this.watchForChannelEvents(c));
959974
const prevChannels = this.channelsSubject.getValue() || [];
960975
this.channelsSubject.next([...prevChannels, ...channels]);
961-
if (channels.length > 0 && !this.activeChannelSubject.getValue()) {
976+
if (
977+
channels.length > 0 &&
978+
!this.activeChannelSubject.getValue() &&
979+
shouldSetActiveChannel
980+
) {
962981
this.setAsActiveChannel(channels[0]);
963982
}
964983
this.hasMoreChannelsSubject.next(channels.length >= this.options!.limit!);
984+
return channels;
965985
} catch (error) {
966986
this.channelsSubject.error(error);
987+
throw error;
967988
}
968989
}
969990

0 commit comments

Comments
 (0)