lenses.sample 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { lenses, getSuitableLenses, getLensRenderers } from "../lenses"
  2. import rawLens from "../rawLens"
  3. describe("getSuitableLenses", () => {
  4. test("returns raw lens if no content type reported (null/undefined)", () => {
  5. const nullResult = getSuitableLenses({
  6. headers: {
  7. "content-type": null,
  8. },
  9. })
  10. const undefinedResult = getSuitableLenses({
  11. headers: {},
  12. })
  13. expect(nullResult).toHaveLength(1)
  14. expect(nullResult).toContainEqual(rawLens)
  15. expect(undefinedResult).toHaveLength(1)
  16. expect(undefinedResult).toContainEqual(rawLens)
  17. })
  18. const contentTypes = {
  19. JSON: [
  20. "application/json",
  21. "application/ld+json",
  22. "application/hal+json; charset=utf8",
  23. ],
  24. Image: [
  25. "image/gif",
  26. "image/jpeg; foo=bar",
  27. "image/png",
  28. "image/bmp",
  29. "image/svg+xml",
  30. "image/x-icon",
  31. "image/vnd.microsoft.icon",
  32. ],
  33. HTML: ["text/html", "application/xhtml+xml", "text/html; charset=utf-8"],
  34. XML: [
  35. "text/xml",
  36. "application/xml",
  37. "application/xhtml+xml; charset=utf-8",
  38. ],
  39. }
  40. lenses
  41. .filter(({ lensName }) => lensName !== rawLens.lensName)
  42. .forEach((el) => {
  43. test(`returns ${el.lensName} lens for its content-types`, () => {
  44. contentTypes[el.lensName].forEach((contentType) => {
  45. expect(
  46. getSuitableLenses({
  47. headers: {
  48. "content-type": contentType,
  49. },
  50. })
  51. ).toContainEqual(el)
  52. })
  53. })
  54. test(`returns Raw Lens along with ${el.lensName} for the content types`, () => {
  55. contentTypes[el.lensName].forEach((contentType) => {
  56. expect(
  57. getSuitableLenses({
  58. headers: {
  59. "content-type": contentType,
  60. },
  61. })
  62. ).toContainEqual(rawLens)
  63. })
  64. })
  65. })
  66. })
  67. describe("getLensRenderers", () => {
  68. test("returns all the lens renderers", () => {
  69. const res = getLensRenderers()
  70. lenses.forEach(({ renderer, rendererImport }) => {
  71. expect(res).toHaveProperty(renderer)
  72. expect(res[renderer]).toBe(rendererImport)
  73. })
  74. })
  75. })