const chai = require('chai'); const should = chai.should(); const httpMocks = require('node-mocks-http'); const cheerio = require('cheerio'); const ExampleStore = require('../../lib/ExampleStore'); const exStore = new ExampleStore(__dirname + '/test-examples'); const indexHandlers = require('../../lib/indexHandlers')(exStore); function invokeHandler(url, handler, options) { options = options || {}; method = options.method || 'GET'; // Prepare request&response var request = httpMocks.createRequest({ method: 'GET', url: '/' }); var response = httpMocks.createResponse(); // Invoke route handler, get returned html and load it with cheerio handler(request, response); var html = response._getData(); return cheerio.load(html); } describe('render index', () => { before(() => exStore.init()); it('without selected repo or example', () => { const $ = invokeHandler('/', indexHandlers.getIndexBare); const inlineJsData = "let window = {};" + $('#inline-js-data').html(); try { eval(inlineJsData); } catch(e) { throw new Error('injected script in #inline-js-data throws an error: "' + e.message + ''); } const $menuRepoItems = $('#menu-repo li'); $menuRepoItems.length.should.equal(2); const repo1Link = $($menuRepoItems[0]).html(); const repo2Link = $($menuRepoItems[1]).html(); repo1Link.should.equal('Example Repo 1'); repo2Link.should.equal('Example Repo 2'); }); });