render-index.test.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. const chai = require('chai');
  2. const should = chai.should();
  3. const httpMocks = require('node-mocks-http');
  4. const cheerio = require('cheerio');
  5. const minify = require('html-minifier').minify;
  6. const ExampleStore = require('../../lib/ExampleStore');
  7. const exStore = new ExampleStore(__dirname + '/test-examples');
  8. const indexHandlers = require('../../lib/indexHandlers')(exStore, __dirname + '/test-examples');
  9. function invokeHandler(url, handler, options) {
  10. options = options || {};
  11. method = options.method || 'GET';
  12. params = options.params || {};
  13. // Prepare request&response
  14. const request = httpMocks.createRequest({
  15. method, url, params
  16. });
  17. const response = httpMocks.createResponse();
  18. // Invoke route handler, get returned html and load it with cheerio
  19. handler(request, response);
  20. const body = response._getData();
  21. const statusCode = response._getStatusCode();
  22. const $ = cheerio.load(body);
  23. return { body, statusCode, $ };
  24. }
  25. describe('render index', () => {
  26. /**
  27. * Run before all tests
  28. */
  29. before(() => exStore.init());
  30. /**
  31. * Index without repo nor example
  32. */
  33. it('without selected repo nor example', () => {
  34. const { statusCode, body, $ } = invokeHandler('/', indexHandlers.getIndexBare);
  35. statusCode.should.equal(200);
  36. console.log(body);
  37. const inlineJsData = "let window = {};" +
  38. $('#inline-js-data').html();
  39. try {
  40. eval(inlineJsData);
  41. } catch(e) {
  42. throw new Error('injected script in #inline-js-data throws an error: "' + e.message + '');
  43. }
  44. const $menuRepoItems = $('#menu-repo li');
  45. $menuRepoItems.length.should.equal(2);
  46. const repo1Link = $($menuRepoItems[0]).html();
  47. const repo2Link = $($menuRepoItems[1]).html();
  48. repo1Link.should.equal('<a href="/example-repo1">Example Repo 1</a>');
  49. repo2Link.should.equal('<a href="/example-repo2">Example Repo 2</a>');
  50. });
  51. /**
  52. * Index with non-existent repo
  53. */
  54. it('without inexistent selected repo', () => {
  55. const { body, statusCode } = invokeHandler(
  56. '/wrong',
  57. indexHandlers.getIndexRepo,
  58. {
  59. params: { repoSlug: 'wrong' }
  60. }
  61. );
  62. statusCode.should.equal(404);
  63. body.should.equal('Repo wrong not found');
  64. });
  65. /**
  66. * Index with existent repo
  67. */
  68. it('with selected repo', () => {
  69. const { $, statusCode } = invokeHandler(
  70. '/example-repo1',
  71. indexHandlers.getIndexRepo,
  72. {
  73. params: { repoSlug: 'example-repo1' }
  74. }
  75. );
  76. statusCode.should.equal(200);
  77. const inlineJsData = "let window = {};" +
  78. $('#inline-js-data').html();
  79. try {
  80. eval(inlineJsData);
  81. } catch(e) {
  82. throw new Error('injected script in #inline-js-data throws an error: "' + e.message + '');
  83. }
  84. const $menuRepoItems = $('#menu-repo li');
  85. $menuRepoItems.length.should.equal(2);
  86. const repo1Link = $($menuRepoItems[0]).html();
  87. const repo2Link = $($menuRepoItems[1]).html();
  88. repo1Link.should.equal('<a href="/example-repo1">Example Repo 1</a>');
  89. repo2Link.should.equal('<a href="/example-repo2">Example Repo 2</a>');
  90. const $menuExampleItems = $('#menu-example div');
  91. var minifiedMenu = minify($('#menu-example').html(), {
  92. collapseWhitespace: true
  93. });
  94. $menuExampleItems.length.should.equal(2);
  95. minifiedMenu.should.equal(
  96. '<div class="pure-u-1 pure-u-md-1-2">Test Stuff<ul><li><a href="/example-repo1/repo1-example1">Test Example</a></li></ul></div>' +
  97. '<div class="pure-u-1 pure-u-md-1-2">Empty Category<ul></ul></div>'
  98. );
  99. });
  100. /**
  101. * Index with existent repo and example
  102. */
  103. it('with selected repo and example', () => {
  104. const { $, body, statusCode } = invokeHandler(
  105. '/example-repo1/repo1-example1',
  106. indexHandlers.getIndexExample,
  107. {
  108. params: {
  109. repoSlug: 'example-repo1',
  110. exampleSlug: 'repo1-example1'
  111. }
  112. }
  113. );
  114. statusCode.should.equal(200);
  115. console.log('body', body);
  116. const inlineJsData = "let window = {};" +
  117. $('#inline-js-data').html();
  118. try {
  119. eval(inlineJsData);
  120. } catch(e) {
  121. throw new Error('injected script in #inline-js-data throws an error: "' + e.message + '');
  122. }
  123. console.log('inlineJsData', inlineJsData);
  124. // const $menuRepoItems = $('#menu-repo li');
  125. // $menuRepoItems.length.should.equal(2);
  126. // const repo1Link = $($menuRepoItems[0]).html();
  127. // const repo2Link = $($menuRepoItems[1]).html();
  128. // repo1Link.should.equal('<a href="/example-repo1">Example Repo 1</a>');
  129. // repo2Link.should.equal('<a href="/example-repo2">Example Repo 2</a>');
  130. // const $menuExampleItems = $('#menu-example div');
  131. // var minifiedMenu = minify($('#menu-example').html(), {
  132. // collapseWhitespace: true
  133. // });
  134. // $menuExampleItems.length.should.equal(2);
  135. // minifiedMenu.should.equal(
  136. // '<div class="pure-u-1 pure-u-md-1-2">Test Stuff<ul><li><a href="/example-repo1/repo1-example1">Test Example</a></li></ul></div>' +
  137. // '<div class="pure-u-1 pure-u-md-1-2">Empty Category<ul></ul></div>'
  138. // );
  139. });
  140. });