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

Commit

Permalink
fix: unit test utils, finances and fulfillment inv unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
justinemmanuelmercado authored and moltar committed Jun 28, 2020
1 parent f08cb36 commit d1b37a7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 91 deletions.
40 changes: 2 additions & 38 deletions test/unit/finances.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { amazonMarketplaces, HttpClient, ParsingError } from '../../src'
import { MWS } from '../../src/mws'
import { ParsingError } from '../../src'
import { NextToken } from '../../src/parsing'
import { getFixture } from '../utils'
import { createMockHttpClient, mockMwsFail, mockMwsServiceStatus, parsingError } from '../utils'

function mockFunctions() {
/**
Expand All @@ -24,38 +23,6 @@ function mockFunctions() {
}
jest.mock('purify-ts', () => mockFunctions())

const httpConfig = {
awsAccessKeyId: '',
marketplace: amazonMarketplaces.CA,
mwsAuthToken: '',
secretKey: '',
sellerId: '',
}

const headers = {
'x-mws-request-id': '0',
'x-mws-timestamp': '2020-05-06T09:22:23.582Z',
'x-mws-quota-max': '1000',
'x-mws-quota-remaining': '999',
'x-mws-quota-resetson': '2020-04-06T10:22:23.582Z',
}

const createMockHttpClient = (fixture: string) =>
new MWS(
new HttpClient(httpConfig, () =>
Promise.resolve({
data: getFixture(fixture),
headers,
}),
),
)

const mockMwsFail = new MWS(
new HttpClient(httpConfig, () => Promise.resolve({ data: '', headers: {} })),
)

const parsingError = 'Expected an object, but received a string with value ""'

describe('finances', () => {
describe('listFinancialEventsByNextToken', () => {
const mockNextToken = new NextToken('ListFinancialEvents', '123')
Expand Down Expand Up @@ -178,9 +145,6 @@ describe('finances', () => {
describe('getServiceStatus', () => {
it('returns a parsed model when the status response is valid', async () => {
expect.assertions(1)

const mockMwsServiceStatus = createMockHttpClient('get_service_status')

expect(await mockMwsServiceStatus.finances.getServiceStatus()).toMatchSnapshot()
})

Expand Down
67 changes: 14 additions & 53 deletions test/unit/fulfillment-inventory.test.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,6 @@
import { amazonMarketplaces, HttpClient, ParsingError } from '../../src'
import { MWS } from '../../src/mws'
import { ParsingError } from '../../src'
import { NextToken } from '../../src/parsing'
import { getFixture } from '../utils'

const httpConfig = {
awsAccessKeyId: '',
marketplace: amazonMarketplaces.CA,
mwsAuthToken: '',
secretKey: '',
sellerId: '',
}

const headers = {
'x-mws-request-id': '0',
'x-mws-timestamp': '2020-05-06T09:22:23.582Z',
'x-mws-quota-max': '1000',
'x-mws-quota-remaining': '999',
'x-mws-quota-resetson': '2020-04-06T10:22:23.582Z',
}
const mockMwsInventorySupply = new MWS(
new HttpClient(httpConfig, () =>
Promise.resolve({
data: getFixture('fulfillment_inventory_list_inventory_supply'),
headers,
}),
),
)

const mockMwsInventorySupplyNT = new MWS(
new HttpClient(httpConfig, () =>
Promise.resolve({
data: getFixture('fulfillment_inventory_list_inventory_supply_nt'),
headers,
}),
),
)

const mockMwsServiceStatus = new MWS(
new HttpClient(httpConfig, () =>
Promise.resolve({
data: getFixture('get_service_status'),
headers,
}),
),
)

const mockMwsFail = new MWS(
new HttpClient(httpConfig, () => Promise.resolve({ data: '', headers: {} })),
)

const mockNextTokenInventorySupply = new NextToken('ListInventorySupply', '123')

const parsingError = 'Expected an object, but received a string with value ""'
import { createMockHttpClient, mockMwsFail, mockMwsServiceStatus, parsingError } from '../utils'

describe('fulfillment-inventory', () => {
describe('listInventorySupply', () => {
Expand All @@ -61,6 +10,11 @@ describe('fulfillment-inventory', () => {

it('returns a parsed model when the response is valid', async () => {
expect.assertions(1)

const mockMwsInventorySupply = createMockHttpClient(
'fulfillment_inventory_list_inventory_supply',
)

expect(
await mockMwsInventorySupply.fulfillmentInventory.listInventorySupply(parameters),
).toMatchSnapshot()
Expand All @@ -76,8 +30,15 @@ describe('fulfillment-inventory', () => {
})

describe('listInventorySupplyByNextToken', () => {
const mockNextTokenInventorySupply = new NextToken('ListInventorySupply', '123')

it('returns a parsed model when the response is valid', async () => {
expect.assertions(1)

const mockMwsInventorySupplyNT = createMockHttpClient(
'fulfillment_inventory_list_inventory_supply_nt',
)

expect(
await mockMwsInventorySupplyNT.fulfillmentInventory.listInventorySupplyByNextToken(
mockNextTokenInventorySupply,
Expand Down
36 changes: 36 additions & 0 deletions test/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
import { readFileSync } from 'fs'
import { join } from 'path'

import { amazonMarketplaces, HttpClient, MWS } from '../src'

const httpConfig = {
awsAccessKeyId: '',
marketplace: amazonMarketplaces.CA,
mwsAuthToken: '',
secretKey: '',
sellerId: '',
}

const headers = {
'x-mws-request-id': '0',
'x-mws-timestamp': '2020-05-06T09:22:23.582Z',
'x-mws-quota-max': '1000',
'x-mws-quota-remaining': '999',
'x-mws-quota-resetson': '2020-04-06T10:22:23.582Z',
}

export const getFixture = (filename: string): string =>
readFileSync(join(__dirname, `unit/__fixtures__/${filename}.xml`), { encoding: 'utf8' })

export const createMockHttpClient = (fixture: string) =>
new MWS(
new HttpClient(httpConfig, () =>
Promise.resolve({
data: getFixture(fixture),
headers,
}),
),
)

export const mockMwsFail = new MWS(
new HttpClient(httpConfig, () => Promise.resolve({ data: '', headers: {} })),
)

export const parsingError = 'Expected an object, but received a string with value ""'

export const mockMwsServiceStatus = createMockHttpClient('get_service_status')

0 comments on commit d1b37a7

Please sign in to comment.