Skip to content

Commit

Permalink
Added test cases all shopping api's (#85)
Browse files Browse the repository at this point in the history
* v2.7.7

* added test for shopping api
  • Loading branch information
pajaydev authored Apr 28, 2020
1 parent c98f5fd commit c36e6b3
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 44 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "./src/index.js",
"homepage": "https://github.com/pajaydev/ebay-node-api",
"scripts": {
"lint": "eslint src/*.js",
"lint": "eslint src/*.js test/*.js",
"test": "mocha && npm run lint",
"docs": "docsify init ./docs",
"serve-docs": "docsify serve docs",
Expand Down
11 changes: 8 additions & 3 deletions src/shopping.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,42 @@ const makeString = require('make-string');

const getAllCategories = function (categoryID) {
const requestURL = `${urlObject.buildShoppingUrl(this.options, 'GetCategoryInfo')}&${stringifyUrl({ 'CategoryID': categoryID || -1 })}`;
console.log(requestURL);
return getRequest(requestURL).then((data) => {
return JSON.parse(data);
}, console.error // eslint-disable-line no-console
);
};

const getUserDetails = function (input) {
if (!input || typeof input !== 'object') throw new Error('Invalid input');
if (!input || typeof input !== 'object') throw new Error('invalid_request_error -> Invalid input');
if (!input.userId) throw new Error('invalid_request_error -> userId is null or invalid');
const requestUrl = `${urlObject.buildShoppingUrl(this.options, 'GetUserProfile')}&${stringifyUrl(input)}`;
console.log(requestUrl);
return getRequest(requestUrl).then((data) => {
return JSON.parse(data);
}, console.error // eslint-disable-line no-console
);
};

const getItemStatus = function (itemIds) {
if (!itemIds) throw new Error('invalid_request_error -> itemIds is null or invalid');
if (!itemIds) throw new Error('invalid_request_error -> Item ID is null or invalid');
const paramsObj = {
'ItemID': makeString(itemIds, { braces: 'false', quotes: 'no' })
};
const requestUrl = `${urlObject.buildShoppingUrl(this.options, 'GetItemStatus')}&${stringifyUrl(paramsObj)}`;
console.log(requestUrl);
return getRequest(requestUrl).then((data) => {
return JSON.parse(data);
}, console.error // eslint-disable-line no-console
);
};

const getShippingCosts = function (input) {
if (!input || typeof input !== 'object') throw new Error('Invalid input');
if (!input || typeof input !== 'object') throw new Error('invalid_request_error -> Invalid input');
if (!input.itemId) throw new Error('invalid_request_error -> Item id is null or invalid');
const url = `${urlObject.buildShoppingUrl(this.options, 'GetShippingCosts')}&${stringifyUrl(input)} `;
console.log(url);
return getRequest(url).then((data) => {
return JSON.parse(data);
}, console.error // eslint-disable-line no-console
Expand All @@ -52,6 +56,7 @@ const getShippingCosts = function (input) {
const getMultipleItems = function (options) {
if (!options || !options.itemId) throw new Error('invalid_request_error -> Item ID is null or invalid');
const requestUrl = `${urlObject.buildShoppingUrl(this.options, 'GetMultipleItems')}&${stringifyUrl({ 'itemId': makeString(options.itemId, { braces: 'false', quotes: 'no' }) })}`;
console.log(requestUrl);
return getRequest(requestUrl).then((data) => {
return JSON.parse(data);
}, console.error // eslint-disable-line no-console
Expand Down
12 changes: 6 additions & 6 deletions test/buildURL.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('test building url methods', () => {


it('test search url', () => {
let expected_search_url = 'https://svcs.ebay.com/services/search/FindingService/v1?SECURITY-APPNAME=testID&OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.0.0&RESPONSE-DATA-FORMAT=JSON&keywords=iphone&outputSelector(0)=SellerInfo&outputSelector(1)=PictureURLLarge&paginationInput.entriesPerPage=6&GLOBAL-ID=EBAY-US';
let expectedSearchUrl = 'https://svcs.ebay.com/services/search/FindingService/v1?SECURITY-APPNAME=testID&OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.0.0&RESPONSE-DATA-FORMAT=JSON&keywords=iphone&outputSelector(0)=SellerInfo&outputSelector(1)=PictureURLLarge&paginationInput.entriesPerPage=6&GLOBAL-ID=EBAY-US';
let options = {
name: 'iphone',
operationName: 'findItemsByKeywords',
Expand All @@ -17,29 +17,29 @@ describe('test building url methods', () => {
globalID: 'EBAY-US',
baseSvcUrl: 'svcs.ebay.com'
};
expect(buildURL.buildSearchUrl(options, 'findItemsByKeywords')).to.be.equal(expected_search_url);
expect(buildURL.buildSearchUrl(options, 'findItemsByKeywords')).to.be.equal(expectedSearchUrl);
});

it('test Shopping url without selector', () => {
let expected_search_url = 'https://open.api.ebay.com/Shopping?appid=testID&callname=demoShoppingName&version=967&siteid=0&responseencoding=JSON';
let expectedSearchUrl = 'https://open.api.ebay.com/Shopping?appid=testID&callname=demoShoppingName&version=967&siteid=0&responseencoding=JSON';
let options = {
name: 'iphone',
param: 'keywords',
clientID: 'testID',
baseUrl: 'open.api.ebay.com'
};
expect(buildURL.buildShoppingUrl(options, 'demoShoppingName')).to.be.equal(expected_search_url);
expect(buildURL.buildShoppingUrl(options, 'demoShoppingName')).to.be.equal(expectedSearchUrl);
});

it('test Shopping url including selector', () => {
let expected_search_url = 'https://open.api.ebay.com/Shopping?appid=testID&callname=demoShoppingName&version=967&siteid=0&responseencoding=JSON&IncludeSelector=true';
let expectedSearchUrl = 'https://open.api.ebay.com/Shopping?appid=testID&callname=demoShoppingName&version=967&siteid=0&responseencoding=JSON&IncludeSelector=true';
let options = {
name: 'iphone',
param: 'keywords',
clientID: 'testID',
includeSelector: true,
baseUrl: 'open.api.ebay.com'
};
expect(buildURL.buildShoppingUrl(options, 'demoShoppingName')).to.be.equal(expected_search_url);
expect(buildURL.buildShoppingUrl(options, 'demoShoppingName')).to.be.equal(expectedSearchUrl);
});
});
8 changes: 4 additions & 4 deletions test/common.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ const { parseObj } = require('../src/common-utils/index');

describe('test common util methods', () => {
it('test parse object to query params', () => {
const expected_param = '&keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest';
const expectedParam = '&keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest';
const options = {
keywords: 'iphone',
categoryId: '111',
sortOrder: 'PricePlusShippingLowest'
};
const emptyOptions = {};
expect(parseObj(options)).to.be.equal(expected_param);
expect(parseObj(options)).to.be.equal(expectedParam);
expect(parseObj(emptyOptions)).to.be.equal('');
expect(parseObj(options, 'userName=ebay')).to.be.equal(`userName=ebay${expected_param}`);
expect(parseObj(options, 'userName=ebay')).to.be.equal(`userName=ebay${expectedParam}`);
});
});
});
8 changes: 4 additions & 4 deletions test/findItemsByKeyword.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const nock = require('nock');
const eBay = require('../src/index');
const Ebay = require('../src/index');
let expect = require('chai').expect;

describe('Test find items by keyword method', () => {
Expand All @@ -22,9 +22,9 @@ describe('Test find items by keyword method', () => {
});

it('test input parameter in findItemsByKeyword method', () => {
let ebay = new eBay({
let ebay = new Ebay({
clientID: 'ClientId'
})
expect(() => { ebay.findItemsByKeywords() }).to.throw('Keyword is missing, Keyword is required');
});
expect(() => { ebay.findItemsByKeywords(); }).to.throw('Keyword is missing, Keyword is required');
});
});
35 changes: 18 additions & 17 deletions test/findingApi.test.js → test/finding.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
'use strict';
const expect = require('chai').expect;
const should = require('chai').should();
const nock = require('nock');
const eBay = require('../src/index');
const Ebay = require('../src/index');
const { constructAdditionalParams } = require('../src/findingApi');
const nockFindingApi = nock('https://svcs.ebay.com/');

describe('test ebay finding Api', () => {

describe('test findingApi methods with required params', () => {
it('test findItemsByCategory with required params', () => {
let ebay = new eBay({
let ebay = new Ebay({
clientID: 'ClientId'
});
expect(() => { ebay.findItemsByCategory(); }).to.throw('Category ID is null or invalid');
});

it('test findCompletedItemswith required params', () => {
let ebay = new eBay({
let ebay = new Ebay({
clientID: 'ClientId'
});
expect(() => { ebay.findCompletedItems(''); }).to.throw('Keyword or category ID are required.');
Expand All @@ -25,20 +26,20 @@ describe('test ebay finding Api', () => {

describe('test constructAdditionalParams', () => {
it('test constructAdditionalParams with required params', () => {
let expected_param = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest';
let expectedParam = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest';
const options = {
keywords: 'iphone',
categoryId: '111',
sortOrder: 'PricePlusShippingLowest'
};
const emptyOptions = {};
expect(constructAdditionalParams(options)).to.be.equal(expected_param);
expect(constructAdditionalParams(options)).to.be.equal(expectedParam);
expect(constructAdditionalParams(emptyOptions)).to.be.equal('');
});

it('test constructAdditionalParams with affiliate params', () => {
let expected_param_with_affiliate = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest&affiliate.trackingId=1234567899&affiliate.networkId=123';
let expected_param = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest';
let expectedParamWithAffiliate = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest&affiliate.trackingId=1234567899&affiliate.networkId=123';
let expectedParam = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest';
const options = {
keywords: 'iphone',
categoryId: '111',
Expand All @@ -55,14 +56,14 @@ describe('test ebay finding Api', () => {
sortOrder: 'PricePlusShippingLowest'
};
const emptyOptions = {};
expect(constructAdditionalParams(options)).to.be.equal(expected_param_with_affiliate);
expect(constructAdditionalParams(optionsWithNoAffiliate)).to.be.equal(expected_param);
expect(constructAdditionalParams(options)).to.be.equal(expectedParamWithAffiliate);
expect(constructAdditionalParams(optionsWithNoAffiliate)).to.be.equal(expectedParam);
expect(constructAdditionalParams(emptyOptions)).to.be.equal('');
});

it('test constructAdditionalParams with additional params', () => {
let expected_param = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest&itemFilter(0).name=Condition&itemFilter(0).value=3000&itemFilter(1).name=SoldItemsOnly&itemFilter(1).value=true';
let expected_pag_param = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest&itemFilter(0).name=Condition&itemFilter(0).value=3000&itemFilter(1).name=SoldItemsOnly&itemFilter(1).value=true&paginationInput.entriesPerPage=2';
let expectedParam = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest&itemFilter(0).name=Condition&itemFilter(0).value=3000&itemFilter(1).name=SoldItemsOnly&itemFilter(1).value=true';
let expectedPaginationParam = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest&itemFilter(0).name=Condition&itemFilter(0).value=3000&itemFilter(1).name=SoldItemsOnly&itemFilter(1).value=true&paginationInput.entriesPerPage=2';
const options = {
keywords: 'iphone',
categoryId: '111',
Expand All @@ -78,18 +79,18 @@ describe('test ebay finding Api', () => {
SoldItemsOnly: true,
entriesPerPage: 2
};
expect(constructAdditionalParams(options)).to.be.equal(expected_param);
expect(constructAdditionalParams(optionsWithPagination)).to.be.equal(expected_pag_param);
expect(constructAdditionalParams(options)).to.be.equal(expectedParam);
expect(constructAdditionalParams(optionsWithPagination)).to.be.equal(expectedPaginationParam);
});
});

describe('test all get apis', () => {
it("test findItemsAdvanced", () => {
let ebay = new eBay({
it('test findItemsAdvanced', () => {
let ebay = new Ebay({
clientID: 'ABCD'
});
nockFindingApi.get('/services/search/FindingService/v1?SECURITY-APPNAME=ABCD&OPERATION-NAME=findItemsAdvanced&SERVICE-VERSION=1.0.0&RESPONSE-DATA-FORMAT=JSON&paginationInput.entriesPerPage=2&keywords=ipad&itemFilter(0).name=ExpeditedShippingType&itemFilter(0).value=OneDayShipping&outputSelector(0)=SellerInfo&outputSelector(1)=PictureURLLarge&GLOBAL-ID=EBAY-US')
.reply(200, { "findItemsAdvancedResponse": [{ "ack": ["Success"] }] });
.reply(200, { 'findItemsAdvancedResponse': [{ 'ack': ['Success'] }] });
return ebay.findItemsAdvanced({
entriesPerPage: 2,
keywords: 'ipad',
Expand All @@ -101,4 +102,4 @@ describe('test ebay finding Api', () => {
});
});
});
});
});
41 changes: 32 additions & 9 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,54 @@
let expect = require('chai').expect;
let should = require('chai').should();
let eBay = require('../src/index');
let Ebay = require('../src/index');

describe('check all the options provided is valid or not - Ebay Constructor ', () => {
it('check input is provided or not', () => {
expect(() => {
new eBay();
new Ebay();
}).to.throw('Options is missing, please provide the input');
});

it('should have client ID', () => {
let ebayApi = new eBay({ clientID: '12345' });
let ebayApi = new Ebay({ clientID: '12345' });
ebayApi.options.should.have.property('clientID');
});

it('should not have client ID', () => {
expect(() => {
new eBay({});
new Ebay({});
}).to.throw('Client ID is Missing\ncheck documentation to get Client ID http://developer.ebay.com/DevZone/account/');
});

it('check instance of Ebay', () => {
let ebayApi = new eBay({ clientID: '12345' });
expect(ebayApi).to.be.a.instanceOf(eBay);
let ebayApi = new Ebay({ clientID: '12345' });
expect(ebayApi).to.be.a.instanceOf(Ebay);
});

});


it('test default params', () => {
const ebay = new Ebay({
clientID: 'ClientId'
});
const expected = {
clientID: 'ClientId',
env: 'PROD',
baseUrl: 'api.ebay.com',
baseSvcUrl: 'svcs.ebay.com',
globalID: 'EBAY-US',
siteId: '0'
};
expect(ebay.options).to.deep.equal(expected);
});

it('test site id, env and country code', () => {
const ebay = new Ebay({
clientID: 'ClientId',
siteId: 3,
env: 'SANDBOX',
countryCode: 'EBAY_UK'
});
expect(ebay.options.siteId).to.equals(3);
expect(ebay.options.env).to.equals('SANDBOX');
expect(ebay.options.globalID).to.equals('EBAY_UK');
});
});
Loading

0 comments on commit c36e6b3

Please sign in to comment.