import processPostData from '../process-post-data'; describe('processPostData 函数', () => { it('当 method 不等于 "POST",data 存在且 runParams 存在时,返回 data', () => { const result = processPostData({ method: 'GET', data: { id: 30 }, runParams: { name: 'John', age: 30 }, }); expect(result).toEqual({ id: 30 }); }); it('当 method 不等于 "POST",data 不存在且 runParams 存在时,返回 undefined', () => { const result = processPostData({ method: 'GET', runParams: { name: 'John', age: 30 }, }); expect(result).toEqual(undefined); }); it('当 method 不等于 "POST",data 存在且 runParams 不存在时,返回 data', () => { const result = processPostData({ method: 'GET', data: { name: 'John', age: 30 }, }); expect(result).toEqual({ name: 'John', age: 30 }); }); it('当 contentType 等于 "application/x-www-form-urlencoded" 时,处理 runParams 和 data,并返回结果', () => { const result = processPostData({ method: 'POST', contentType: 'application/x-www-form-urlencoded', runParams: { name: 'John', age: 30 }, data: { email: 'john@example.com' }, }); expect(result).toBe('name=John&age=30&email=john%40example.com'); }); it('当 contentType 不等于 "application/x-www-form-urlencoded" 时,处理 runParams 和 data,并返回结果', () => { const result = processPostData({ method: 'POST', contentType: 'application/json', runParams: { name: 'John', age: 30 }, data: { email: 'john@example.com' }, }); expect(result).toEqual({ name: 'John', age: 30, email: 'john@example.com', }); }); it('当 formatParam 是存在时,调用 formatParam 并返回结果', () => { const formatParam = jest.fn((params) => `Formatted: ${params}`); const result = processPostData({ contentType: 'application/x-www-form-urlencoded', method: 'POST', runParams: { name: 'John', age: 30 }, data: { email: 'john@example.com' }, formatParam, }); expect(formatParam).toHaveBeenCalledWith( 'name=John&age=30&email=john%40example.com', ); expect(result).toBe('Formatted: name=John&age=30&email=john%40example.com'); }); it('当 formatParam 不存在时,返回处理后的结果', () => { const result = processPostData({ method: 'POST', runParams: { name: 'John', age: 30 }, data: { email: 'john@example.com' }, }); expect(result).toEqual({ name: 'John', age: 30, email: 'john@example.com', }); }); it('当 data 不存在,runParams 存在时,返回值为 runParams', () => { const options = { contentType: 'application/json', method: 'POST', runParams: { key: 'value' }, }; const result = processPostData(options); expect(result).toEqual(options.runParams); }); it('当 data 存在,runParams 不存在时,返回值为 data', () => { const options = { contentType: 'application/json', method: 'POST', data: { key: 'value' }, }; const result = processPostData(options); expect(result).toEqual(options.data); }); it('当 data 不存在,runParams 存在且 contentType 不是 "application/x-www-form-urlencoded" 时,返回 runParams 的值', () => { const options = { contentType: 'application/json', method: 'POST', runParams: { id: 1 }, }; const result = processPostData(options); expect(result).toEqual(options.runParams); }); it('当 data 不存在,runParams 存在且 contentType 是 "application/x-www-form-urlencoded" 时,处理 runParams,并返回结果', () => { const options = { contentType: 'application/x-www-form-urlencoded', method: 'POST', runParams: { id: 1 }, }; const result = processPostData(options); expect(result).toBe('id=1'); }); it('当 data 存在,runParams 不存在且 contentType 不是 "application/x-www-form-urlencoded" 时,返回 data 的值', () => { const options = { contentType: 'application/json', method: 'POST', data: { name: 'John' }, }; const result = processPostData(options); expect(result).toEqual(options.data); }); it('当 data 存在,runParams 不存在且 contentType 是 "application/x-www-form-urlencoded" 时,处理 data,并返回结果', () => { const options = { contentType: 'application/x-www-form-urlencoded', method: 'POST', data: { name: 'John', age: 30, email: 'john@example.com' }, }; const result = processPostData(options); expect(result).toBe('name=John&age=30&email=john%40example.com'); }); });