Skip to content

Commit cde2324

Browse files
committed
feat: ChannelService.init returns the query result #248
1 parent 90d8315 commit cde2324

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

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

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

@@ -65,16 +66,21 @@ describe('ChannelService', () => {
6566
],
6667
});
6768
service = TestBed.inject(ChannelService);
68-
init = async (
69+
init = (
6970
channels?: Channel[],
7071
sort?: ChannelSort,
71-
options?: ChannelOptions
72+
options?: ChannelOptions,
73+
mockChannelQuery?: Function
7274
) => {
73-
mockChatClient.queryChannels.and.returnValue(
74-
channels || generateMockChannels()
75-
);
75+
if (mockChannelQuery) {
76+
mockChannelQuery();
77+
} else {
78+
mockChatClient.queryChannels.and.returnValue(
79+
channels || generateMockChannels()
80+
);
81+
}
7682

77-
await service.init(filters, sort, options);
83+
return service.init(filters, sort, options);
7884
};
7985
});
8086

@@ -122,6 +128,23 @@ describe('ChannelService', () => {
122128
});
123129
});
124130

131+
it('should return the result of the init', async () => {
132+
const expectedResult = generateMockChannels();
133+
const result = await init(expectedResult);
134+
135+
expect(result as any as MockChannel[]).toEqual(expectedResult);
136+
});
137+
138+
it('should return the result of the init - error', async () => {
139+
const error = 'there was an error';
140+
141+
await expectAsync(
142+
init(undefined, undefined, undefined, () =>
143+
mockChatClient.queryChannels.and.rejectWith(error)
144+
)
145+
).toBeRejectedWith(error);
146+
});
147+
125148
it('should reset', async () => {
126149
await init();
127150
const messagesSpy = jasmine.createSpy();

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ export class ChannelService {
392392
* @param filters
393393
* @param sort
394394
* @param options
395+
* @returns the list of channels found by the query
395396
*/
396397
async init(
397398
filters: ChannelFilters,
@@ -408,10 +409,11 @@ export class ChannelService {
408409
message_limit: this.messagePageSize,
409410
};
410411
this.sort = sort || { last_message_at: -1, updated_at: -1 };
411-
await this.queryChannels();
412+
const result = await this.queryChannels();
412413
this.chatClientService.notification$.subscribe(
413414
(notification) => void this.handleNotification(notification)
414415
);
416+
return result;
415417
}
416418

417419
/**
@@ -962,8 +964,10 @@ export class ChannelService {
962964
this.setAsActiveChannel(channels[0]);
963965
}
964966
this.hasMoreChannelsSubject.next(channels.length >= this.options!.limit!);
967+
return channels;
965968
} catch (error) {
966969
this.channelsSubject.error(error);
970+
throw error;
967971
}
968972
}
969973

0 commit comments

Comments
 (0)