forked from Axionvera/axionvera-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmswExample.ts
More file actions
330 lines (279 loc) · 9.26 KB
/
Copy pathmswExample.ts
File metadata and controls
330 lines (279 loc) · 9.26 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
* Example: How SDK consumers can use MSW handlers in their own application tests
*
* This demonstrates the complete setup for testing an application that uses the Axionvera SDK
*/
import { StellarClient, server, setupMswTest, overrideHandlers, healthHandler, accountHandler } from '../src/index';
// Example 1: Basic MSW setup for consumer tests
describe('Consumer Application Tests with MSW', () => {
let client: StellarClient;
// Use the provided setup helper
setupMswTest();
beforeEach(() => {
client = new StellarClient({
network: 'testnet',
retryConfig: {
maxRetries: 2,
enabled: true
}
});
});
it('should test application workflow with mocked Stellar API', async () => {
// Test your application logic that uses the SDK
const health = await client.getHealth();
expect(health.status).toBe('healthy');
});
});
// Example 2: Custom handlers for specific test scenarios
describe('Custom Test Scenarios', () => {
let client: StellarClient;
setupMswTest();
beforeEach(() => {
client = new StellarClient({ network: 'testnet' });
});
it('should handle rate limiting scenario', async () => {
// Override the default handler for this specific test
overrideHandlers(
// Mock rate limit response
healthHandler.use(
(req, res, ctx) => {
return res(
ctx.status(429),
ctx.json({ error: 'Rate limit exceeded' })
);
}
)
);
// Test how your application handles rate limits
await expect(client.getHealth()).rejects.toEqual(
expect.objectContaining({
response: expect.objectContaining({
status: 429
})
})
);
});
it('should handle account not found scenario', async () => {
// Override account handler for specific test
overrideHandlers(
accountHandler.use(
(req, res, ctx) => {
return res(
ctx.status(404),
ctx.json({ error: 'Account not found' })
);
}
)
);
await expect(client.getAccount('nonexistent-account')).rejects.toEqual(
expect.objectContaining({
response: expect.objectContaining({
status: 404
})
})
);
});
});
// Example 3: Testing complex application workflows
describe('Complex Application Workflows', () => {
let client: StellarClient;
setupMswTest();
beforeEach(() => {
client = new StellarClient({
network: 'testnet',
retryConfig: {
maxRetries: 3,
baseDelayMs: 100,
enabled: true
}
});
});
it('should test complete user onboarding flow', async () => {
// Simulate a complete user onboarding workflow
const accountId = 'GD5JPQ7VKFOVRWPOEX74JYXHHFNTFZ2JE5WZ4K2MWTROVHMWHD7KUZ2V';
// Step 1: Check network health
const health = await client.getHealth();
expect(health.status).toBe('healthy');
// Step 2: Get network configuration
const network = await client.getNetwork();
expect(network.passphrase).toBeDefined();
// Step 3: Check if account exists
try {
const account = await client.getAccount(accountId);
console.log('Account exists:', account.accountId());
} catch (error) {
console.log('Account does not exist, creating new account...');
// Your application logic for creating a new account
}
// Step 4: Get latest ledger for transaction context
const ledger = await client.getLatestLedger();
expect(ledger.sequence).toBeDefined();
});
it('should test transaction submission workflow', async () => {
// Mock a complete transaction workflow
let transactionId = '';
// Override handlers to track transaction submission
overrideHandlers(
// Mock transaction submission
rest.post('https://soroban-testnet.stellar.org/transactions', (req, res, ctx) => {
transactionId = 'generated-transaction-id-' + Date.now();
return res(
ctx.status(200),
ctx.json({
hash: transactionId,
ledger: 123456,
envelope_xdr: 'AAAAAgAAAAA...',
result_xdr: 'AAAAAgAAAAA...',
result_meta_xdr: 'AAAAAgAAAAA...'
})
);
}),
// Mock transaction retrieval
rest.get(`https://soroban-testnet.stellar.org/transactions/:transactionId`, (req, res, ctx) => {
return res(
ctx.status(200),
ctx.json({
status: 'SUCCESS',
latest_ledger: 123456,
latest_ledger_close_time: 1640995200,
oldest_ledger: 123450,
oldest_ledger_close_time: 1640991600,
application_order: 1,
id: req.params.transactionId,
hash: req.params.transactionId
})
);
})
);
// Mock transaction
const mockTransaction = {
toXDR: () => 'AAAAAgAAAAA=='
} as any;
// Submit transaction
const result = await client.sendTransaction(mockTransaction);
expect(result.hash).toBe(transactionId);
// Poll for completion
const transaction = await client.pollTransaction(result.hash, {
timeoutMs: 5000,
intervalMs: 100
});
expect(transaction).toEqual(expect.objectContaining({
status: 'SUCCESS'
}));
});
});
// Example 4: Performance testing with MSW
describe('Performance Tests', () => {
let client: StellarClient;
setupMswTest();
beforeEach(() => {
client = new StellarClient({ network: 'testnet' });
});
it('should handle concurrent requests efficiently', async () => {
const startTime = Date.now();
// Create multiple concurrent requests
const promises = Array(20).fill(null).map(async (_, index) => {
const health = await client.getHealth();
return { index, health };
});
const results = await Promise.all(promises);
const endTime = Date.now();
// Verify all requests succeeded
expect(results).toHaveLength(20);
results.forEach(result => {
expect(result.health).toEqual({ status: 'healthy', version: '20.0.0' });
});
// Verify performance (should complete reasonably quickly)
const duration = endTime - startTime;
console.log(`Completed 20 concurrent requests in ${duration}ms`);
expect(duration).toBeLessThan(5000); // Should complete within 5 seconds
});
});
// Example 5: Integration with testing frameworks
describe('Framework Integration Examples', () => {
// Example of how to integrate with different testing frameworks
it('should work with Jest (as shown above)', async () => {
const client = new StellarClient({ network: 'testnet' });
const health = await client.getHealth();
expect(health.status).toBe('healthy');
});
// Example for Vitest (if using Vitest instead of Jest)
/*
import { beforeAll, afterEach, afterAll, expect, test } from 'vitest';
import { server } from '../src/test/msw/server';
beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
test('should work with Vitest', async () => {
const client = new StellarClient({ network: 'testnet' });
const health = await client.getHealth();
expect(health.status).toBe('healthy');
});
*/
});
// Example 6: Custom mock data for specific scenarios
describe('Custom Mock Data Scenarios', () => {
let client: StellarClient;
setupMswTest();
beforeEach(() => {
client = new StellarClient({ network: 'testnet' });
});
it('should test with custom account data', async () => {
const customAccountData = {
id: 'CUSTOM_ACCOUNT_ID',
account_id: 'CUSTOM_ACCOUNT_ID',
sequence: 999999999,
subentry_count: 5,
last_modified_ledger: 999999,
threshold: { low_threshold: 2, med_threshold: 3, high_threshold: 4 },
flags: { auth_required: true, auth_revocable: true, auth_immutable: false },
balances: [
{ balance: '5000.0000000', asset_type: 'native' },
{ balance: '1000.0000000', asset_type: 'credit_alphanum4', asset_code: 'USD', asset_issuer: 'ISSUER_ID' }
],
signers: [
{ key: 'SIGNER_KEY_1', weight: 2 },
{ key: 'SIGNER_KEY_2', weight: 1 }
],
data: {
custom_key: 'custom_value'
}
};
overrideHandlers(
rest.get('https://soroban-testnet.stellar.org/accounts/:accountId', (req, res, ctx) => {
return res(
ctx.status(200),
ctx.json(customAccountData)
);
})
);
const account = await client.getAccount('CUSTOM_ACCOUNT_ID');
expect(account.accountId()).toBe('CUSTOM_ACCOUNT_ID');
// Test your application logic with this custom data
});
});
// Export examples for documentation
export const MSW_USAGE_EXAMPLES = {
basicSetup: `
import { setupMswTest, StellarClient } from 'axionvera-sdk';
describe('My App Tests', () => {
setupMswTest();
it('should work', async () => {
const client = new StellarClient();
const health = await client.getHealth();
expect(health.status).toBe('healthy');
});
});
`,
customHandlers: `
import { overrideHandlers, rest } from 'axionvera-sdk';
it('should test error scenario', async () => {
overrideHandlers(
rest.get('https://soroban-testnet.stellar.org/health', (req, res, ctx) => {
return res(ctx.status(500));
})
);
// Test your error handling
});
`
};