Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Commit

Permalink
test: made unit tests getFeedSubmissionResult and modify parseResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
justinemmanuelmercado authored and moltar committed Jun 28, 2020
1 parent 1e3ad85 commit a03d3ad
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
31 changes: 14 additions & 17 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,19 +247,11 @@ const defaultFetch = ({ url, method, headers, data }: Request): Promise<RequestR
return Promise.reject(error.response.data)
})

const parseGetReport = (response: RequestResponse): [string, RequestMeta] => [
response.data,
{
requestId: response.headers['x-mws-request-id'],
timestamp: new Date(response.headers['x-mws-timestamp']),
quotaMax: Number(response.headers['x-mws-quota-max']),
quotaRemaining: Number(response.headers['x-mws-quota-remaining']),
quotaResetOn: new Date(response.headers['x-mws-quota-resetson']),
},
]

const parseResponse = <T>(response: RequestResponse): [T, RequestMeta] => {
const responseData = parser.parse(response.data)
const parseResponse = <T>(
response: RequestResponse,
parseString = false,
): [T | string, RequestMeta] => {
const responseData = parseString ? response.data : parser.parse(response.data)
return [
responseData,
{
Expand Down Expand Up @@ -334,10 +326,15 @@ export class HttpClient {
throw new InvalidUPCIdentifier(`${info.action} request failed`)
}

// GetReport returns a string that should be treated as a file instead of XML data
// http://docs.developer.amazonservices.com/en_CA/reports/Reports_GetReport.html
if (info.action === 'GetReport') {
return parseGetReport(response)
/**
* GetReport and GetFeedSubmissionResult returns a string that should be treated as a file instead of XML data
* Get Report
* http://docs.developer.amazonservices.com/en_CA/reports/Reports_GetReport.html
* GetFeedSubmissionResult
* http://docs.developer.amazonservices.com/en_CA/feeds/Feeds_GetFeedSubmissionResult.html
*/
if (info.action === 'GetReport' || info.action === 'GetFeedSubmissionResult') {
return parseResponse(response, true)
}

return parseResponse(response)
Expand Down
18 changes: 18 additions & 0 deletions test/unit/feeds.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@ import { NextToken } from '../../src/parsing'
import { createMockHttpClient, mockMwsFail, parsingError } from '../utils'

describe('feeds', () => {
describe('getFeedSubmissionResult', () => {
it('returns an XML file when succesful', async () => {
expect.assertions(1)

const mockGetFeedSubmissionResult = createMockHttpClient('feeds_get_feed_submission_result')

expect(await mockGetFeedSubmissionResult.feeds.getFeedSubmissionResult()).toMatchSnapshot()
})

it('throws a parsing error when the response isnt valid', async () => {
expect.assertions(1)

await expect(() => mockMwsFail.feeds.getFeedSubmissionResult()).rejects.toStrictEqual(
new ParsingError(parsingError),
)
})
})

describe('cancelFeedSubmissions', () => {
it('returns count and submission info of canceled feed submissions', async () => {
expect.assertions(1)
Expand Down

0 comments on commit a03d3ad

Please sign in to comment.