Skip to content

Commit

Permalink
Convert tests to esm
Browse files Browse the repository at this point in the history
  • Loading branch information
adrinr committed Dec 2, 2024
1 parent ce58668 commit de4a7a4
Show file tree
Hide file tree
Showing 22 changed files with 1,051 additions and 760 deletions.
92 changes: 69 additions & 23 deletions test/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import { array as _array, string, object } from '..';
_array({ handlebars: hbs });
string({ handlebars: hbs });

var context = { array: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], duplicate: ['a', 'b', 'b', 'c', 'd', 'b', 'f', 'a', 'g'] };
var context = {
array: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
duplicate: ['a', 'b', 'b', 'c', 'd', 'b', 'f', 'a', 'g']
};

describe('array', function() {
describe('after', function() {
Expand All @@ -31,7 +34,10 @@ describe('array', function() {
describe('arrayify', function() {
it('should arrayify a value', function() {
equal(hbs.compile('{{#each (arrayify .)}}{{.}}{{/each}}')('foo'), 'foo');
equal(hbs.compile('{{#each (arrayify .)}}{{.}}{{/each}}')(['foo']), 'foo');
equal(
hbs.compile('{{#each (arrayify .)}}{{.}}{{/each}}')(['foo']),
'foo'
);
});
});

Expand All @@ -53,9 +59,14 @@ describe('array', function() {
});

describe('eachIndex', function() {
it('should render the block using the array and each item\'s index', function() {
var fn = hbs.compile('{{#eachIndex array}} {{item}} is {{index}} {{/eachIndex}}');
equal(fn(context), ' a is 0 b is 1 c is 2 d is 3 e is 4 f is 5 g is 6 h is 7 ');
it("should render the block using the array and each item's index", function() {
var fn = hbs.compile(
'{{#eachIndex array}} {{item}} is {{index}} {{/eachIndex}}'
);
equal(
fn(context),
' a is 0 b is 1 c is 2 d is 3 e is 4 f is 5 g is 6 h is 7 '
);
});
});

Expand Down Expand Up @@ -97,7 +108,6 @@ describe('array', function() {
});

it('should render a block for each object that has a "first" property with the value "d"', function() {

var ctx = {
collection: [
{ first: 'aaa', last: 'bbb' },
Expand All @@ -111,7 +121,8 @@ describe('array', function() {
]
};

var source = '{{#filter collection "d" property="first"}}{{this.first}}{{else}}ZZZ{{/filter}}';
var source =
'{{#filter collection "d" property="first"}}{{this.first}}{{else}}ZZZ{{/filter}}';
var fn = hbs.compile(source);
equal(fn(ctx), 'd');
});
Expand Down Expand Up @@ -161,15 +172,17 @@ describe('array', function() {
});

it('should render the inverse block when a value does not exist', function() {
var fn = hbs.compile('{{#inArray array "foo"}}AAA{{else}}BBB{{/inArray}}');
var fn = hbs.compile(
'{{#inArray array "foo"}}AAA{{else}}BBB{{/inArray}}'
);
equal(fn(context), 'BBB');
});
});

describe('isArray', function() {
it('should return true if the value is an array', function() {
equal(hbs.compile('{{isArray "foo"}}')(), 'false');
equal(hbs.compile('{{isArray \'["foo"]\'}}')(), 'false');
equal(hbs.compile("{{isArray '[\"foo\"]'}}")(), 'false');
equal(hbs.compile('{{isArray foo}}')({ foo: ['foo'] }), 'true');
equal(hbs.compile('{{isArray (arrayify "foo")}}')(), 'true');
equal(hbs.compile('{{isArray (arrayify ["foo"])}}')(), 'true');
Expand All @@ -180,16 +193,23 @@ describe('array', function() {
var ctx = { array: ['foo', 'bar', 'baz'] };

it('should return a null value for undefined array', function() {
equal(hbs.compile('{{#if (itemAt)}}exists{{else}}notfound{{/if}}')(), 'notfound');
equal(
hbs.compile('{{#if (itemAt)}}exists{{else}}notfound{{/if}}')(),
'notfound'
);
});

it('should return a null value for empty array', function() {
var fn = hbs.compile('{{#if (itemAt array)}}exists{{else}}notfound{{/if}}');
var fn = hbs.compile(
'{{#if (itemAt array)}}exists{{else}}notfound{{/if}}'
);
equal(fn({ array: [] }), 'notfound');
});

it('should return a null value for exceed bound', function() {
var fn = hbs.compile('{{#if (itemAt array 999)}}exists{{else}}notfound{{/if}}');
var fn = hbs.compile(
'{{#if (itemAt array 999)}}exists{{else}}notfound{{/if}}'
);
equal(fn(ctx), 'notfound');
});

Expand Down Expand Up @@ -250,12 +270,16 @@ describe('array', function() {

describe('lengthEqual', function() {
it('should render the first block if length is the given number', function() {
var fn = hbs.compile('{{#lengthEqual array 8}}AAA{{else}}BBB{{/lengthEqual}}');
var fn = hbs.compile(
'{{#lengthEqual array 8}}AAA{{else}}BBB{{/lengthEqual}}'
);
equal(fn(context), 'AAA');
});

it('should render the inverse block if length is not the given number', function() {
var fn = hbs.compile('{{#lengthEqual array 3}}AAA{{else}}BBB{{/lengthEqual}}');
var fn = hbs.compile(
'{{#lengthEqual array 3}}AAA{{else}}BBB{{/lengthEqual}}'
);
equal(fn(context), 'BBB');
});
});
Expand Down Expand Up @@ -412,23 +436,38 @@ describe('array', function() {

describe('withFirst', function() {
it('should use the first item in an array', function() {
var fn = hbs.compile('{{#withFirst array}}{{this}} is smart.{{/withFirst}}');
var fn = hbs.compile(
'{{#withFirst array}}{{this}} is smart.{{/withFirst}}'
);
equal(fn(context), 'a is smart.');
});
it('should return an empty string when no array is passed:', function() {
equal(hbs.compile('{{#withFirst}}{{/withFirst}}')(), '');
});
it('should use the first two items in an array', function() {
var fn = hbs.compile('{{#withFirst array 2}}{{this}} is smart.{{/withFirst}}');
var fn = hbs.compile(
'{{#withFirst array 2}}{{this}} is smart.{{/withFirst}}'
);
equal(fn(context), 'a is smart.b is smart.');
});
});

describe('withGroup', function() {
it('should iterate over an array grouping elements by a given number', function() {
var fn = hbs.compile('{{#withGroup collection 4}}{{#each this}}{{name}}{{/each}}<br>{{/withGroup}}');
var fn = hbs.compile(
'{{#withGroup collection 4}}{{#each this}}{{name}}{{/each}}<br>{{/withGroup}}'
);
var res = fn({
collection: [{ name: 'a' }, { name: 'b' }, { name: 'c' }, { name: 'd' }, { name: 'e' }, { name: 'f' }, { name: 'g' }, { name: 'h' }]
collection: [
{ name: 'a' },
{ name: 'b' },
{ name: 'c' },
{ name: 'd' },
{ name: 'e' },
{ name: 'f' },
{ name: 'g' },
{ name: 'h' }
]
});
equal(res, 'abcd<br>efgh<br>');
});
Expand All @@ -443,7 +482,9 @@ describe('array', function() {
equal(fn(context), 'h is dumb.');
});
it('should use the last two items in an array', function() {
var fn = hbs.compile('{{#withLast array 2}}{{this}} is dumb.{{/withLast}}');
var fn = hbs.compile(
'{{#withLast array 2}}{{this}} is dumb.{{/withLast}}'
);
equal(fn(context), 'g is dumb.h is dumb.');
});
});
Expand All @@ -460,12 +501,16 @@ describe('array', function() {
});

it('should sort the array in reverse order', function() {
var fn = hbs.compile('{{#withSort array reverse="true"}}{{this}}{{/withSort}}');
var fn = hbs.compile(
'{{#withSort array reverse="true"}}{{this}}{{/withSort}}'
);
equal(fn(context), 'hgfedcba');
});

it('should sort the array by deliveries', function() {
var fn = hbs.compile('{{#withSort collection "deliveries"}}{{name}}: {{deliveries}} <br>{{/withSort}}');
var fn = hbs.compile(
'{{#withSort collection "deliveries"}}{{name}}: {{deliveries}} <br>{{/withSort}}'
);
var res = fn({
collection: [
{ name: 'f', deliveries: 8021 },
Expand All @@ -477,7 +522,9 @@ describe('array', function() {
});

it('should sort the array by deliveries in reverse order', function() {
var fn = hbs.compile('{{#withSort collection "deliveries" reverse="true"}}{{name}}: {{deliveries}} <br>{{/withSort}}');
var fn = hbs.compile(
'{{#withSort collection "deliveries" reverse="true"}}{{name}}: {{deliveries}} <br>{{/withSort}}'
);
var res = fn({
collection: [
{ name: 'f', deliveries: 8021 },
Expand All @@ -499,5 +546,4 @@ describe('array', function() {
equal(fn(context).toString(), 'a,b,c,d,f,g');
});
});

});
115 changes: 66 additions & 49 deletions test/code.js
Original file line number Diff line number Diff line change
@@ -1,88 +1,105 @@
'use strict';

require('mocha');
var assert = require('assert');
import 'mocha';
import { equal, throws } from 'assert';
var hbs = require('handlebars').create();
var helpers = require('..');
helpers.code({handlebars: hbs});
import { code } from '..';
code({ handlebars: hbs });

describe('code', function() {
describe('embed', function() {
it('should embed markdown:', function() {
assert.equal(hbs.compile('{{{embed "test/fixtures/simple.md"}}}')(), [
'```markdown',
'## Some Markdown\n',
' - one',
' - two',
' - three\n',
'[Click here](http://github.com)\n',
'```\n'
].join('\n'));
equal(
hbs.compile('{{{embed "test/fixtures/simple.md"}}}')(),
[
'```markdown',
'## Some Markdown\n',
' - one',
' - two',
' - three\n',
'[Click here](http://github.com)\n',
'```\n'
].join('\n')
);
});

it('should determine the language from the file extension', function() {
assert.equal(hbs.compile('{{{embed "test/fixtures/embedded.md"}}}')(), [
'```markdown',
'## Markdown',
'',
'Code example',
'',
'&#x60&#x60&#x60js',
'var urlresolve = function(base, href) {',
' return url.resolve(base, href);',
'};',
'&#x60&#x60&#x60',
'',
'[Click here](http://assemble.io) for more documentation.',
'',
'```\n'
].join('\n'));
equal(
hbs.compile('{{{embed "test/fixtures/embedded.md"}}}')(),
[
'```markdown',
'## Markdown',
'',
'Code example',
'',
'&#x60&#x60&#x60js',
'var urlresolve = function(base, href) {',
' return url.resolve(base, href);',
'};',
'&#x60&#x60&#x60',
'',
'[Click here](http://assemble.io) for more documentation.',
'',
'```\n'
].join('\n')
);
});

it('should use the language defined in the last argument', function() {
var template = hbs.compile('{{{embed "test/fixtures/index.html" "hbs"}}}');
assert.equal(template(), [
'```hbs',
'<!DOCTYPE html>',
' <html lang="en">',
' <head>',
' <meta charset="UTF-8">',
' <title>{{title}}</title>',
' </head>',
' <body>',
' {{> foo }}',
' </body>',
'</html>',
'',
'```\n'
].join('\n'));
var template = hbs.compile(
'{{{embed "test/fixtures/index.html" "hbs"}}}'
);
equal(
template(),
[
'```hbs',
'<!DOCTYPE html>',
' <html lang="en">',
' <head>',
' <meta charset="UTF-8">',
' <title>{{title}}</title>',
' </head>',
' <body>',
' {{> foo }}',
' </body>',
'</html>',
'',
'```\n'
].join('\n')
);
});
});

describe('gist', function() {
it('should return a gist script tag', function() {
var fn = hbs.compile('{{{gist "abcdefg"}}}');
assert.equal(fn(), '<script src="https://gist.github.com/abcdefg.js"></script>');
equal(fn(), '<script src="https://gist.github.com/abcdefg.js"></script>');
});
});

describe('jsfiddle', function() {
it('should return a jsfiddle embed link, with default tabs assigned', function() {
var source = '{{{jsfiddle id="UXbas"}}}';
var fn = hbs.compile(source);
assert.equal(fn(), '<iframe width="100%" height="300" src="http://jsfiddle.net/UXbas/embedded/result,js,html,css/presentation/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>');
equal(
fn(),
'<iframe width="100%" height="300" src="http://jsfiddle.net/UXbas/embedded/result,js,html,css/presentation/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>'
);
});

it('should throw an error if id is missing', function() {
assert.throws(function() {
throws(function() {
hbs.compile('{{jsfiddle}}')();
});
});

it('should return a jsfiddle embed link, with custom tabs assigned', function() {
var source = '{{{jsfiddle id="UXbas" tabs="html,css"}}}';
var fn = hbs.compile(source);
assert.equal(fn(), '<iframe width="100%" height="300" src="http://jsfiddle.net/UXbas/embedded/html,css/presentation/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>');
equal(
fn(),
'<iframe width="100%" height="300" src="http://jsfiddle.net/UXbas/embedded/html,css/presentation/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>'
);
});
});
});
Loading

0 comments on commit de4a7a4

Please sign in to comment.