Skip to content

Instantly share code, notes, and snippets.

View lgandecki's full-sized avatar

Łukasz Gandecki lgandecki

View GitHub Profile
//Remember to add this require
const { estimateShipping } = require("./estimateShipping");
exports.resolvers = {
Product: {
__resolveReference(object) {
return {
...object,
...inventory.find(product => product.upc === object.upc)
};
it("should be calculated with estimateShipping based on the price and weight", async () => {
const PRICE = 999;
const WEIGHT = 100;
const mocks = { Product: () => ({ price: PRICE, weight: WEIGHT }) };
td.when(estimateShipping({ price: PRICE, weight: WEIGHT })).thenReturn(99);
const result = await executeGraphql({
query,
service,
const td = require("testdouble");
require("testdouble-jest")(td, jest);
const { gql } = require("apollo-server");
const { executeGraphql } = require("federation-testing-tool");
// This is the added line! Make sure it's above the line that requires resolvers.
const { estimateShipping } = td.replace("./estimateShipping");
//
const { typeDefs } = require("./typeDefs");
// Put these two lines at the top of inventory.test.js
const td = require("testdouble");
require("testdouble-jest")(td, jest);
exports.estimateShipping = ({price, weight}) => {
};
it("should be calculated as 1 dollar per 2 pounds for an item with a price of 1000 or below", async () => {
const mocks = { Product: () => ({ price: 999, weight: 100 }) };
const result = await executeGraphql({ query, service, mocks });
expect(result.data._getProduct.shippingEstimate).toEqual(50);
});
it("should be 0 for an item with a price over 1000", async () => {
const mocks = { Product: () => ({ price: 1001 }) };
const result = await executeGraphql({ query, service, mocks });
expect(result.data._getProduct.shippingEstimate).toStrictEqual(0);
});
const { executeGraphql } = require("federation-testing-tool");
// ...
// Modify your first it.todo function callback, and remove .todo
it("should be 0 for an item with a price over 1000", async () => {
const result = await executeGraphql({ query, service });
expect(result.data._getProduct.shippingEstimate).toEqual(0)
});
const { gql } = require("apollo-server");
const { typeDefs } = require("./typeDefs");
const { resolvers } = require("./resolvers");
const service = { typeDefs, resolvers }
// describe("Based on the data passed down from the gateway, the shippingEstimate", () => {
// ..
const { gql } = require("apollo-server");
describe("Based on the data passed down from the gateway, the shippingEstimate", () => {
const query = gql`
{
_getProduct {
shippingEstimate
}
}
`;