Procházet zdrojové kódy

First test on index

Benoît Hubert před 8 roky
rodič
revize
3f1c6a17c1

+ 1 - 2
html/index.mustache.html

@@ -74,9 +74,8 @@
 <script src="/js/vendor/ace/ace.js" type="text/javascript" charset="utf-8"></script>
 <script src="/js/req-promise.js"></script>
 <script src="/js/editor-local-storage.js"></script>
-<script>
+<script id="inline-js-data">
 window._webSandboxFiles = {{{filesJSON}}};
-console.log(window._webSandboxFiles);
 </script>
 <script src="/js/editor.js"></script>
 </body>

+ 2 - 1
lib/indexHandlers.js

@@ -33,7 +33,8 @@ module.exports = function(exStore) {
       console.log(menuRepo);
       res.send(Mustache.render(indexTpml, {
         // title,
-        menuRepo
+        menuRepo,
+        filesJSON: '[]'
       }));
     },
 

+ 3 - 1
package.json

@@ -12,6 +12,8 @@
   },
   "devDependencies": {
     "chai": "^4.1.2",
-    "mocha": "^4.0.1"
+    "cheerio": "^1.0.0-rc.2",
+    "mocha": "^4.0.1",
+    "node-mocks-http": "^1.6.6"
   }
 }

+ 36 - 0
test/integration/render-index.test.js

@@ -1,7 +1,27 @@
 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', () => {
 
@@ -9,6 +29,22 @@ describe('render index', () => {
 
   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('<a href="/example-repo1">Example Repo 1</a>');
+    repo2Link.should.equal('<a href="/example-repo2">Example Repo 2</a>');
+
   });
 
 });