You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
5.9 KiB

4 years ago
  1. const { JSDOM } = require("jsdom");
  2. const sampleFeed = require("fs")
  3. .readFileSync(__dirname + "/fixtures/contentful.rss.xml")
  4. .toString();
  5. const sampleFeedParsed = require("./fixtures/contentful.rss.json");
  6. const { expect } = require("chai");
  7. const fetch = require("node-fetch");
  8. const moment = (global.moment = require("moment"));
  9. const { stub } = require("sinon");
  10. const { version } = require("../package.json");
  11. describe("jquery.rss", () => {
  12. let $, element, originalAjax;
  13. const feedUrl = "https://www.contentful.com/blog/feed.xml";
  14. const fakeGetJson = content => {
  15. originalAjax = $.ajax;
  16. $.ajax = function({ url, success }) {
  17. success({
  18. responseData: {
  19. feed: {
  20. entries: [
  21. {
  22. content: content,
  23. contentSnippet: content
  24. }
  25. ]
  26. }
  27. }
  28. });
  29. };
  30. };
  31. before(() => {
  32. const { window, document } = new JSDOM(`<!DOCTYPE html>`);
  33. global.window = window;
  34. global.document = window.document;
  35. $ = global.jQuery = require("jquery");
  36. require("../dist/jquery.rss.min");
  37. });
  38. beforeEach(() => {
  39. element = $("<div>").appendTo($("body"));
  40. });
  41. afterEach(() => {
  42. if (typeof originalAjax === "function") {
  43. $.ajax = originalAjax;
  44. originalAjax = null;
  45. }
  46. });
  47. it("supports multiple rss feeds", done => {
  48. originalAjax = $.ajax;
  49. $.ajax = function({ url, success }) {
  50. expect(url).to.include(
  51. "q=https%3A%2F%2Fwww.contentful.com%2Fblog%2Ffeed.xml,http%3A%2F%2Fwww.ebaytechblog.com%2Ffeed%2F"
  52. );
  53. done();
  54. };
  55. var $container = element;
  56. $container.rss([
  57. "https://www.contentful.com/blog/feed.xml",
  58. "http://www.ebaytechblog.com/feed/"
  59. ]);
  60. });
  61. it("renders an unordered list by default", function(done) {
  62. var $container = element;
  63. $container.rss(feedUrl, {}, function() {
  64. var renderedContent = $container.html().replace(/\n/g, "");
  65. expect(renderedContent).to.match(/<ul>.*<\/ul>/);
  66. done();
  67. });
  68. });
  69. it("renders 2 list entries if limit is set to 2", function(done) {
  70. var $container = element;
  71. $container.rss(
  72. feedUrl,
  73. {
  74. limit: 2
  75. },
  76. function() {
  77. expect($("li", $container).length).to.equal(2);
  78. done();
  79. }
  80. );
  81. });
  82. it("renders the defined entry template", function(done) {
  83. var $container = element;
  84. $container.rss(
  85. feedUrl,
  86. {
  87. limit: 1,
  88. entryTemplate: "<li>foo</li>"
  89. },
  90. function() {
  91. var renderedContent = $container
  92. .html()
  93. .split("\n")
  94. .map(function(s) {
  95. return s.trim();
  96. })
  97. .join("")
  98. .trim();
  99. expect(renderedContent).to.match(/<ul><li>foo<\/li><\/ul>/);
  100. done();
  101. }
  102. );
  103. });
  104. it("renders the defined layout template", function(done) {
  105. var $container = element;
  106. $container.rss(
  107. feedUrl,
  108. {
  109. limit: 1,
  110. layoutTemplate: "foo<ul>{entries}</ul>bar"
  111. },
  112. function() {
  113. var renderedContent = $container.html().replace(/\n/g, "");
  114. expect(renderedContent).to.match(/foo<ul>.*<\/ul>/);
  115. done();
  116. }
  117. );
  118. });
  119. it("supports custom tokens", function(done) {
  120. var $container = element;
  121. $container.rss(
  122. feedUrl,
  123. {
  124. limit: 1,
  125. entryTemplate: "<li>{myCustomStaticToken} {myCustomDynamicToken}</li>",
  126. tokens: {
  127. myCustomStaticToken: "static",
  128. myCustomDynamicToken: function() {
  129. return "dynamic";
  130. }
  131. }
  132. },
  133. function() {
  134. var renderedContent = $container
  135. .html()
  136. .split("\n")
  137. .map(function(s) {
  138. return s.trim();
  139. })
  140. .join("")
  141. .trim();
  142. expect(renderedContent).to.match(
  143. new RegExp("<ul><li>static dynamic</li></ul>")
  144. );
  145. done();
  146. }
  147. );
  148. });
  149. it("removes p-tags but not the content", function(done) {
  150. var $container = element;
  151. fakeGetJson("<p>May the fourth be with you!</p>");
  152. $container.rss(
  153. feedUrl,
  154. {
  155. limit: 1,
  156. entryTemplate: "<li>{bodyPlain}</li>"
  157. },
  158. function() {
  159. var renderedContent = $container
  160. .html()
  161. .split("\n")
  162. .map(function(s) {
  163. return s.trim();
  164. })
  165. .join("")
  166. .trim();
  167. expect(renderedContent).to.match(/<ul><li>.*<\/li><\/ul>/);
  168. done();
  169. }
  170. );
  171. });
  172. it("calls the error callback if something went wrong", function(done) {
  173. element.rss("https://google.com", {
  174. error: function() {
  175. expect(1).to.equal(1);
  176. done();
  177. }
  178. });
  179. });
  180. it("calls the success callback", function(done) {
  181. element.rss(feedUrl, {
  182. limit: 1,
  183. success: function() {
  184. expect(1).to.equal(1);
  185. done();
  186. }
  187. });
  188. });
  189. it("renders the defined entry template in the layout template", function(done) {
  190. var $container = element;
  191. $container.rss(
  192. feedUrl,
  193. {
  194. limit: 1,
  195. entryTemplate: "<li>bazinga</li>",
  196. layoutTemplate: "<ul><li>topic</li>{entries}</ul>"
  197. },
  198. function() {
  199. var renderedContent = $container.html().replace(/\n/g, "");
  200. expect(renderedContent).to.equal(
  201. "<ul><li>topic</li><li>bazinga</li></ul>"
  202. );
  203. done();
  204. }
  205. );
  206. });
  207. it("renders when layout template only contains the entries token", function(done) {
  208. var $container = $("<table>").appendTo(element);
  209. $container.rss(
  210. feedUrl,
  211. {
  212. limit: 1,
  213. layoutTemplate: "{entries}",
  214. entryTemplate: "<tr><td>{title}</td></tr>"
  215. },
  216. function() {
  217. var renderedContent = $container[0].outerHTML.replace(/\n/g, "");
  218. expect(renderedContent).to.match(
  219. /<table><tbody><tr><td>.*<\/td><\/tr><\/tbody><\/table>/
  220. );
  221. done();
  222. }
  223. );
  224. });
  225. });