forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiHooks.test.tsx
More file actions
69 lines (58 loc) · 2.18 KB
/
apiHooks.test.tsx
File metadata and controls
69 lines (58 loc) · 2.18 KB
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
import { renderHook, waitFor } from '@testing-library/react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { camelCaseObject, getConfig } from '@edx/frontend-platform';
import api from '@src/editors/data/services/cms/api';
import { useValidateInputBlock } from './apiHooks';
// Mock external dependencies
jest.mock('@edx/frontend-platform');
jest.mock('@src/editors/data/services/cms/api', () => ({
validateBlockNumericInput: jest.fn(),
}));
const mockedCamelCaseObject = jest.mocked(camelCaseObject);
const mockedGetConfig = jest.mocked(getConfig);
const mockedValidateBlockNumericInput = jest.mocked(api.validateBlockNumericInput);
// Test wrapper component
const createWrapper = () => {
const queryClient = new QueryClient({
defaultOptions: {
queries: { retry: false },
mutations: { retry: false },
},
});
const wrapper = ({ children }) => (
<QueryClientProvider client={queryClient}>
{children}
</QueryClientProvider>
);
return wrapper;
};
describe('useValidateInputBlock', () => {
beforeEach(() => {
jest.clearAllMocks();
mockedGetConfig.mockReturnValue({
STUDIO_BASE_URL: 'http://studio.local.openedx.io:8001',
});
});
test('should return camelCase data on successful API call', async () => {
const mockResponse = {
data: { is_valid: true, result: 'success' },
} as any;
const mockCamelCaseResult = { isValid: true, result: 'success' };
mockedValidateBlockNumericInput.mockResolvedValue(Promise.resolve(mockResponse));
mockedCamelCaseObject.mockReturnValue(mockCamelCaseResult);
const { result } = renderHook(() => useValidateInputBlock(), {
wrapper: createWrapper(),
});
const testFormula = 'x + 1';
result.current.mutate(testFormula);
await waitFor(() => {
expect(result.current.isSuccess).toBe(true);
});
expect(mockedValidateBlockNumericInput).toHaveBeenCalledWith({
studioEndpointUrl: 'http://studio.local.openedx.io:8001',
data: { formula: testFormula },
});
expect(mockedCamelCaseObject).toHaveBeenCalledWith(mockResponse.data);
expect(result.current.data).toEqual({ isValid: true, result: 'success' });
});
});