From b556df0e4a2415ac8180b887c29ce2b1e3b2cab6 Mon Sep 17 00:00:00 2001 From: surunzi Date: Fri, 9 Sep 2016 21:17:08 +0800 Subject: [PATCH] Add: Snippets tests, api doc --- doc/Tool_Api.md | 35 ++++++++++++++++++++++++++++++++++ src/Snippets/Snippets.es6 | 28 ++++++++++++++++++++++----- test/snippets.js | 40 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 5 deletions(-) diff --git a/doc/Tool_Api.md b/doc/Tool_Api.md index e147a20..8d3e41e 100644 --- a/doc/Tool_Api.md +++ b/doc/Tool_Api.md @@ -76,6 +76,41 @@ console.html('Red'); ## Snippets +Allow you to register small function that can be triggered multiple times. + +### clear + +Clear Snippets. + +### add + +Add Snippet. + +|Name|Type |Desc | +|----|--------|------------------------| +|name|string |Snippet name | +|fn |function|Function to be triggered| +|desc|string |Snippet description | + +```javascript +snippets.add('hello', function () +{ + console.log('Hello World!'); +}, 'Display hello on console'); +``` + +### remove + +Remove specified log. + +|Name|Type |Desc | +|----|------|-----------------| +|name|string|Snippet to remove| + +```javascript +snippets.remove('hello'); +``` + ## Features ## Settings diff --git a/src/Snippets/Snippets.es6 b/src/Snippets/Snippets.es6 index bf299b9..6c33926 100644 --- a/src/Snippets/Snippets.es6 +++ b/src/Snippets/Snippets.es6 @@ -24,13 +24,31 @@ export default class Snippets extends Tool } add(name, fn, desc) { - this._snippets.push({ - name: name, - fn: fn, - desc: desc - }); + this._snippets.push({name, fn, desc}); this._render(); + + return this; + } + remove(name) + { + let snippets = this._snippets; + + for (let i = 0, len = snippets.length; i < len; i++) + { + if (snippets[i].name === name) snippets.splice(i, 1); + } + + this._render(); + + return this; + } + clear() + { + this._snippets = []; + this._render(); + + return this; } _bindEvent() { diff --git a/test/snippets.js b/test/snippets.js index e69de29..8e54060 100644 --- a/test/snippets.js +++ b/test/snippets.js @@ -0,0 +1,40 @@ +var tool = eruda.get('snippets'), + $tool = $('.eruda-snippets'); + +describe('default', function () +{ + it('border all', function () + { + expect($tool.find('.eruda-name').eq(0)).toContainText('Border All'); + }); + it('refresh page', function () + { + expect($tool.find('.eruda-name').eq(1)).toContainText('Refresh Page'); + }); +}); + +describe('basic', function () +{ + it('clear', function () + { + tool.clear(); + expect($tool.find('.eruda-name')).toHaveLength(0); + }); + + it('add', function () + { + tool.add('Test', function () + { + console.log('eruda'); + }, 'This is the description'); + expect($tool.find('.eruda-name')).toContainText('Test'); + expect($tool.find('.eruda-description')).toContainText('This is the description'); + }); + + it('remove', function () + { + tool.remove('Test'); + expect($tool.find('.eruda-name')).toHaveLength(0); + }); +}); +