@rooguys/sdk 0.1.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,32 +1,32 @@
1
- import axios from 'axios';
2
1
  import { Rooguys } from '../../index';
3
- import { createMockAxiosInstance, mockSuccessResponse, mockErrorResponse } from '../utils/mockClient';
2
+ import {
3
+ createMockRooguysClient,
4
+ setupMockRequest,
5
+ setupMockRequestError,
6
+ expectRequestWith,
7
+ MockAxiosInstance,
8
+ } from '../utils/mockClient';
4
9
  import { mockResponses, mockErrors } from '../fixtures/responses';
5
10
 
6
- jest.mock('axios');
7
- const mockedAxios = axios as jest.Mocked<typeof axios>;
8
-
9
11
  describe('Levels Resource', () => {
10
12
  let client: Rooguys;
11
- let mockAxiosInstance: ReturnType<typeof createMockAxiosInstance>;
12
- const apiKey = 'test-api-key';
13
+ let mockAxios: MockAxiosInstance;
13
14
 
14
15
  beforeEach(() => {
15
- mockAxiosInstance = createMockAxiosInstance();
16
- mockedAxios.create.mockReturnValue(mockAxiosInstance as any);
17
- client = new Rooguys(apiKey);
18
- jest.clearAllMocks();
16
+ const mock = createMockRooguysClient();
17
+ client = mock.client;
18
+ mockAxios = mock.mockAxios;
19
19
  });
20
20
 
21
21
  describe('list', () => {
22
22
  it('should list levels with default parameters', async () => {
23
- mockAxiosInstance.get.mockResolvedValue(
24
- mockSuccessResponse(mockResponses.levelsListResponse)
25
- );
23
+ setupMockRequest(mockAxios, mockResponses.levelsListResponse);
26
24
 
27
25
  const result = await client.levels.list();
28
26
 
29
- expect(mockAxiosInstance.get).toHaveBeenCalledWith('/levels', {
27
+ expectRequestWith(mockAxios, {
28
+ method: 'GET',
29
+ url: '/levels',
30
30
  params: { page: 1, limit: 50 },
31
31
  });
32
32
  expect(result).toEqual(mockResponses.levelsListResponse);
@@ -34,13 +34,13 @@ describe('Levels Resource', () => {
34
34
  });
35
35
 
36
36
  it('should list levels with custom pagination', async () => {
37
- mockAxiosInstance.get.mockResolvedValue(
38
- mockSuccessResponse(mockResponses.levelsListResponse)
39
- );
37
+ setupMockRequest(mockAxios, mockResponses.levelsListResponse);
40
38
 
41
39
  await client.levels.list(2, 25);
42
40
 
43
- expect(mockAxiosInstance.get).toHaveBeenCalledWith('/levels', {
41
+ expectRequestWith(mockAxios, {
42
+ method: 'GET',
43
+ url: '/levels',
44
44
  params: { page: 2, limit: 25 },
45
45
  });
46
46
  });
@@ -55,7 +55,7 @@ describe('Levels Resource', () => {
55
55
  totalPages: 0,
56
56
  },
57
57
  };
58
- mockAxiosInstance.get.mockResolvedValue(mockSuccessResponse(emptyResponse));
58
+ setupMockRequest(mockAxios, emptyResponse);
59
59
 
60
60
  const result = await client.levels.list();
61
61
 
@@ -64,9 +64,7 @@ describe('Levels Resource', () => {
64
64
  });
65
65
 
66
66
  it('should handle levels with all fields', async () => {
67
- mockAxiosInstance.get.mockResolvedValue(
68
- mockSuccessResponse(mockResponses.levelsListResponse)
69
- );
67
+ setupMockRequest(mockAxios, mockResponses.levelsListResponse);
70
68
 
71
69
  const result = await client.levels.list();
72
70
 
@@ -80,9 +78,7 @@ describe('Levels Resource', () => {
80
78
  });
81
79
 
82
80
  it('should handle levels sorted by level_number', async () => {
83
- mockAxiosInstance.get.mockResolvedValue(
84
- mockSuccessResponse(mockResponses.levelsListResponse)
85
- );
81
+ setupMockRequest(mockAxios, mockResponses.levelsListResponse);
86
82
 
87
83
  const result = await client.levels.list();
88
84
 
@@ -91,9 +87,7 @@ describe('Levels Resource', () => {
91
87
  });
92
88
 
93
89
  it('should throw error for invalid pagination', async () => {
94
- mockAxiosInstance.get.mockRejectedValue(
95
- mockErrorResponse(400, mockErrors.invalidPaginationError.message)
96
- );
90
+ setupMockRequestError(mockAxios, 400, 'Limit must be between 1 and 100');
97
91
 
98
92
  await expect(client.levels.list(1, 150)).rejects.toThrow(
99
93
  'Limit must be between 1 and 100'
@@ -101,9 +95,7 @@ describe('Levels Resource', () => {
101
95
  });
102
96
 
103
97
  it('should handle server error', async () => {
104
- mockAxiosInstance.get.mockRejectedValue(
105
- mockErrorResponse(500, 'Internal server error')
106
- );
98
+ setupMockRequestError(mockAxios, 500, 'Internal server error');
107
99
 
108
100
  await expect(client.levels.list()).rejects.toThrow('Internal server error');
109
101
  });
@@ -127,7 +119,7 @@ describe('Levels Resource', () => {
127
119
  totalPages: 1,
128
120
  },
129
121
  };
130
- mockAxiosInstance.get.mockResolvedValue(mockSuccessResponse(levelsWithNulls));
122
+ setupMockRequest(mockAxios, levelsWithNulls);
131
123
 
132
124
  const result = await client.levels.list();
133
125
 
@@ -1,40 +1,39 @@
1
- import axios from 'axios';
2
1
  import { Rooguys } from '../../index';
3
- import { createMockAxiosInstance, mockSuccessResponse, mockErrorResponse } from '../utils/mockClient';
2
+ import {
3
+ createMockRooguysClient,
4
+ setupMockRequest,
5
+ setupMockRequestError,
6
+ expectRequestWith,
7
+ MockAxiosInstance,
8
+ } from '../utils/mockClient';
4
9
  import { mockResponses, mockErrors } from '../fixtures/responses';
5
10
 
6
- jest.mock('axios');
7
- const mockedAxios = axios as jest.Mocked<typeof axios>;
8
-
9
11
  describe('Questionnaires Resource', () => {
10
12
  let client: Rooguys;
11
- let mockAxiosInstance: ReturnType<typeof createMockAxiosInstance>;
12
- const apiKey = 'test-api-key';
13
+ let mockAxios: MockAxiosInstance;
13
14
 
14
15
  beforeEach(() => {
15
- mockAxiosInstance = createMockAxiosInstance();
16
- mockedAxios.create.mockReturnValue(mockAxiosInstance as any);
17
- client = new Rooguys(apiKey);
18
- jest.clearAllMocks();
16
+ const mock = createMockRooguysClient();
17
+ client = mock.client;
18
+ mockAxios = mock.mockAxios;
19
19
  });
20
20
 
21
21
  describe('get', () => {
22
22
  it('should get questionnaire by slug', async () => {
23
- mockAxiosInstance.get.mockResolvedValue(
24
- mockSuccessResponse(mockResponses.questionnaireResponse)
25
- );
23
+ setupMockRequest(mockAxios, mockResponses.questionnaireResponse);
26
24
 
27
25
  const result = await client.questionnaires.get('user-persona');
28
26
 
29
- expect(mockAxiosInstance.get).toHaveBeenCalledWith('/questionnaire/user-persona');
27
+ expectRequestWith(mockAxios, {
28
+ method: 'GET',
29
+ url: '/questionnaires/user-persona',
30
+ });
30
31
  expect(result).toEqual(mockResponses.questionnaireResponse);
31
32
  expect(result.slug).toBe('user-persona');
32
33
  });
33
34
 
34
35
  it('should handle questionnaire with multiple questions', async () => {
35
- mockAxiosInstance.get.mockResolvedValue(
36
- mockSuccessResponse(mockResponses.questionnaireResponse)
37
- );
36
+ setupMockRequest(mockAxios, mockResponses.questionnaireResponse);
38
37
 
39
38
  const result = await client.questionnaires.get('user-persona');
40
39
 
@@ -43,9 +42,7 @@ describe('Questionnaires Resource', () => {
43
42
  });
44
43
 
45
44
  it('should handle questionnaire with answer options', async () => {
46
- mockAxiosInstance.get.mockResolvedValue(
47
- mockSuccessResponse(mockResponses.questionnaireResponse)
48
- );
45
+ setupMockRequest(mockAxios, mockResponses.questionnaireResponse);
49
46
 
50
47
  const result = await client.questionnaires.get('user-persona');
51
48
 
@@ -55,9 +52,7 @@ describe('Questionnaires Resource', () => {
55
52
  });
56
53
 
57
54
  it('should throw 404 error when questionnaire not found', async () => {
58
- mockAxiosInstance.get.mockRejectedValue(
59
- mockErrorResponse(404, 'Questionnaire not found')
60
- );
55
+ setupMockRequestError(mockAxios, 404, 'Questionnaire not found');
61
56
 
62
57
  await expect(
63
58
  client.questionnaires.get('nonexistent-slug')
@@ -65,13 +60,14 @@ describe('Questionnaires Resource', () => {
65
60
  });
66
61
 
67
62
  it('should handle slug with special characters', async () => {
68
- mockAxiosInstance.get.mockResolvedValue(
69
- mockSuccessResponse(mockResponses.questionnaireResponse)
70
- );
63
+ setupMockRequest(mockAxios, mockResponses.questionnaireResponse);
71
64
 
72
65
  await client.questionnaires.get('user-persona-v2');
73
66
 
74
- expect(mockAxiosInstance.get).toHaveBeenCalledWith('/questionnaire/user-persona-v2');
67
+ expectRequestWith(mockAxios, {
68
+ method: 'GET',
69
+ url: '/questionnaires/user-persona-v2',
70
+ });
75
71
  });
76
72
 
77
73
  it('should handle inactive questionnaire', async () => {
@@ -79,7 +75,7 @@ describe('Questionnaires Resource', () => {
79
75
  ...mockResponses.questionnaireResponse,
80
76
  is_active: false,
81
77
  };
82
- mockAxiosInstance.get.mockResolvedValue(mockSuccessResponse(inactiveQuestionnaire));
78
+ setupMockRequest(mockAxios, inactiveQuestionnaire);
83
79
 
84
80
  const result = await client.questionnaires.get('old-questionnaire');
85
81
 
@@ -89,21 +85,20 @@ describe('Questionnaires Resource', () => {
89
85
 
90
86
  describe('getActive', () => {
91
87
  it('should get active questionnaire', async () => {
92
- mockAxiosInstance.get.mockResolvedValue(
93
- mockSuccessResponse(mockResponses.questionnaireResponse)
94
- );
88
+ setupMockRequest(mockAxios, mockResponses.questionnaireResponse);
95
89
 
96
90
  const result = await client.questionnaires.getActive();
97
91
 
98
- expect(mockAxiosInstance.get).toHaveBeenCalledWith('/questionnaire/active');
92
+ expectRequestWith(mockAxios, {
93
+ method: 'GET',
94
+ url: '/questionnaires/active',
95
+ });
99
96
  expect(result).toEqual(mockResponses.questionnaireResponse);
100
97
  expect(result.is_active).toBe(true);
101
98
  });
102
99
 
103
100
  it('should throw 404 error when no active questionnaire', async () => {
104
- mockAxiosInstance.get.mockRejectedValue(
105
- mockErrorResponse(404, 'No active questionnaire found for this project')
106
- );
101
+ setupMockRequestError(mockAxios, 404, 'No active questionnaire found for this project');
107
102
 
108
103
  await expect(client.questionnaires.getActive()).rejects.toThrow(
109
104
  'No active questionnaire found'
@@ -111,9 +106,7 @@ describe('Questionnaires Resource', () => {
111
106
  });
112
107
 
113
108
  it('should handle active questionnaire with all fields', async () => {
114
- mockAxiosInstance.get.mockResolvedValue(
115
- mockSuccessResponse(mockResponses.questionnaireResponse)
116
- );
109
+ setupMockRequest(mockAxios, mockResponses.questionnaireResponse);
117
110
 
118
111
  const result = await client.questionnaires.getActive();
119
112
 
@@ -126,9 +119,7 @@ describe('Questionnaires Resource', () => {
126
119
  });
127
120
 
128
121
  it('should handle server error', async () => {
129
- mockAxiosInstance.get.mockRejectedValue(
130
- mockErrorResponse(500, 'Internal server error')
131
- );
122
+ setupMockRequestError(mockAxios, 500, 'Internal server error');
132
123
 
133
124
  await expect(client.questionnaires.getActive()).rejects.toThrow(
134
125
  'Internal server error'