resolve-url.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2014 Simon Lydell
  2. // X11 (“MIT”) Licensed. (See LICENSE.)
  3. var test = require("tape")
  4. var resolveUrl = require("../")
  5. "use strict"
  6. test("resolveUrl", function(t) {
  7. t.plan(7)
  8. t.equal(typeof resolveUrl, "function", "is a function")
  9. t.equal(
  10. resolveUrl("https://example.com/"),
  11. "https://example.com/"
  12. )
  13. var loc = "https://example.com/articles/resolving-urls/edit"
  14. t.equal(
  15. resolveUrl(loc, "remove"),
  16. "https://example.com/articles/resolving-urls/remove"
  17. )
  18. t.equal(
  19. resolveUrl(loc, "/static/scripts/app.js"),
  20. "https://example.com/static/scripts/app.js"
  21. )
  22. t.equal(
  23. resolveUrl(loc, "/static/scripts/app.js", "../source-maps/app.js.map"),
  24. "https://example.com/static/source-maps/app.js.map"
  25. )
  26. t.equal(
  27. resolveUrl(loc, "/static/scripts/app.js", "../source-maps/app.js.map", "../coffee/app.coffee"),
  28. "https://example.com/static/coffee/app.coffee"
  29. )
  30. t.equal(
  31. resolveUrl(loc, "//cdn.example.com/jquery.js"),
  32. "https://cdn.example.com/jquery.js"
  33. )
  34. })
  35. test("edge cases", function(t) {
  36. t.plan(4)
  37. t["throws"](resolveUrl, /at least one argument/, "throws with no arguments")
  38. var accidentallyUndefined
  39. var result
  40. t.doesNotThrow(
  41. function() { result = resolveUrl(accidentallyUndefined) },
  42. "undefined is still an argument"
  43. )
  44. t.ok(result.match(/\/undefined$/), "undefined is stringified")
  45. t.equal(
  46. resolveUrl("http://foo.org/test", undefined, {}, ["a/b"], null),
  47. "http://foo.org/a/null",
  48. "arguments are stringified"
  49. )
  50. })