Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Commit

Permalink
Merge branch 'release-1.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jun 28, 2016
2 parents ca42235 + 58139b9 commit f714127
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 40 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
<a name="1.0.3"></a>
## 1.0.3 (2016-06-28)


### Features

* **menu:** write complete metadata to menu file([98e1106](https://github.com/poppinss/docketjs/commit/98e1106))



<a name="1.0.2"></a>
## 1.0.2 (2016-06-18)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "docketjs",
"version": "1.0.2",
"version": "1.0.3",
"description": "DocketJs is a battery included Markdown to HTML converter for Node.js. It has everything you need to successfully convert a bunch of markdown files to HTML and generate toc and a menu file.",
"main": "index.js",
"scripts": {
Expand Down
17 changes: 8 additions & 9 deletions src/Menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,18 @@ class Menu {
/**
* adds a new child to the menu items
*
* @param {String} title
* @param {String} permalink
* @param {Array} [categories]
* @param {Number} [weight]
* @param {Object} child
*
* @note - A child is expected to have title and permalink
*
* @public
*/
addChild (title, permalink, categories, weight) {
categories = categories || ['root']
weight = weight || 0
_.each(categories, (category) => {
addChild (child) {
child.categories = child.categories || ['root']
child.weight = child.weight || 0
_.each(child.categories, (category) => {
this.items[category] = this.items[category] || []
this.items[category].push({title, permalink, weight})
this.items[category].push(child)
})
}

Expand Down
9 changes: 9 additions & 0 deletions test/docs/menu-new.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@
{
"title": "Routing",
"permalink": "routing",
"categories": [
"basics"
],
"weight": 0
},
{
"title": "Controllers",
"permalink": "controllers",
"categories": [
"basics"
],
"weight": 0
}
],
"database": [
{
"title": "Model",
"permalink": "model",
"categories": [
"database"
],
"weight": 0
}
]
Expand Down
75 changes: 45 additions & 30 deletions test/menu.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,62 +20,74 @@ require('co-mocha')
describe('Menu', function () {
it('should add a new item to the list', function () {
const menu = new Menu()
menu.addChild('Routing', 'routing', ['basics'])
expect(menu.tree()).deep.equal({basics: [{title: 'Routing', permalink: 'routing', weight: 0}]})
const child = {title: 'Routing', permalink: 'routing', categories: ['basics']}
menu.addChild(child)
expect(menu.tree()).deep.equal({basics: [child]})
})

it('should add multiple items to the menu list', function () {
const menu = new Menu()
menu.addChild('Routing', 'routing', ['basics'])
menu.addChild('Controllers', 'controllers', ['basics'])
expect(menu.tree()).deep.equal({basics: [{title: 'Routing', permalink: 'routing', weight: 0}, {title: 'Controllers', permalink: 'controllers', weight: 0}]})
const child1 = {title: 'Routing', permalink: 'routing', categories: ['basics']}
const child2 = {title: 'Controllers', permalink: 'controllers', categories: ['basics']}
menu.addChild(child1)
menu.addChild(child2)
expect(menu.tree()).deep.equal({basics: [child1, child2]})
})

it('should add same child to multiple categories', function () {
const menu = new Menu()
menu.addChild('Ioc', 'ioc-container', ['basics', 'fundamentals'])
expect(menu.tree()).deep.equal({fundamentals: [{title: 'Ioc', permalink: 'ioc-container', weight: 0}], basics: [{title: 'Ioc', permalink: 'ioc-container', weight: 0}]})
const child = {title: 'Ioc', permalink: 'ioc-container', categories: ['basics', 'fundamentals']}
menu.addChild(child)
expect(menu.tree()).deep.equal({fundamentals: [child], basics: [child]})
})

it('should add same childs to root categories when no categories are defined', function () {
const menu = new Menu()
menu.addChild('Ioc', 'ioc-container')
expect(menu.tree()).deep.equal({root: [{title: 'Ioc', permalink: 'ioc-container', weight: 0}]})
const child = {title: 'Ioc', permalink: 'ioc-container'}
menu.addChild(child)
expect(menu.tree()).deep.equal({root: [child]})
})

it('should return item using the permalink', function () {
const menu = new Menu()
menu.addChild('Ioc', 'ioc-container', ['basics', 'fundamentals'])
menu.addChild('Routing', 'routing', ['basics'])
menu.addChild('Controllers', 'controllers', ['basics'])
const child = menu.getChild('ioc-container')
expect(child).deep.equal({permalink: 'ioc-container', title: 'Ioc', weight: 0})
const child1 = {title: 'Ioc', permalink: 'ioc-container', categories: ['basics', 'fundamentals']}
const child2 = {title: 'Routing', permalink: 'routing', categories: ['basics']}
const child3 = {title: 'Controllers', permalink: 'controllers', categories: ['basics']}
menu.addChild(child1)
menu.addChild(child2)
menu.addChild(child3)
const ioc = menu.getChild('ioc-container')
expect(ioc).deep.equal(child1)
const routes = menu.getChild('routing')
expect(routes).deep.equal({permalink: 'routing', title: 'Routing', weight: 0})
expect(routes).deep.equal(child2)
})

it('should return undefined when unable to find item using the permalink', function () {
const menu = new Menu()
menu.addChild('Ioc', 'ioc-container', ['basics', 'fundamentals'])
menu.addChild('Routing', 'routing', ['basics'])
menu.addChild('Controllers', 'controllers', ['basics'])
const child = menu.getChild('ioc')
expect(child).to.equal(undefined)
const child = {title: 'Ioc', permalink: 'ioc-container', categories: ['basics', 'fundamentals']}
menu.addChild(child)
const ioc = menu.getChild('ioc')
expect(ioc).to.equal(undefined)
})

it('should return the tree sorted by childs weight', function () {
const menu = new Menu()
menu.addChild('Routing', 'routing', ['basics'], 1)
menu.addChild('Controllers', 'controllers', ['basics'], 0)
expect(menu.tree()).deep.equal({basics: [{title: 'Controllers', permalink: 'controllers', weight: 0}, {title: 'Routing', permalink: 'routing', weight: 1}]})
const child1 = {title: 'Routing', permalink: 'routing', categories: ['basics'], weight: 1}
const child2 = {title: 'Controllers', permalink: 'controllers', categories: ['basics'], weight: 0}
menu.addChild(child1)
menu.addChild(child2)
expect(menu.tree()).deep.equal({basics: [child2, child1]})
})

it('should return tree only for the defined categories', function () {
const menu = new Menu()
menu.addChild('Routing', 'routing', ['basics'])
menu.addChild('Controllers', 'controllers', ['basics'])
menu.addChild('Model', 'model', ['database'])
expect(menu.tree(['database'])).deep.equal({database: [{title: 'Model', permalink: 'model', weight: 0}]})
const child1 = {title: 'Routing', permalink: 'routing', categories: ['basics']}
const child2 = {title: 'Controllers', permalink: 'controllers', categories: ['basics']}
const child3 = {title: 'Model', permalink: 'model', categories: ['database']}
menu.addChild(child1)
menu.addChild(child2)
menu.addChild(child3)
expect(menu.tree(['database'])).deep.equal({database: [child3]})
})

it('should be able to load menu items from a json file', function * () {
Expand All @@ -93,9 +105,12 @@ describe('Menu', function () {
it('should write menu items to a file', function * () {
const menuFilePath = path.join(__dirname, './docs/menu-new.json')
const menu = new Menu(menuFilePath)
menu.addChild('Routing', 'routing', ['basics'])
menu.addChild('Controllers', 'controllers', ['basics'])
menu.addChild('Model', 'model', ['database'])
const child1 = {title: 'Routing', permalink: 'routing', categories: ['basics']}
const child2 = {title: 'Controllers', permalink: 'controllers', categories: ['basics']}
const child3 = {title: 'Model', permalink: 'model', categories: ['database']}
menu.addChild(child1)
menu.addChild(child2)
menu.addChild(child3)
yield menu.write()
const contents = yield Q.nfcall(fsExtra.readJSON, menuFilePath)
expect(contents).deep.equal(menu.items)
Expand Down

0 comments on commit f714127

Please sign in to comment.