TAGS :Viewed: 3 - Published at: a few seconds ago

[ Multiple expect declorations with the same expected output ]

I'm testing the function shopify.formatRequestUrl below and each time I'm expecting that the output of the function is going to be the same. Is there any way to make this more DRY?

expect(shopify.formatRequestUrl("shop"))
  .equal("https://"+settings.shop+"/admin/shop.json")

expect(shopify.formatRequestUrl("/shop"))
  .equal("https://"+settings.shop+"/admin/shop.json")

expect(shopify.formatRequestUrl("/shop/"))
  .equal("https://"+settings.shop+"/admin/shop.json")

expect(shopify.formatRequestUrl("shop.json"))
  .equal("https://"+settings.shop+"/admin/shop.json")

expect(shopify.formatRequestUrl("/admin/shop"))
  .equal("https://"+settings.shop+"/admin/shop.json")

expect(shopify.formatRequestUrl("/admin/shop.json"))
  .equal("https://"+settings.shop+"/admin/shop.json")

Something like this: psudo code

expectAll[
  shopify.formatRequestUrl("shop"),
  shopify.formatRequestUrl("/shop"),
  shopify.formatRequestUrl("/shop/"),
  shopify.formatRequestUrl("shop.json"),
  shopify.formatRequestUrl("/admin/shop"),
  shopify.formatRequestUrl("/admin/shop.json")
]
  .equal("https://"+settings.shop+"/admin/shop.json")

Does something like this exist?

Answer 1


Using underscore and looping over an object of all the arguments works.

_.each([
  "shop",
  "/shop",
  "/shop/",
  "shop/",
  "shop.json",
  "/shop.json",
  "/shop.json/",
  "shop.json/",
  "/admin/shop",
  "/admin/shop/",
  "/admin/shop.json",
  "/admin/shop.json/",
  "admin/shop.json/",
], function(result){
  expect(shopify.formatRequestUrl(result))
    .equal("https://"+settings.shop+"/admin/shop.json")
})