Be creative with Midway mock

Jackie Fong
Jan 24 · 5 min read
Image credit: Pixabay


module.exports = {
register: function (mock) {
mock.route({
id: "getItem",
label: "getItem",
path: "/item/{id}",
method: "GET",
handler: function (req, reply) {
mock.util.respondWithFile(this, reply);
}
})
.variant({
id: "giftCard",
label: "giftCard",
handler: function(req, reply) {
mock.util.respondWithFile(this, reply);
}
})
.variant({
id: "eGiftCard",
label: "eGiftCard",
handler: function(req, reply) {
mock.util.respondWithFile(this, reply);
}
})
}
};
module.exports = {
register: function (mock) {
mock.route({
id: "getShipping",
label: "getShipping",
path: "/fulfillment/{id}/shipping",
method: "GET",
handler: function (req, reply) {
mock.util.respondWithFile(this, reply);
}
})
.variant({
id: "freightShipping",
label: "freightShipping",
handler: function(req, reply) {
mock.util.respondWithFile(this, reply);
}
})
.variant({
id: "standardShipping",
label: "standardShipping",
handler: function(req, reply) {
mock.util.respondWithFile(this, reply);
}
})
}
};
module.exports = {
register: function (mock) {
mock.route({
id: "getPickup",
label: "getPickup",
path: "/fulfillment/{id}/pickup",
method: "GET",
handler: function (req, reply) {
mock.util.respondWithFile(this, reply);
}
})
.variant({
id: "pickupOnly",
label: "pickupOnly",
handler: function(req, reply) {
mock.util.respondWithFile(this, reply);
}
})
}
};

// 1. Get item
const _getItemVariant = (mock, variantId) => {
return {
id: `${variantId}`,
label: `${variantId}`,
method: "POST",
handler(req, reply) {
var mockedPath = "test/automation/mocked-data/fetch/item/POST/";
mock.util.respondWithFile(this, reply, { filePath: mockedPath + variantId + ".json" });
}
};
};
// 2. Get shipping info
const _getShippingVariant = (mock, variantId) => {
return {
id: `${variantId}`,
label: `${variantId}`,
method: "POST",
handler(req, reply) {
var mockedPath = "test/automation/mocked-data/fetch/shipping/POST/";
mock.util.respondWithFile(this, reply, { filePath: mockedPath + variantId + ".json" });
}
};
};

// 3. Get pickup info
const _getPickupVariant = (mock, variantId) => {
return {
id: `${variantId}`,
label: `${variantId}`,
method: "POST",
handler(req, reply) {
var mockedPath = "test/automation/mocked-data/fetch/pickup/POST/";
mock.util.respondWithFile(this, reply, { filePath: mockedPath + variantId + ".json" });
}
};
};
module.exports = {
register(mock) {
mock.route({
id: "getItem",
label: "getItem",
path: "/fetch",
method: "POST",
handler(req, reply) {
var mockedPath = "test/automation/mocked-data/fetch/item/POST/";
mock.util.respondWithFile(this, reply, { filePath: mockedPath + "default.json" });
}
})

// Get item call
.variant(_getItemVariant(mock, “giftCard”)) .variant(_getItemVariant(mock, “eGiftCard”))
// Get shipping info call .variant(_getShippingVariant(mock, “shipOneDay”)) .variant(_getShippingVariant(mock, “shipTwoDays”))
// Get pickup info call .variant(_getPickupVariant(mock, “pickupInPreferredStore”)) .variant(_getPickupVariant(mock, “pickupInNearbyStore”)) }
};
Image credit: Pixabay

WalmartLabs

Using technology, data and design to change the way the world shops. Learn more about us - http://walmartlabs.com/

Jackie Fong

Written by

Test automation, software quality

WalmartLabs

Using technology, data and design to change the way the world shops. Learn more about us - http://walmartlabs.com/