Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { MockedAuthorizationContext } from '@rocket.chat/mock-providers/src/MockedAuthorizationContext';
import { MockedServerContext } from '@rocket.chat/mock-providers/src/MockedServerContext';
import { MockedSettingsContext } from '@rocket.chat/mock-providers/src/MockedSettingsContext';
import { MockedUserContext } from '@rocket.chat/mock-providers/src/MockedUserContext';
import { QueryClientProvider, QueryClient } from '@tanstack/react-query';
import { renderHook, act } from '@testing-library/react-hooks';
import React from 'react';
import { useToggleReactionMutation } from './useToggleReactionMutation';
const queryClient = new QueryClient({
defaultOptions: {
queries: {
// ✅ turns retries off
retry: false,
},
},
logger: {
log: (..._) => {
// Log debugging information
},
warn: (..._) => {
// Log warning
},
error: (..._) => {
// Log error
},
},
});
beforeEach(() => {
queryClient.clear();
});
it('should be call rest `POST /v1/chat.react` method', async () => {
const fn = jest.fn();
const { result, waitFor } = renderHook(() => useToggleReactionMutation(), {
wrapper: ({ children }) => (
<QueryClientProvider client={queryClient}>
<MockedServerContext
handleRequest={({ method, pathPattern, params }) => {
if (method === 'POST' && pathPattern === '/v1/chat.react') {
fn(params);
return { success: true } as any;
}
throw new Error(`Endpoint not mocked: ${method} ${pathPattern}`);
}}
>
<MockedSettingsContext settings={{}}>
<MockedUserContext userPreferences={{}}>
<MockedAuthorizationContext permissions={[]}>{children}</MockedAuthorizationContext>
</MockedUserContext>
</MockedSettingsContext>
</MockedServerContext>
</QueryClientProvider>
),
});
act(async () => {
await result.current.mutateAsync({ mid: 'MID', reaction: 'smile' });
});
await waitFor(() => result.current.isLoading === false);
expect(fn).toHaveBeenCalledWith({
messageId: 'MID',
reaction: 'smile',
});
});
it('should not work for non-logged in users', async () => {
const fn = jest.fn();
const { result, waitForValueToChange } = renderHook(() => useToggleReactionMutation(), {
wrapper: ({ children }) => (
<QueryClientProvider client={queryClient}>
<MockedServerContext
handleRequest={({ method, pathPattern, params }) => {
if (method === 'POST' && pathPattern === '/v1/chat.react') {
fn(params);
return { success: true } as any;
}
throw new Error(`Endpoint not mocked: ${method} ${pathPattern}`);
}}
>
<MockedSettingsContext settings={{}}>
<MockedAuthorizationContext permissions={[]}>{children}</MockedAuthorizationContext>
</MockedSettingsContext>
</MockedServerContext>
</QueryClientProvider>
),
});
act(() => {
return result.current.mutate({ mid: 'MID', reaction: 'smile' });
});
await waitForValueToChange(() => result.current.status);
expect(fn).not.toHaveBeenCalled();
expect(result.current.status).toBe('error');
expect(result.current.error).toEqual(new Error('Not logged in'));
});