Skip to content

Commit

Permalink
test: fix flaky connection recovery throttle tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinCupela committed May 22, 2024
1 parent 06f25c5 commit 8d26920
Showing 1 changed file with 51 additions and 16 deletions.
67 changes: 51 additions & 16 deletions src/components/ChannelList/__tests__/ChannelList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1562,8 +1562,8 @@ describe('ChannelList', () => {
.mockReturnValueOnce(1)
.mockReturnValueOnce(RECOVER_LOADED_CHANNELS_THROTTLE_INTERVAL_IN_MS + 1);

await act(() => {
chatClient.dispatchEvent({
await act(async () => {
await chatClient.dispatchEvent({
type: 'connection.recovered',
});
});
Expand All @@ -1575,8 +1575,8 @@ describe('ChannelList', () => {
);
});

await act(() => {
chatClient.dispatchEvent({
await act(async () => {
await chatClient.dispatchEvent({
type: 'connection.recovered',
});
});
Expand All @@ -1591,6 +1591,41 @@ describe('ChannelList', () => {
dateNowSpy.mockRestore();
});

it('should throttle channel queries', async () => {
jest.useFakeTimers('modern');
renderUI(chatClient);
expect(chatClient.queryChannels).toHaveBeenCalledTimes(1);

const dateNowSpy = jest
.spyOn(Date, 'now')
.mockReturnValueOnce(1)
.mockReturnValueOnce(RECOVER_LOADED_CHANNELS_THROTTLE_INTERVAL_IN_MS);

await act(async () => {
await chatClient.dispatchEvent({
type: 'connection.recovered',
});
});

jest.advanceTimersByTime(100);

await act(async () => {
await chatClient.dispatchEvent({
type: 'connection.recovered',
});
});
jest.advanceTimersByTime(100);
await waitFor(() => {
expect(chatClient.queryChannels).toHaveBeenCalledTimes(2);
expect(chatClient.queryChannels.mock.calls[1][2]).toStrictEqual(
expect.objectContaining({ offset: 0 }),
);
});

dateNowSpy.mockRestore();
jest.useRealTimers();
});

it('should circumvent the throttle interval if the last query failed', async () => {
renderUI(chatClient);
expect(chatClient.queryChannels).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -1640,8 +1675,8 @@ describe('ChannelList', () => {
.mockReturnValueOnce(MIN_RECOVER_LOADED_CHANNELS_THROTTLE_INTERVAL_IN_MS)
.mockReturnValueOnce(MIN_RECOVER_LOADED_CHANNELS_THROTTLE_INTERVAL_IN_MS + 1);

await act(() => {
chatClient.dispatchEvent({
await act(async () => {
await chatClient.dispatchEvent({
type: 'connection.recovered',
});
});
Expand All @@ -1653,8 +1688,8 @@ describe('ChannelList', () => {
);
});

await act(() => {
chatClient.dispatchEvent({
await act(async () => {
await chatClient.dispatchEvent({
type: 'connection.recovered',
});
});
Expand All @@ -1663,8 +1698,8 @@ describe('ChannelList', () => {
expect(chatClient.queryChannels).toHaveBeenCalledTimes(2);
});

await act(() => {
chatClient.dispatchEvent({
await act(async () => {
await chatClient.dispatchEvent({
type: 'connection.recovered',
});
});
Expand All @@ -1687,8 +1722,8 @@ describe('ChannelList', () => {
.mockReturnValueOnce(recoveryThrottleIntervalMs + 1)
.mockReturnValueOnce(MIN_RECOVER_LOADED_CHANNELS_THROTTLE_INTERVAL_IN_MS + 1);

await act(() => {
chatClient.dispatchEvent({
await act(async () => {
await chatClient.dispatchEvent({
type: 'connection.recovered',
});
});
Expand All @@ -1700,14 +1735,14 @@ describe('ChannelList', () => {
);
});

await act(() => {
chatClient.dispatchEvent({
await act(async () => {
await chatClient.dispatchEvent({
type: 'connection.recovered',
});
});

await act(() => {
chatClient.dispatchEvent({
await act(async () => {
await chatClient.dispatchEvent({
type: 'connection.recovered',
});
});
Expand Down

0 comments on commit 8d26920

Please sign in to comment.