/test/scripts/helpers/open_graph.js

https://gitlab.com/nycancccat/hexo · JavaScript · 368 lines · 297 code · 71 blank · 0 comment · 0 complexity · 10c36ec088096392da54d0e5687209c6 MD5 · raw file

  1. 'use strict';
  2. var should = require('chai').should(); // eslint-disable-line
  3. describe('open_graph', function() {
  4. var Hexo = require('../../../lib/hexo');
  5. var hexo = new Hexo();
  6. var openGraph = require('../../../lib/plugins/helper/open_graph');
  7. var isPost = require('../../../lib/plugins/helper/is').post;
  8. var tag = require('hexo-util').htmlTag;
  9. function meta(options) {
  10. return tag('meta', options);
  11. }
  12. it('default', function() {
  13. var result = openGraph.call({
  14. page: {},
  15. config: hexo.config,
  16. is_post: isPost
  17. });
  18. result.should.eql([
  19. meta({name: 'description'}),
  20. meta({property: 'og:type', content: 'website'}),
  21. meta({property: 'og:title', content: hexo.config.title}),
  22. meta({property: 'og:url'}),
  23. meta({property: 'og:site_name', content: hexo.config.title}),
  24. meta({property: 'og:description'}),
  25. meta({name: 'twitter:card', content: 'summary'}),
  26. meta({name: 'twitter:title', content: hexo.config.title}),
  27. meta({name: 'twitter:description'})
  28. ].join('\n'));
  29. });
  30. it('title - page', function() {
  31. var ctx = {
  32. page: {title: 'Hello world'},
  33. config: hexo.config,
  34. is_post: isPost
  35. };
  36. var result = openGraph.call(ctx);
  37. result.should.contain(meta({property: 'og:title', content: ctx.page.title}));
  38. });
  39. it('title - options', function() {
  40. var result = openGraph.call({
  41. page: {title: 'Hello world'},
  42. config: hexo.config,
  43. is_post: isPost
  44. }, {title: 'test'});
  45. result.should.contain(meta({property: 'og:title', content: 'test'}));
  46. });
  47. it('type - options', function() {
  48. var result = openGraph.call({
  49. page: {},
  50. config: hexo.config,
  51. is_post: isPost
  52. }, {type: 'photo'});
  53. result.should.contain(meta({property: 'og:type', content: 'photo'}));
  54. });
  55. it('type - is_post', function() {
  56. var result = openGraph.call({
  57. page: {},
  58. config: hexo.config,
  59. is_post: function() {
  60. return true;
  61. }
  62. });
  63. result.should.contain(meta({property: 'og:type', content: 'article'}));
  64. });
  65. it('url - context', function() {
  66. var ctx = {
  67. page: {},
  68. config: hexo.config,
  69. is_post: isPost,
  70. url: 'http://hexo.io/foo'
  71. };
  72. var result = openGraph.call(ctx);
  73. result.should.contain(meta({property: 'og:url', content: ctx.url}));
  74. });
  75. it('url - options', function() {
  76. var result = openGraph.call({
  77. page: {},
  78. config: hexo.config,
  79. is_post: isPost,
  80. url: 'http://hexo.io/foo'
  81. }, {url: 'http://hexo.io/bar'});
  82. result.should.contain(meta({property: 'og:url', content: 'http://hexo.io/bar'}));
  83. });
  84. it('images - content', function() {
  85. var result = openGraph.call({
  86. page: {
  87. content: [
  88. '<p>123456789</p>',
  89. '<img src="http://hexo.io/test.jpg">'
  90. ].join('')
  91. },
  92. config: hexo.config,
  93. is_post: isPost
  94. });
  95. result.should.contain(meta({property: 'og:image', content: 'http://hexo.io/test.jpg'}));
  96. });
  97. it('images - string', function() {
  98. var result = openGraph.call({
  99. page: {
  100. photos: 'http://hexo.io/test.jpg'
  101. },
  102. config: hexo.config,
  103. is_post: isPost
  104. });
  105. result.should.contain(meta({property: 'og:image', content: 'http://hexo.io/test.jpg'}));
  106. });
  107. it('images - array', function() {
  108. var result = openGraph.call({
  109. page: {
  110. photos: [
  111. 'http://hexo.io/foo.jpg',
  112. 'http://hexo.io/bar.jpg'
  113. ]
  114. },
  115. config: hexo.config,
  116. is_post: isPost
  117. });
  118. result.should.contain([
  119. meta({property: 'og:image', content: 'http://hexo.io/foo.jpg'}),
  120. meta({property: 'og:image', content: 'http://hexo.io/bar.jpg'})
  121. ].join('\n'));
  122. });
  123. it('images - don\'t pollute context', function() {
  124. var ctx = {
  125. page: {
  126. content: [
  127. '<p>123456789</p>',
  128. '<img src="http://hexo.io/test.jpg">'
  129. ].join(''),
  130. photos: []
  131. },
  132. config: hexo.config,
  133. is_post: isPost
  134. };
  135. openGraph.call(ctx);
  136. ctx.page.photos.should.eql([]);
  137. });
  138. it('images - options.image', function() {
  139. var result = openGraph.call({
  140. page: {},
  141. config: hexo.config,
  142. is_post: isPost
  143. }, {image: 'http://hexo.io/test.jpg'});
  144. result.should.contain(meta({property: 'og:image', content: 'http://hexo.io/test.jpg'}));
  145. });
  146. it('images - options.images', function() {
  147. var result = openGraph.call({
  148. page: {},
  149. config: hexo.config,
  150. is_post: isPost
  151. }, {images: 'http://hexo.io/test.jpg'});
  152. result.should.contain(meta({property: 'og:image', content: 'http://hexo.io/test.jpg'}));
  153. });
  154. it('images - prepend config.url to the path (without prefixing /)', function() {
  155. var result = openGraph.call({
  156. page: {},
  157. config: hexo.config,
  158. is_post: isPost
  159. }, {images: 'test.jpg'});
  160. result.should.contain(meta({property: 'og:image', content: hexo.config.url + '/test.jpg'}));
  161. });
  162. it('images - prepend config.url to the path (with prefixing /)', function() {
  163. var result = openGraph.call({
  164. page: {},
  165. config: hexo.config,
  166. is_post: isPost
  167. }, {images: '/test.jpg'});
  168. result.should.contain(meta({property: 'og:image', content: hexo.config.url + '/test.jpg'}));
  169. });
  170. it('site_name - options', function() {
  171. var result = openGraph.call({
  172. page: {},
  173. config: hexo.config,
  174. is_post: isPost
  175. }, {site_name: 'foo'});
  176. result.should.contain(meta({property: 'og:site_name', content: 'foo'}));
  177. });
  178. it('description - page', function() {
  179. var ctx = {
  180. page: {description: 'test'},
  181. config: hexo.config,
  182. is_post: isPost
  183. };
  184. var result = openGraph.call(ctx);
  185. result.should.contain(meta({name: 'description', content: ctx.page.description}));
  186. result.should.contain(meta({property: 'og:description', content: ctx.page.description}));
  187. });
  188. it('description - options', function() {
  189. var ctx = {
  190. page: {description: 'test'},
  191. config: hexo.config,
  192. is_post: isPost
  193. };
  194. var result = openGraph.call(ctx, {description: 'foo'});
  195. result.should.contain(meta({name: 'description', content: 'foo'}));
  196. result.should.contain(meta({property: 'og:description', content: 'foo'}));
  197. });
  198. it('description - excerpt', function() {
  199. var ctx = {
  200. page: {excerpt: 'test'},
  201. config: hexo.config,
  202. is_post: isPost
  203. };
  204. var result = openGraph.call(ctx);
  205. result.should.contain(meta({name: 'description', content: ctx.page.excerpt}));
  206. result.should.contain(meta({property: 'og:description', content: ctx.page.excerpt}));
  207. });
  208. it('description - content', function() {
  209. var ctx = {
  210. page: {content: 'test'},
  211. config: hexo.config,
  212. is_post: isPost
  213. };
  214. var result = openGraph.call(ctx);
  215. result.should.contain(meta({name: 'description', content: ctx.page.content}));
  216. result.should.contain(meta({property: 'og:description', content: ctx.page.content}));
  217. });
  218. it('description - config', function() {
  219. var ctx = {
  220. page: {},
  221. config: hexo.config,
  222. is_post: isPost
  223. };
  224. hexo.config.description = 'test';
  225. var result = openGraph.call(ctx);
  226. result.should.contain(meta({name: 'description', content: hexo.config.description}));
  227. result.should.contain(meta({property: 'og:description', content: hexo.config.description}));
  228. hexo.config.description = '';
  229. });
  230. it('description - escape', function() {
  231. var ctx = {
  232. page: {description: '<b>Important!</b> Today is "not" \'Xmas\'!'},
  233. config: hexo.config,
  234. is_post: isPost
  235. };
  236. var result = openGraph.call(ctx);
  237. var escaped = 'Important! Today is &quot;not&quot; &apos;Xmas&apos;!';
  238. result.should.contain(meta({name: 'description', content: escaped}));
  239. result.should.contain(meta({property: 'og:description', content: escaped}));
  240. });
  241. it('twitter_card - options', function() {
  242. var result = openGraph.call({
  243. page: {},
  244. config: hexo.config,
  245. is_post: isPost
  246. }, {twitter_card: 'photo'});
  247. result.should.contain(meta({name: 'twitter:card', content: 'photo'}));
  248. });
  249. it('twitter_id - options (without prefixing @)', function() {
  250. var result = openGraph.call({
  251. page: {},
  252. config: hexo.config,
  253. is_post: isPost
  254. }, {twitter_id: 'hexojs'});
  255. result.should.contain(meta({name: 'twitter:creator', content: '@hexojs'}));
  256. });
  257. it('twitter_id - options (with prefixing @)', function() {
  258. var result = openGraph.call({
  259. page: {},
  260. config: hexo.config,
  261. is_post: isPost
  262. }, {twitter_id: '@hexojs'});
  263. result.should.contain(meta({name: 'twitter:creator', content: '@hexojs'}));
  264. });
  265. it('twitter_site - options', function() {
  266. var result = openGraph.call({
  267. page: {},
  268. config: hexo.config,
  269. is_post: isPost
  270. }, {twitter_site: 'Hello'});
  271. result.should.contain(meta({name: 'twitter:site', content: 'Hello'}));
  272. });
  273. it('google_plus - options', function() {
  274. var result = openGraph.call({
  275. page: {},
  276. config: hexo.config,
  277. is_post: isPost
  278. }, {google_plus: '+123456789'});
  279. result.should.contain(tag('link', {rel: 'publisher', href: '+123456789'}));
  280. });
  281. it('fb_admins - options', function() {
  282. var result = openGraph.call({
  283. page: {},
  284. config: hexo.config,
  285. is_post: isPost
  286. }, {fb_admins: '123456789'});
  287. result.should.contain(meta({property: 'fb:admins', content: '123456789'}));
  288. });
  289. it('fb_app_id - options', function() {
  290. var result = openGraph.call({
  291. page: {},
  292. config: hexo.config,
  293. is_post: isPost
  294. }, {fb_app_id: '123456789'});
  295. result.should.contain(meta({property: 'fb:app_id', content: '123456789'}));
  296. });
  297. });