From cb7643bf4ecd5aa1fac71f81072914e503bd5543 Mon Sep 17 00:00:00 2001 From: Marco Aurelio Date: Wed, 6 Jun 2018 19:03:01 -0300 Subject: [PATCH] Add more tests --- tests/index.ts | 95 +++++++++++++++++++++++++++++++++++++-------- tests/tsconfig.json | 4 ++ 2 files changed, 83 insertions(+), 16 deletions(-) create mode 100644 tests/tsconfig.json diff --git a/tests/index.ts b/tests/index.ts index ae7dd7b..9e236f5 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -1,7 +1,9 @@ -import Zoop from '../lib'; +import Zoop, { Marketplace } from '../lib'; const TEST_API_KEY = 'zpk_test_EzCkzFFKibGQU6HFq7EYVuxI'; +const TEST_NONEXISTENT_ID = '87df28a2236e173b45ea8b0d049cf62b'; const TEST_MARKETPLACE_ID = '3249465a7753536b62545a6a684b0000'; +const TEST_BUYER_ID = '19355ed168914b57b564e23ce41b48c3'; const TEST_BUYER_CREATION_INFO = { first_name: 'Tester', last_name: 'McTester', @@ -17,26 +19,87 @@ const TEST_BUYER_CREATION_INFO = { neighborhood: 'Test', }, }; +const TEST_TRANSACTION_ID = '01ee71b399144a20822ea93c965974d3'; -it('should create the API client', async () => { - new Zoop(TEST_API_KEY); +let zoop: Zoop; +let marketplace: Marketplace; + +beforeAll(() => { + zoop = new Zoop(TEST_API_KEY); + marketplace = zoop.marketplace(TEST_MARKETPLACE_ID); }); -it('should retrieve marketplace info', async () => { - const zoop = new Zoop(TEST_API_KEY); - const marketplaceInfo = await zoop.marketplace(TEST_MARKETPLACE_ID).get(); - expect(marketplaceInfo).toBeDefined(); +describe('marketplace', () => { + it('should retrieve marketplace info', async () => { + const marketplaceInfo = await zoop.marketplace(TEST_MARKETPLACE_ID).get(); + expect(marketplaceInfo).toBeDefined(); + expect(marketplaceInfo).toBeInstanceOf(Object); + expect(marketplaceInfo!.id).toEqual(TEST_MARKETPLACE_ID); + }); + + it('should throw when attempting to access another marketplace', async () => { + const marketplaceInfoPromise = zoop.marketplace(TEST_NONEXISTENT_ID).get(); + expect(marketplaceInfoPromise).rejects.toThrow(); + }); }); -it('should create/retrieve buyers', async () => { - const zoop = new Zoop(TEST_API_KEY); - const marketplace = zoop.marketplace(TEST_MARKETPLACE_ID); +describe('buyer', () => { + it('should create buyer', async () => { + const buyerInfo = await marketplace.createBuyer(TEST_BUYER_CREATION_INFO); + expect(buyerInfo).toBeDefined(); + expect(buyerInfo).toBeInstanceOf(Object); + expect(typeof buyerInfo.id).toBe('string'); + }); + + it('should retrieve buyer', async () => { + const buyerInfo = await marketplace.buyer(TEST_BUYER_ID).get(); + expect(buyerInfo).toBeDefined(); + expect(buyerInfo).toBeInstanceOf(Object); + expect(buyerInfo!.id).toEqual(TEST_BUYER_ID); + }); + + it('should return undefined when buyer does not exist', async () => { + const buyerInfo = await marketplace.buyer(TEST_NONEXISTENT_ID).get(); + expect(buyerInfo).toBeUndefined(); + }); + + it('should list buyers', async () => { + let count = 0; + + for await (const buyer of marketplace.listBuyers()) { + expect(buyer).toBeDefined(); + + // No need to iterate over all buyers + if (++count > 200) { + break; + } + } + }); +}); + +describe('transaction', () => { + it('should retrieve transaction', async () => { + const transactionInfo = await marketplace.transaction(TEST_TRANSACTION_ID).get(); + expect(transactionInfo).toBeDefined(); + expect(transactionInfo).toBeInstanceOf(Object); + expect(transactionInfo!.id).toEqual(TEST_TRANSACTION_ID); + }); + + it('should return undefined when transaction does not exist', async () => { + const transactionInfo = await marketplace.transaction(TEST_NONEXISTENT_ID).get(); + expect(transactionInfo).toBeUndefined(); + }); + + it('should list transactions', async () => { + let count = 0; - const createdBuyerInfo = await marketplace.createBuyer(TEST_BUYER_CREATION_INFO); - expect(createdBuyerInfo).toBeDefined(); - expect(typeof createdBuyerInfo.id).toBe('string'); + for await (const transaction of marketplace.listTransactions()) { + expect(transaction).toBeDefined(); - const retrievedBuyerInfo = await marketplace.buyer(createdBuyerInfo.id).get(); - expect(retrievedBuyerInfo).toBeDefined(); - expect(retrievedBuyerInfo).toEqual(createdBuyerInfo); + // No need to iterate over all transactions + if (++count > 200) { + break; + } + } + }); }); diff --git a/tests/tsconfig.json b/tests/tsconfig.json new file mode 100644 index 0000000..7fb25f6 --- /dev/null +++ b/tests/tsconfig.json @@ -0,0 +1,4 @@ +{ + "extends": "../tsconfig.json", + "exclude": [] +}