summaryrefslogtreecommitdiffstats
path: root/lib/models/__tests__
diff options
context:
space:
mode:
Diffstat (limited to 'lib/models/__tests__')
-rw-r--r--lib/models/__tests__/config.js63
-rw-r--r--lib/models/__tests__/glossary.js42
-rw-r--r--lib/models/__tests__/glossaryEntry.js17
-rw-r--r--lib/models/__tests__/plugin.js29
-rw-r--r--lib/models/__tests__/summary.js81
-rw-r--r--lib/models/__tests__/templateBlock.js106
-rw-r--r--lib/models/__tests__/templateEngine.js51
7 files changed, 389 insertions, 0 deletions
diff --git a/lib/models/__tests__/config.js b/lib/models/__tests__/config.js
new file mode 100644
index 0000000..8445cef
--- /dev/null
+++ b/lib/models/__tests__/config.js
@@ -0,0 +1,63 @@
+jest.autoMockOff();
+
+var Immutable = require('immutable');
+
+describe('Config', function() {
+ var Config = require('../config');
+
+ var config = Config.createWithValues({
+ hello: {
+ world: 1,
+ test: 'Hello',
+ isFalse: false
+ }
+ });
+
+ describe('getValue', function() {
+ it('must return value as immutable', function() {
+ var value = config.getValue('hello');
+ expect(Immutable.Map.isMap(value)).toBeTruthy();
+ });
+
+ it('must return deep value', function() {
+ var value = config.getValue('hello.world');
+ expect(value).toBe(1);
+ });
+
+ it('must return default value if non existant', function() {
+ var value = config.getValue('hello.nonExistant', 'defaultValue');
+ expect(value).toBe('defaultValue');
+ });
+
+ it('must not return default value for falsy values', function() {
+ var value = config.getValue('hello.isFalse', 'defaultValue');
+ expect(value).toBe(false);
+ });
+ });
+
+ describe('setValue', function() {
+ it('must set value as immutable', function() {
+ var testConfig = config.setValue('hello', {
+ 'cool': 1
+ });
+ var value = testConfig.getValue('hello');
+
+ expect(Immutable.Map.isMap(value)).toBeTruthy();
+ expect(value.size).toBe(1);
+ expect(value.has('cool')).toBeTruthy();
+ });
+
+ it('must set deep value', function() {
+ var testConfig = config.setValue('hello.world', 2);
+ var hello = testConfig.getValue('hello');
+ var world = testConfig.getValue('hello.world');
+
+ expect(Immutable.Map.isMap(hello)).toBeTruthy();
+ expect(hello.size).toBe(3);
+
+ expect(world).toBe(2);
+ });
+ });
+});
+
+
diff --git a/lib/models/__tests__/glossary.js b/lib/models/__tests__/glossary.js
new file mode 100644
index 0000000..2ce224c
--- /dev/null
+++ b/lib/models/__tests__/glossary.js
@@ -0,0 +1,42 @@
+jest.autoMockOff();
+
+describe('Glossary', function() {
+ var File = require('../file');
+ var Glossary = require('../glossary');
+ var GlossaryEntry = require('../glossaryEntry');
+
+ var glossary = Glossary.createFromEntries(File(), [
+ {
+ name: 'Hello World',
+ description: 'Awesome!'
+ },
+ {
+ name: 'JavaScript',
+ description: 'This is a cool language'
+ }
+ ]);
+
+ describe('createFromEntries', function() {
+ it('must add all entries', function() {
+ var entries = glossary.getEntries();
+ expect(entries.size).toBe(2);
+ });
+
+ it('must add entries as GlossaryEntries', function() {
+ var entries = glossary.getEntries();
+ var entry = entries.get('hello-world');
+ expect(entry instanceof GlossaryEntry).toBeTruthy();
+ });
+ });
+
+ describe('toText', function() {
+ pit('return as markdown', function() {
+ return glossary.toText('.md')
+ .then(function(text) {
+ expect(text).toContain('# Glossary');
+ });
+ });
+ });
+});
+
+
diff --git a/lib/models/__tests__/glossaryEntry.js b/lib/models/__tests__/glossaryEntry.js
new file mode 100644
index 0000000..9eabc68
--- /dev/null
+++ b/lib/models/__tests__/glossaryEntry.js
@@ -0,0 +1,17 @@
+jest.autoMockOff();
+
+describe('GlossaryEntry', function() {
+ var GlossaryEntry = require('../glossaryEntry');
+
+ describe('getID', function() {
+ it('must return a normalized ID', function() {
+ var entry = new GlossaryEntry({
+ name: 'Hello World'
+ });
+
+ expect(entry.getID()).toBe('hello-world');
+ });
+ });
+});
+
+
diff --git a/lib/models/__tests__/plugin.js b/lib/models/__tests__/plugin.js
new file mode 100644
index 0000000..81d9d51
--- /dev/null
+++ b/lib/models/__tests__/plugin.js
@@ -0,0 +1,29 @@
+jest.autoMockOff();
+
+describe('Plugin', function() {
+ var Plugin = require('../plugin');
+
+ describe('createFromString', function() {
+ it('must parse name', function() {
+ var plugin = Plugin.createFromString('hello');
+ expect(plugin.getName()).toBe('hello');
+ expect(plugin.getVersion()).toBe('*');
+ });
+
+ it('must parse version', function() {
+ var plugin = Plugin.createFromString('hello@1.0.0');
+ expect(plugin.getName()).toBe('hello');
+ expect(plugin.getVersion()).toBe('1.0.0');
+ });
+ });
+
+ describe('isLoaded', function() {
+ it('must return false for empty plugin', function() {
+ var plugin = Plugin.createFromString('hello');
+ expect(plugin.isLoaded()).toBe(false);
+ });
+
+ });
+});
+
+
diff --git a/lib/models/__tests__/summary.js b/lib/models/__tests__/summary.js
new file mode 100644
index 0000000..ad040cf
--- /dev/null
+++ b/lib/models/__tests__/summary.js
@@ -0,0 +1,81 @@
+
+describe('Summary', function() {
+ var File = require('../file');
+ var Summary = require('../summary');
+
+ var summary = Summary.createFromParts(File(), [
+ {
+ articles: [
+ {
+ title: 'My First Article',
+ path: 'README.md'
+ },
+ {
+ title: 'My Second Article',
+ path: 'article.md'
+ }
+ ]
+ },
+ {
+ title: 'Test'
+ }
+ ]);
+
+ describe('createFromEntries', function() {
+ it('must add all parts', function() {
+ var parts = summary.getParts();
+ expect(parts.size).toBe(2);
+ });
+ });
+
+ describe('getByLevel', function() {
+ it('can return a Part', function() {
+ var part = summary.getByLevel('1');
+
+ expect(part).toBeDefined();
+ expect(part.getArticles().size).toBe(2);
+ });
+
+ it('can return a Part (2)', function() {
+ var part = summary.getByLevel('2');
+
+ expect(part).toBeDefined();
+ expect(part.getTitle()).toBe('Test');
+ expect(part.getArticles().size).toBe(0);
+ });
+
+ it('can return an Article', function() {
+ var article = summary.getByLevel('1.1');
+
+ expect(article).toBeDefined();
+ expect(article.getTitle()).toBe('My First Article');
+ });
+ });
+
+ describe('getByPath', function() {
+ it('return correct article', function() {
+ var article = summary.getByPath('README.md');
+
+ expect(article).toBeDefined();
+ expect(article.getTitle()).toBe('My First Article');
+ });
+
+ it('return correct article', function() {
+ var article = summary.getByPath('article.md');
+
+ expect(article).toBeDefined();
+ expect(article.getTitle()).toBe('My Second Article');
+ });
+ });
+
+ describe('toText', function() {
+ pit('return as markdown', function() {
+ return summary.toText('.md')
+ .then(function(text) {
+ expect(text).toContain('# Summary');
+ });
+ });
+ });
+});
+
+
diff --git a/lib/models/__tests__/templateBlock.js b/lib/models/__tests__/templateBlock.js
new file mode 100644
index 0000000..44d53de
--- /dev/null
+++ b/lib/models/__tests__/templateBlock.js
@@ -0,0 +1,106 @@
+var nunjucks = require('nunjucks');
+var Immutable = require('immutable');
+var Promise = require('../../utils/promise');
+
+describe('TemplateBlock', function() {
+ var TemplateBlock = require('../templateBlock');
+
+ describe('create', function() {
+ pit('must initialize a simple TemplateBlock from a function', function() {
+ var templateBlock = TemplateBlock.create('sayhello', function(block) {
+ return '<p>Hello, World!</p>';
+ });
+
+ // Check basic templateBlock properties
+ expect(templateBlock.getName()).toBe('sayhello');
+ expect(templateBlock.getPost()).toBeNull();
+ expect(templateBlock.getParse()).toBeTruthy();
+ expect(templateBlock.getEndTag()).toBe('endsayhello');
+ expect(templateBlock.getBlocks().size).toBe(0);
+ expect(templateBlock.getShortcuts().size).toBe(0);
+ expect(templateBlock.getExtensionName()).toBe('BlocksayhelloExtension');
+
+ // Check result of applying block
+ return Promise()
+ .then(function() {
+ return templateBlock.applyBlock();
+ })
+ .then(function(result) {
+ expect(result.name).toBe('sayhello');
+ expect(result.body).toBe('<p>Hello, World!</p>');
+ });
+ });
+ });
+
+ describe('toNunjucksExt()', function() {
+ pit('must create a valid nunjucks extension', function() {
+ var templateBlock = TemplateBlock.create('sayhello', function(block) {
+ return '<p>Hello, World!</p>';
+ });
+
+ // Create a fresh Nunjucks environment
+ var env = new nunjucks.Environment(null, { autoescape: false });
+
+ // Add template block to environement
+ var Ext = templateBlock.toNunjucksExt();
+ env.addExtension(templateBlock.getExtensionName(), new Ext());
+
+ // Render a template using the block
+ var src = '{% sayhello %}{% endsayhello %}';
+ return Promise.nfcall(env.renderString.bind(env), src)
+ .then(function(res) {
+ expect(res).toBe('<p>Hello, World!</p>');
+ });
+ });
+
+ pit('must apply block arguments correctly', function() {
+ var templateBlock = TemplateBlock.create('sayhello', function(block) {
+ return '<'+block.kwargs.tag+'>Hello, '+block.kwargs.name+'!</'+block.kwargs.tag+'>';
+ });
+
+ // Create a fresh Nunjucks environment
+ var env = new nunjucks.Environment(null, { autoescape: false });
+
+ // Add template block to environement
+ var Ext = templateBlock.toNunjucksExt();
+ env.addExtension(templateBlock.getExtensionName(), new Ext());
+
+ // Render a template using the block
+ var src = '{% sayhello name="Samy", tag="p" %}{% endsayhello %}';
+ return Promise.nfcall(env.renderString.bind(env), src)
+ .then(function(res) {
+ expect(res).toBe('<p>Hello, Samy!</p>');
+ });
+ });
+
+ pit('must handle nested blocks', function() {
+ var templateBlock = new TemplateBlock({
+ name: 'yoda',
+ blocks: Immutable.List(['start', 'end']),
+ process: function(block) {
+ var nested = {};
+
+ block.blocks.forEach(function(blk) {
+ nested[blk.name] = blk.body.trim();
+ });
+
+ return '<p class="yoda">'+nested.end+' '+nested.start+'</p>';
+ }
+ });
+
+ // Create a fresh Nunjucks environment
+ var env = new nunjucks.Environment(null, { autoescape: false });
+
+ // Add template block to environement
+ var Ext = templateBlock.toNunjucksExt();
+ env.addExtension(templateBlock.getExtensionName(), new Ext());
+
+ // Render a template using the block
+ var src = '{% yoda %}{% start %}this sentence should be{% end %}inverted{% endyoda %}';
+ return Promise.nfcall(env.renderString.bind(env), src)
+ .then(function(res) {
+ expect(res).toBe('<p class="yoda">inverted this sentence should be</p>');
+ });
+ });
+ });
+}); \ No newline at end of file
diff --git a/lib/models/__tests__/templateEngine.js b/lib/models/__tests__/templateEngine.js
new file mode 100644
index 0000000..6f18b18
--- /dev/null
+++ b/lib/models/__tests__/templateEngine.js
@@ -0,0 +1,51 @@
+
+describe('TemplateBlock', function() {
+ var TemplateEngine = require('../templateEngine');
+
+ describe('create', function() {
+ it('must initialize with a list of filters', function() {
+ var engine = TemplateEngine.create({
+ filters: {
+ hello: function(name) {
+ return 'Hello ' + name + '!';
+ }
+ }
+ });
+ var env = engine.toNunjucks();
+ var res = env.renderString('{{ "Luke"|hello }}');
+
+ expect(res).toBe('Hello Luke!');
+ });
+
+ it('must initialize with a list of globals', function() {
+ var engine = TemplateEngine.create({
+ globals: {
+ hello: function(name) {
+ return 'Hello ' + name + '!';
+ }
+ }
+ });
+ var env = engine.toNunjucks();
+ var res = env.renderString('{{ hello("Luke") }}');
+
+ expect(res).toBe('Hello Luke!');
+ });
+
+ it('must pass context to filters and blocks', function() {
+ var engine = TemplateEngine.create({
+ filters: {
+ hello: function(name) {
+ return 'Hello ' + name + ' ' + this.lastName + '!';
+ }
+ },
+ context: {
+ lastName: 'Skywalker'
+ }
+ });
+ var env = engine.toNunjucks();
+ var res = env.renderString('{{ "Luke"|hello }}');
+
+ expect(res).toBe('Hello Luke Skywalker!');
+ });
+ });
+}); \ No newline at end of file