render-index.test.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. const chai = require('chai');
  2. const should = chai.should();
  3. const httpMocks = require('node-mocks-http');
  4. const cheerio = require('cheerio');
  5. const ExampleStore = require('../../lib/ExampleStore');
  6. const exStore = new ExampleStore(__dirname + '/test-examples');
  7. const indexHandlers = require('../../lib/indexHandlers')(exStore);
  8. function invokeHandler(url, handler, options) {
  9. options = options || {};
  10. method = options.method || 'GET';
  11. // Prepare request&response
  12. var request = httpMocks.createRequest({
  13. method: 'GET',
  14. url: '/'
  15. });
  16. var response = httpMocks.createResponse();
  17. // Invoke route handler, get returned html and load it with cheerio
  18. handler(request, response);
  19. var html = response._getData();
  20. return cheerio.load(html);
  21. }
  22. describe('render index', () => {
  23. before(() => exStore.init());
  24. it('without selected repo or example', () => {
  25. const $ = invokeHandler('/', indexHandlers.getIndexBare);
  26. const inlineJsData = "let window = {};" +
  27. $('#inline-js-data').html();
  28. try {
  29. eval(inlineJsData);
  30. } catch(e) {
  31. throw new Error('injected script in #inline-js-data throws an error: "' + e.message + '');
  32. }
  33. const $menuRepoItems = $('#menu-repo li');
  34. $menuRepoItems.length.should.equal(2);
  35. const repo1Link = $($menuRepoItems[0]).html();
  36. const repo2Link = $($menuRepoItems[1]).html();
  37. repo1Link.should.equal('<a href="/example-repo1">Example Repo 1</a>');
  38. repo2Link.should.equal('<a href="/example-repo2">Example Repo 2</a>');
  39. });
  40. });