From d1b37a700ceb5a430c803ae4605ef408e6cc5e30 Mon Sep 17 00:00:00 2001 From: EJ Mercado Date: Mon, 22 Jun 2020 16:11:56 +0800 Subject: [PATCH] fix: unit test utils, finances and fulfillment inv unit tests --- test/unit/finances.test.ts | 40 +-------------- test/unit/fulfillment-inventory.test.ts | 67 ++++++------------------- test/utils.ts | 36 +++++++++++++ 3 files changed, 52 insertions(+), 91 deletions(-) diff --git a/test/unit/finances.test.ts b/test/unit/finances.test.ts index b860e812..858bb850 100644 --- a/test/unit/finances.test.ts +++ b/test/unit/finances.test.ts @@ -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() { /** @@ -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') @@ -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() }) diff --git a/test/unit/fulfillment-inventory.test.ts b/test/unit/fulfillment-inventory.test.ts index 81f4c212..5adbe184 100644 --- a/test/unit/fulfillment-inventory.test.ts +++ b/test/unit/fulfillment-inventory.test.ts @@ -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', () => { @@ -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() @@ -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, diff --git a/test/utils.ts b/test/utils.ts index 0a442615..e5789314 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -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')