From 47405e2e3377231d4b05832617b7dee07b9a88e7 Mon Sep 17 00:00:00 2001 From: Will Lopez Date: Wed, 11 Dec 2019 13:14:55 -0600 Subject: [PATCH 1/2] chore: add integration test for surchargeById query Signed-off-by: Will Lopez --- .../surcharges/util/surchargeSchema.js | 4 +- .../surchargeById/SurchargeByIdQuery.graphql | 9 ++++ .../surchargeById/surchargeById.test.js | 54 +++++++++++++++++++ tests/util/factory.js | 5 ++ 4 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 tests/integration/api/queries/surchargeById/SurchargeByIdQuery.graphql create mode 100644 tests/integration/api/queries/surchargeById/surchargeById.test.js diff --git a/src/plugins/surcharges/util/surchargeSchema.js b/src/plugins/surcharges/util/surchargeSchema.js index 60e1dc072e8..b1c80410ad9 100644 --- a/src/plugins/surcharges/util/surchargeSchema.js +++ b/src/plugins/surcharges/util/surchargeSchema.js @@ -54,7 +54,7 @@ const Message = new SimpleSchema({ language: String }); -const surchargeSchema = new SimpleSchema({ +export const Surcharge = new SimpleSchema({ "amount": { type: Number }, @@ -98,4 +98,4 @@ const surchargeSchema = new SimpleSchema({ } }); -export default surchargeSchema; +export default Surcharge; diff --git a/tests/integration/api/queries/surchargeById/SurchargeByIdQuery.graphql b/tests/integration/api/queries/surchargeById/SurchargeByIdQuery.graphql new file mode 100644 index 00000000000..95d971f3c8c --- /dev/null +++ b/tests/integration/api/queries/surchargeById/SurchargeByIdQuery.graphql @@ -0,0 +1,9 @@ + +query ($shopId: ID!, $surchargeId: ID!){ + surchargeById(shopId: $shopId, surchargeId: $surchargeId){ + _id + amount { + amount + } + } +} diff --git a/tests/integration/api/queries/surchargeById/surchargeById.test.js b/tests/integration/api/queries/surchargeById/surchargeById.test.js new file mode 100644 index 00000000000..fdfff602614 --- /dev/null +++ b/tests/integration/api/queries/surchargeById/surchargeById.test.js @@ -0,0 +1,54 @@ +import encodeOpaqueId from "@reactioncommerce/api-utils/encodeOpaqueId.js"; +import importAsString from "@reactioncommerce/api-utils/importAsString.js"; +import Factory from "/tests/util/factory.js"; +import TestApp from "/tests/util/TestApp.js"; + +const SurchargeByIdQuery = importAsString("./SurchargeByIdQuery.graphql"); + +jest.setTimeout(300000); + +const internalShopId = "123"; +const internalSurchargeId = "456"; +const opaqueShopId = encodeOpaqueId("reaction/shop", internalShopId); // reaction/shop:123 +const opaqueSurchargeId = encodeOpaqueId("reaction/surcharge", internalSurchargeId); // reaction/surcharge:456 +const shopName = "Test Shop"; +let testApp; +let surchargeById; + +const mockSurcharge = Factory.Surcharge.makeOne({ + _id: internalSurchargeId, + shopId: internalShopId, + amount: 20 +}); + +beforeAll(async () => { + testApp = new TestApp(); + await testApp.start(); + + await testApp.insertPrimaryShop({ _id: internalShopId, name: shopName }); + await testApp.collections.Surcharges.insertOne(mockSurcharge); + surchargeById = testApp.query(SurchargeByIdQuery); +}); + +afterAll(async () => { + await testApp.collections.Accounts.deleteMany({}); + await testApp.collections.Surcharges.deleteMany({}); + await testApp.collections.Shops.deleteMany({}); + await testApp.stop(); +}); + +test("retrieve a surcharge by its id", async () => { + let result; + + try { + result = await surchargeById({ + shopId: opaqueShopId, + surchargeId: opaqueSurchargeId + }); + } catch (error) { + expect(error).toBeUndefined(); + return; + } + + expect(result.surchargeById.amount.amount).toEqual(20); +}); diff --git a/tests/util/factory.js b/tests/util/factory.js index d278b572bfe..fd63bebd392 100644 --- a/tests/util/factory.js +++ b/tests/util/factory.js @@ -54,6 +54,10 @@ import { Shop } from "../../src/core-services/shop/simpleSchemas.js"; +import { + Surcharge +} from "../../src/plugins/surcharges/util/surchargeSchema.js"; + import { Tag } from "../../src/core-services/tags/simpleSchemas.js"; @@ -104,6 +108,7 @@ const schemasToAddToFactory = { ProductVariant, ShipmentQuote, Shop, + Surcharge, Tag, TaxRates }; From 6b3c975b0c23b585c9a5694cdcb49603c2c2aa16 Mon Sep 17 00:00:00 2001 From: Will Lopez Date: Wed, 11 Dec 2019 16:40:41 -0600 Subject: [PATCH 2/2] chore: add integration test for surcharges query Signed-off-by: Will Lopez --- .../surchargeById/SurchargeByIdQuery.graphql | 1 - .../surcharges/SurchargesQuery.graphql | 10 ++++ .../api/queries/surcharges/surcharges.test.js | 52 +++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 tests/integration/api/queries/surcharges/SurchargesQuery.graphql create mode 100644 tests/integration/api/queries/surcharges/surcharges.test.js diff --git a/tests/integration/api/queries/surchargeById/SurchargeByIdQuery.graphql b/tests/integration/api/queries/surchargeById/SurchargeByIdQuery.graphql index 95d971f3c8c..2ea3e2fc136 100644 --- a/tests/integration/api/queries/surchargeById/SurchargeByIdQuery.graphql +++ b/tests/integration/api/queries/surchargeById/SurchargeByIdQuery.graphql @@ -1,7 +1,6 @@ query ($shopId: ID!, $surchargeId: ID!){ surchargeById(shopId: $shopId, surchargeId: $surchargeId){ - _id amount { amount } diff --git a/tests/integration/api/queries/surcharges/SurchargesQuery.graphql b/tests/integration/api/queries/surcharges/SurchargesQuery.graphql new file mode 100644 index 00000000000..48910853c6e --- /dev/null +++ b/tests/integration/api/queries/surcharges/SurchargesQuery.graphql @@ -0,0 +1,10 @@ + +query ($shopId: ID!, $first: ConnectionLimitInt){ + surcharges(shopId: $shopId, first: $first){ + nodes { + amount { + amount + } + } + } +} diff --git a/tests/integration/api/queries/surcharges/surcharges.test.js b/tests/integration/api/queries/surcharges/surcharges.test.js new file mode 100644 index 00000000000..24401d5eb12 --- /dev/null +++ b/tests/integration/api/queries/surcharges/surcharges.test.js @@ -0,0 +1,52 @@ +import encodeOpaqueId from "@reactioncommerce/api-utils/encodeOpaqueId.js"; +import importAsString from "@reactioncommerce/api-utils/importAsString.js"; +import Factory from "/tests/util/factory.js"; +import TestApp from "/tests/util/TestApp.js"; + +const SurchargesQuery = importAsString("./SurchargesQuery.graphql"); + +jest.setTimeout(300000); + +const internalShopId = "123"; +const opaqueShopId = encodeOpaqueId("reaction/shop", internalShopId); // reaction/shop:123 +const shopName = "Test Shop"; +let testApp; +let surcharges; +let mockSurcharges; + +beforeAll(async () => { + testApp = new TestApp(); + await testApp.start(); + + await testApp.insertPrimaryShop({ _id: internalShopId, name: shopName }); + mockSurcharges = Factory.Surcharge.makeMany(3, { + shopId: internalShopId, + amount: 10 + }); + await testApp.collections.Surcharges.insertMany(mockSurcharges); + surcharges = testApp.query(SurchargesQuery); +}); + +afterAll(async () => { + await testApp.collections.Accounts.deleteMany({}); + await testApp.collections.Surcharges.deleteMany({}); + await testApp.collections.Shops.deleteMany({}); + await testApp.stop(); +}); + +test("retrieve a list of surcharges", async () => { + let result; + + try { + result = await surcharges({ + shopId: opaqueShopId, + first: 3 + }); + } catch (error) { + expect(error).toBeUndefined(); + return; + } + + expect(result.surcharges.nodes[0].amount.amount).toEqual(10); + expect(result.surcharges.nodes.length).toEqual(3); +});