-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcustom_method.test.tsx
More file actions
218 lines (173 loc) · 7.61 KB
/
Copy pathcustom_method.test.tsx
File metadata and controls
218 lines (173 loc) · 7.61 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { describe, expect, it, vi, beforeEach } from 'vitest';
import { CustomMethod } from '@aep_dev/aep-lib-ts';
import { CustomMethodComponent } from './custom_method';
import { ResourceInstance } from '@/state/fetch';
import { ResourceSchema } from '@/state/openapi';
// Mock fetch globally
global.fetch = vi.fn();
describe('CustomMethodComponent', () => {
const mockResourceSchema = {
server_url: 'http://localhost:8080',
} as ResourceSchema;
const createMockResourceInstance = (path: string): ResourceInstance => {
return {
id: '123',
path: path,
properties: { path: path },
schema: mockResourceSchema,
delete: vi.fn(),
update: vi.fn(),
} as unknown as ResourceInstance;
};
const createMockCustomMethod = (name: string, request: any = null): CustomMethod => {
return {
name,
method: 'POST',
request,
response: null,
};
};
beforeEach(() => {
vi.clearAllMocks();
(global.fetch as any).mockClear();
});
it('renders and submits custom method without request fields', async () => {
(global.fetch as any).mockResolvedValue({
ok: true,
json: async () => ({ success: true }),
});
const customMethod = createMockCustomMethod('archive', null);
const resourceInstance = createMockResourceInstance('books/123');
render(<CustomMethodComponent resourceInstance={resourceInstance} customMethod={customMethod} />);
expect(screen.getByText('archive')).toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: 'Submit' }));
await waitFor(() => {
expect(global.fetch).toHaveBeenCalledWith(
'http://localhost:8080/books/123:archive',
expect.objectContaining({
method: 'POST',
body: undefined,
})
);
});
expect(screen.getByText('Response:')).toBeInTheDocument();
expect(screen.getByText(/"success": true/)).toBeInTheDocument();
});
it('renders form fields and validates before submission', async () => {
const requestSchema = {
type: 'object',
properties: {
reason: { type: 'string' },
priority: { type: 'integer' },
},
required: ['reason'],
};
const customMethod = createMockCustomMethod('archive', requestSchema);
const resourceInstance = createMockResourceInstance('books/123');
render(<CustomMethodComponent resourceInstance={resourceInstance} customMethod={customMethod} />);
expect(screen.getByLabelText('reason')).toBeInTheDocument();
expect(screen.getByLabelText('priority')).toBeInTheDocument();
// Submit without filling required field
fireEvent.click(screen.getByRole('button', { name: 'Submit' }));
await waitFor(() => {
expect(screen.getByText('Required')).toBeInTheDocument();
});
expect(global.fetch).not.toHaveBeenCalled();
});
it('submits form with valid data and displays response', async () => {
(global.fetch as any).mockResolvedValue({
ok: true,
json: async () => ({ status: 'archived', timestamp: '2024-01-01' }),
});
const requestSchema = {
type: 'object',
properties: {
reason: { type: 'string' },
priority: { type: 'integer' },
},
required: ['reason'],
};
const customMethod = createMockCustomMethod('archive', requestSchema);
const resourceInstance = createMockResourceInstance('books/123');
render(<CustomMethodComponent resourceInstance={resourceInstance} customMethod={customMethod} />);
fireEvent.change(screen.getByLabelText('reason'), { target: { value: 'outdated' } });
fireEvent.change(screen.getByLabelText('priority'), { target: { value: '5' } });
fireEvent.click(screen.getByRole('button', { name: 'Submit' }));
await waitFor(() => {
expect(global.fetch).toHaveBeenCalledWith(
'http://localhost:8080/books/123:archive',
expect.objectContaining({
body: JSON.stringify({ reason: 'outdated', priority: 5 }),
})
);
});
expect(screen.getByText(/"status": "archived"/)).toBeInTheDocument();
});
it('handles nested objects in request schema', async () => {
(global.fetch as any).mockResolvedValue({
ok: true,
json: async () => ({ success: true }),
});
const requestSchema = {
type: 'object',
properties: {
metadata: {
type: 'object',
properties: {
reason: { type: 'string' },
notes: { type: 'string' },
},
},
},
};
const customMethod = createMockCustomMethod('archive', requestSchema);
const resourceInstance = createMockResourceInstance('books/123');
render(<CustomMethodComponent resourceInstance={resourceInstance} customMethod={customMethod} />);
expect(screen.getByText('metadata')).toBeInTheDocument();
fireEvent.change(screen.getByLabelText('reason'), { target: { value: 'outdated' } });
fireEvent.change(screen.getByLabelText('notes'), { target: { value: 'no longer relevant' } });
fireEvent.click(screen.getByRole('button', { name: 'Submit' }));
await waitFor(() => {
expect(global.fetch).toHaveBeenCalledWith(
'http://localhost:8080/books/123:archive',
expect.objectContaining({
body: JSON.stringify({
metadata: { reason: 'outdated', notes: 'no longer relevant' }
}),
})
);
});
});
it('displays error when request fails', async () => {
(global.fetch as any).mockResolvedValue({
ok: false,
status: 500,
});
const customMethod = createMockCustomMethod('archive', null);
const resourceInstance = createMockResourceInstance('books/123');
render(<CustomMethodComponent resourceInstance={resourceInstance} customMethod={customMethod} />);
fireEvent.click(screen.getByRole('button', { name: 'Submit' }));
await waitFor(() => {
expect(screen.getByText(/"error":/)).toBeInTheDocument();
});
});
it('shows loading state during submission', async () => {
let resolvePromise: (value: any) => void;
const promise = new Promise((resolve) => {
resolvePromise = resolve;
});
(global.fetch as any).mockReturnValue(promise);
const customMethod = createMockCustomMethod('archive', null);
const resourceInstance = createMockResourceInstance('books/123');
render(<CustomMethodComponent resourceInstance={resourceInstance} customMethod={customMethod} />);
fireEvent.click(screen.getByRole('button', { name: 'Submit' }));
await waitFor(() => {
expect(screen.getByRole('button', { name: 'Submitting...' })).toBeDisabled();
});
resolvePromise!({ ok: true, json: async () => ({ success: true }) });
await waitFor(() => {
expect(screen.getByRole('button', { name: 'Submit' })).not.toBeDisabled();
});
});
});