|
1 |
| -import { parseFetchArgs } from '../../src/instrument/fetch'; |
| 1 | +import { parseFetchArgs, parseFetchPayload } from '../../src/instrument/fetch'; |
2 | 2 |
|
3 | 3 | describe('instrument > parseFetchArgs', () => {
|
4 |
| - const data = { name: 'Test' }; |
5 |
| - |
6 | 4 | it.each([
|
7 |
| - ['string URL only', ['http://example.com'], { method: 'GET', url: 'http://example.com', body: null }], |
8 |
| - ['URL object only', [new URL('http://example.com')], { method: 'GET', url: 'http://example.com/', body: null }], |
9 |
| - ['Request URL only', [{ url: 'http://example.com' }], { method: 'GET', url: 'http://example.com', body: null }], |
| 5 | + ['string URL only', ['http://example.com'], { method: 'GET', url: 'http://example.com' }], |
| 6 | + ['URL object only', [new URL('http://example.com')], { method: 'GET', url: 'http://example.com/' }], |
| 7 | + ['Request URL only', [{ url: 'http://example.com' }], { method: 'GET', url: 'http://example.com' }], |
10 | 8 | [
|
11 | 9 | 'Request URL & method only',
|
12 | 10 | [{ url: 'http://example.com', method: 'post' }],
|
13 |
| - { method: 'POST', url: 'http://example.com', body: null }, |
| 11 | + { method: 'POST', url: 'http://example.com' }, |
14 | 12 | ],
|
| 13 | + ['string URL & options', ['http://example.com', { method: 'post' }], { method: 'POST', url: 'http://example.com' }], |
| 14 | + [ |
| 15 | + 'URL object & options', |
| 16 | + [new URL('http://example.com'), { method: 'post' }], |
| 17 | + { method: 'POST', url: 'http://example.com/' }, |
| 18 | + ], |
| 19 | + [ |
| 20 | + 'Request URL & options', |
| 21 | + [{ url: 'http://example.com' }, { method: 'post' }], |
| 22 | + { method: 'POST', url: 'http://example.com' }, |
| 23 | + ], |
| 24 | + ])('%s', (_name, args, expected) => { |
| 25 | + const actual = parseFetchArgs(args as unknown[]); |
| 26 | + |
| 27 | + expect(actual).toEqual(expected); |
| 28 | + }); |
| 29 | +}); |
| 30 | + |
| 31 | +describe('instrument > parseFetchPayload', () => { |
| 32 | + const data = [1, 2, 3]; |
| 33 | + const jsonData = '{"data":[1,2,3]}'; |
| 34 | + |
| 35 | + it.each([ |
| 36 | + ['string URL only', ['http://example.com'], undefined], |
| 37 | + ['URL object only', [new URL('http://example.com')], undefined], |
| 38 | + ['Request URL only', [{ url: 'http://example.com' }], undefined], |
15 | 39 | [
|
16 |
| - 'string URL & options', |
17 |
| - ['http://example.com', { method: 'post', body: JSON.stringify(data) }], |
18 |
| - { method: 'POST', url: 'http://example.com', body: '{"name":"Test"}' }, |
| 40 | + 'Request URL & method only', |
| 41 | + [{ url: 'http://example.com', method: 'post', body: JSON.stringify({ data }) }], |
| 42 | + jsonData, |
19 | 43 | ],
|
| 44 | + ['string URL & options', ['http://example.com', { method: 'post', body: JSON.stringify({ data }) }], jsonData], |
20 | 45 | [
|
21 | 46 | 'URL object & options',
|
22 |
| - [new URL('http://example.com'), { method: 'post', body: JSON.stringify(data) }], |
23 |
| - { method: 'POST', url: 'http://example.com/', body: '{"name":"Test"}' }, |
| 47 | + [new URL('http://example.com'), { method: 'post', body: JSON.stringify({ data }) }], |
| 48 | + jsonData, |
24 | 49 | ],
|
25 | 50 | [
|
26 | 51 | 'Request URL & options',
|
27 |
| - [{ url: 'http://example.com' }, { method: 'post', body: JSON.stringify(data) }], |
28 |
| - { method: 'POST', url: 'http://example.com', body: '{"name":"Test"}' }, |
| 52 | + [{ url: 'http://example.com' }, { method: 'post', body: JSON.stringify({ data }) }], |
| 53 | + jsonData, |
29 | 54 | ],
|
30 | 55 | ])('%s', (_name, args, expected) => {
|
31 |
| - const actual = parseFetchArgs(args as unknown[]); |
| 56 | + const actual = parseFetchPayload(args as unknown[]); |
32 | 57 |
|
33 | 58 | expect(actual).toEqual(expected);
|
34 | 59 | });
|
|
0 commit comments