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, $ } = invokeHandler('/', indexHandlers.getIndexBare);
  35. statusCode.should.equal(200);
  36. const inlineJsData = "let window = {};" +
  37. $('#inline-js-data').html();
  38. try {
  39. eval(inlineJsData);
  40. } catch(e) {
  41. throw new Error('injected script in #inline-js-data throws an error: "' + e.message + '');
  42. }
  43. const $menuRepoItems = $('#menu-repo li');
  44. $menuRepoItems.length.should.equal(2);
  45. const repo1Link = $($menuRepoItems[0]).html();
  46. const repo2Link = $($menuRepoItems[1]).html();
  47. repo1Link.should.equal('<a href="/example-repo1">Example Repo 1</a>');
  48. repo2Link.should.equal('<a href="/example-repo2">Example Repo 2</a>');
  49. });
  50. /**
  51. * Index with non-existent repo
  52. */
  53. it('without inexistent selected repo', () => {
  54. const { body, statusCode } = invokeHandler(
  55. '/wrong',
  56. indexHandlers.getIndexRepo,
  57. {
  58. params: { repoSlug: 'wrong' }
  59. }
  60. );
  61. statusCode.should.equal(404);
  62. body.should.equal('Repo wrong not found');
  63. });
  64. /**
  65. * Index with existent repo
  66. */
  67. it('with selected repo', () => {
  68. const { $, statusCode } = invokeHandler(
  69. '/example-repo1',
  70. indexHandlers.getIndexRepo,
  71. {
  72. params: { repoSlug: 'example-repo1' }
  73. }
  74. );
  75. statusCode.should.equal(200);
  76. const inlineJsData = "let window = {};" +
  77. $('#inline-js-data').html();
  78. try {
  79. eval(inlineJsData);
  80. } catch(e) {
  81. throw new Error('injected script in #inline-js-data throws an error: "' + e.message + '');
  82. }
  83. const $menuRepoItems = $('#menu-repo li');
  84. $menuRepoItems.length.should.equal(2);
  85. const repo1Link = $($menuRepoItems[0]).html();
  86. const repo2Link = $($menuRepoItems[1]).html();
  87. repo1Link.should.equal('<a href="/example-repo1">Example Repo 1</a>');
  88. repo2Link.should.equal('<a href="/example-repo2">Example Repo 2</a>');
  89. const $menuExampleItems = $('#menu-example div');
  90. var minifiedMenu = minify($('#menu-example').html(), {
  91. collapseWhitespace: true
  92. });
  93. $menuExampleItems.length.should.equal(2);
  94. minifiedMenu.should.equal(
  95. '<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>' +
  96. '<div class="pure-u-1 pure-u-md-1-2">Empty Category<ul></ul></div>'
  97. );
  98. });
  99. /**
  100. * Index with existent repo and example
  101. */
  102. it('with selected repo and example', () => {
  103. const { $, body, statusCode } = invokeHandler(
  104. '/example-repo1/repo1-example1',
  105. indexHandlers.getIndexExample,
  106. {
  107. params: {
  108. repoSlug: 'example-repo1',
  109. exampleSlug: 'repo1-example1'
  110. }
  111. }
  112. );
  113. statusCode.should.equal(200);
  114. console.log('body', body);
  115. const inlineJsData = "let window = {};" +
  116. $('#inline-js-data').html();
  117. try {
  118. eval(inlineJsData);
  119. } catch(e) {
  120. throw new Error('injected script in #inline-js-data throws an error: "' + e.message + '');
  121. }
  122. console.log('inlineJsData', inlineJsData);
  123. // const $menuRepoItems = $('#menu-repo li');
  124. // $menuRepoItems.length.should.equal(2);
  125. // const repo1Link = $($menuRepoItems[0]).html();
  126. // const repo2Link = $($menuRepoItems[1]).html();
  127. // repo1Link.should.equal('<a href="/example-repo1">Example Repo 1</a>');
  128. // repo2Link.should.equal('<a href="/example-repo2">Example Repo 2</a>');
  129. // const $menuExampleItems = $('#menu-example div');
  130. // var minifiedMenu = minify($('#menu-example').html(), {
  131. // collapseWhitespace: true
  132. // });
  133. // $menuExampleItems.length.should.equal(2);
  134. // minifiedMenu.should.equal(
  135. // '<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>' +
  136. // '<div class="pure-u-1 pure-u-md-1-2">Empty Category<ul></ul></div>'
  137. // );
  138. });
  139. });