Benoît Hubert 8 years ago
parent
commit
a6066b9978

+ 4 - 0
js/editor.js

@@ -36,6 +36,10 @@ $(document).ready(function() {
     $('#nav-menus').toggleClass('in');
   });
 
+  if(_webSandboxFiles.length === 0) {
+    return;
+  }
+
 
   // /**
   //  * Make the left panel resizable

+ 2 - 1
lib/indexHandlers.js

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

+ 1 - 0
package.json

@@ -13,6 +13,7 @@
   "devDependencies": {
     "chai": "^4.1.2",
     "cheerio": "^1.0.0-rc.2",
+    "html-minifier": "^3.5.6",
     "mocha": "^4.0.1",
     "node-mocks-http": "^1.6.6"
   }

+ 82 - 8
test/integration/render-index.test.js

@@ -2,6 +2,7 @@ const chai          = require('chai');
 const should        = chai.should();
 const httpMocks     = require('node-mocks-http');
 const cheerio       = require('cheerio');
+const minify        = require('html-minifier').minify;
 const ExampleStore  = require('../../lib/ExampleStore');
 const exStore       = new ExampleStore(__dirname + '/test-examples');
 const indexHandlers = require('../../lib/indexHandlers')(exStore);
@@ -9,27 +10,91 @@ const indexHandlers = require('../../lib/indexHandlers')(exStore);
 function invokeHandler(url, handler, options) {
   options = options || {};
   method = options.method || 'GET';
+  params = options.params || {};
 
   // Prepare request&response
-  var request  = httpMocks.createRequest({
-    method: 'GET',
-    url: '/'
+  const request  = httpMocks.createRequest({
+    method, url, params
   });
-  var response = httpMocks.createResponse();
+  const 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);
+  const body = response._getData();
+  const statusCode = response._getStatusCode();
+  const $ = cheerio.load(body);
+  return { body, statusCode, $ };
 }
 
+
+
 describe('render index', () => {
 
+
+  /**
+   * Run before all tests
+   */
   before(() => exStore.init());
 
-  it('without selected repo or example', () => {
 
-    const $ = invokeHandler('/', indexHandlers.getIndexBare);
+  /**
+   * Index without repo nor example
+   */
+  it('without selected repo nor example', () => {
+
+    const { statusCode, $ } = invokeHandler('/', indexHandlers.getIndexBare);
+    statusCode.should.equal(200);
+
+    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>');
+
+  });
+
+
+  /**
+   * Index with non-existent repo
+   */
+  it('without inexistent selected repo', () => {
+
+    const { body, statusCode } = invokeHandler(
+      '/wrong',
+      indexHandlers.getIndexRepo,
+      {
+        params: { repoSlug: 'wrong' }
+      }
+    );
+    statusCode.should.equal(404);
+    body.should.equal('Repo wrong not found');
+
+  });
+
+
+  /**
+   * Index with existent repo
+   */
+  it('with selected repo', () => {
+
+    const { $, statusCode } = invokeHandler(
+      '/example-repo1',
+      indexHandlers.getIndexRepo,
+      {
+        params: { repoSlug: 'example-repo1' }
+      }
+    );
+    statusCode.should.equal(200);
+
     const inlineJsData = "let window = {};" +
       $('#inline-js-data').html();
     try {
@@ -45,6 +110,15 @@ describe('render index', () => {
     repo1Link.should.equal('<a href="/example-repo1">Example Repo 1</a>');
     repo2Link.should.equal('<a href="/example-repo2">Example Repo 2</a>');
 
+    const $menuExampleItems = $('#menu-example div');
+    var minifiedMenu = minify($('#menu-example').html(), {
+      collapseWhitespace: true
+    });
+    $menuExampleItems.length.should.equal(2);
+    minifiedMenu.should.equal(
+      '<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>' +
+      '<div class="pure-u-1 pure-u-md-1-2">Empty Category<ul></ul></div>'
+      );
   });
 
 });

+ 4 - 0
test/integration/test-examples/example-repo1/repo-config.json

@@ -4,6 +4,10 @@
     {
       "slug": "test-stuff",
       "title": "Test Stuff"
+    },
+    {
+      "slug": "empty-category",
+      "title": "Empty Category"
     }
   ]
 }

+ 1 - 1
test/integration/test-examples/example-repo1/repo1-example1/config.json

@@ -1,6 +1,6 @@
 {
   "title": "Test Example",
-  "category": "test-example",
+  "category": "test-stuff",
   "html": [ "example.html" ],
   "js": [ "script.js" ],
   "css": [],

+ 1 - 1
test/integration/test-examples/example-repo2/repo2-example1/config.json

@@ -1,6 +1,6 @@
 {
   "title": "Random Example",
-  "category": "random-example",
+  "category": "random-stuff",
   "html": [ "example.html" ],
   "js": [ "script.js" ],
   "css": [],