refator: In spec tests, use expectTemplate over equals and shouldThrow (#1683)

This commit is contained in:
John Boehr
2020-05-04 15:04:33 -07:00
committed by GitHub
parent 3789a30955
commit 77825f8d35
17 changed files with 3346 additions and 4120 deletions
+212 -226
View File
@@ -1,61 +1,57 @@
describe('subexpressions', function() {
it('arg-less helper', function() {
var string = '{{foo (bar)}}!';
var context = {};
var helpers = {
foo: function(val) {
return val + val;
},
bar: function() {
return 'LOL';
}
};
shouldCompileTo(string, [context, helpers], 'LOLLOL!');
expectTemplate('{{foo (bar)}}!')
.withHelpers({
foo: function(val) {
return val + val;
},
bar: function() {
return 'LOL';
}
})
.toCompileTo('LOLLOL!');
});
it('helper w args', function() {
var string = '{{blog (equal a b)}}';
var context = { bar: 'LOL' };
var helpers = {
blog: function(val) {
return 'val is ' + val;
},
equal: function(x, y) {
return x === y;
}
};
shouldCompileTo(string, [context, helpers], 'val is true');
expectTemplate('{{blog (equal a b)}}')
.withInput({ bar: 'LOL' })
.withHelpers({
blog: function(val) {
return 'val is ' + val;
},
equal: function(x, y) {
return x === y;
}
})
.toCompileTo('val is true');
});
it('mixed paths and helpers', function() {
var string = '{{blog baz.bat (equal a b) baz.bar}}';
var context = { bar: 'LOL', baz: { bat: 'foo!', bar: 'bar!' } };
var helpers = {
blog: function(val, that, theOther) {
return 'val is ' + val + ', ' + that + ' and ' + theOther;
},
equal: function(x, y) {
return x === y;
}
};
shouldCompileTo(string, [context, helpers], 'val is foo!, true and bar!');
expectTemplate('{{blog baz.bat (equal a b) baz.bar}}')
.withInput({ bar: 'LOL', baz: { bat: 'foo!', bar: 'bar!' } })
.withHelpers({
blog: function(val, that, theOther) {
return 'val is ' + val + ', ' + that + ' and ' + theOther;
},
equal: function(x, y) {
return x === y;
}
})
.toCompileTo('val is foo!, true and bar!');
});
it('supports much nesting', function() {
var string = '{{blog (equal (equal true true) true)}}';
var context = { bar: 'LOL' };
var helpers = {
blog: function(val) {
return 'val is ' + val;
},
equal: function(x, y) {
return x === y;
}
};
shouldCompileTo(string, [context, helpers], 'val is true');
expectTemplate('{{blog (equal (equal true true) true)}}')
.withInput({ bar: 'LOL' })
.withHelpers({
blog: function(val) {
return 'val is ' + val;
},
equal: function(x, y) {
return x === y;
}
})
.toCompileTo('val is true');
});
it('GH-800 : Complex subexpressions', function() {
@@ -69,20 +65,33 @@ describe('subexpressions', function() {
}
};
shouldCompileTo(
"{{dash 'abc' (concat a b)}}",
[context, helpers],
'abc-ab'
);
shouldCompileTo('{{dash d (concat a b)}}', [context, helpers], 'd-ab');
shouldCompileTo('{{dash c.c (concat a b)}}', [context, helpers], 'c-ab');
shouldCompileTo('{{dash (concat a b) c.c}}', [context, helpers], 'ab-c');
shouldCompileTo('{{dash (concat a e.e) c.c}}', [context, helpers], 'ae-c');
expectTemplate("{{dash 'abc' (concat a b)}}")
.withInput(context)
.withHelpers(helpers)
.toCompileTo('abc-ab');
expectTemplate('{{dash d (concat a b)}}')
.withInput(context)
.withHelpers(helpers)
.toCompileTo('d-ab');
expectTemplate('{{dash c.c (concat a b)}}')
.withInput(context)
.withHelpers(helpers)
.toCompileTo('c-ab');
expectTemplate('{{dash (concat a b) c.c}}')
.withInput(context)
.withHelpers(helpers)
.toCompileTo('ab-c');
expectTemplate('{{dash (concat a e.e) c.c}}')
.withInput(context)
.withHelpers(helpers)
.toCompileTo('ae-c');
});
it('provides each nested helper invocation its own options hash', function() {
var string = '{{equal (equal true true) true}}';
var lastOptions = null;
var helpers = {
equal: function(x, y, options) {
@@ -93,200 +102,177 @@ describe('subexpressions', function() {
return x === y;
}
};
shouldCompileTo(string, [{}, helpers], 'true');
expectTemplate('{{equal (equal true true) true}}')
.withHelpers(helpers)
.toCompileTo('true');
});
it('with hashes', function() {
var string = "{{blog (equal (equal true true) true fun='yes')}}";
var context = { bar: 'LOL' };
var helpers = {
blog: function(val) {
return 'val is ' + val;
},
equal: function(x, y) {
return x === y;
}
};
shouldCompileTo(string, [context, helpers], 'val is true');
expectTemplate("{{blog (equal (equal true true) true fun='yes')}}")
.withInput({ bar: 'LOL' })
.withHelpers({
blog: function(val) {
return 'val is ' + val;
},
equal: function(x, y) {
return x === y;
}
})
.toCompileTo('val is true');
});
it('as hashes', function() {
var string = "{{blog fun=(equal (blog fun=1) 'val is 1')}}";
var helpers = {
blog: function(options) {
return 'val is ' + options.hash.fun;
},
equal: function(x, y) {
return x === y;
}
};
shouldCompileTo(string, [{}, helpers], 'val is true');
expectTemplate("{{blog fun=(equal (blog fun=1) 'val is 1')}}")
.withHelpers({
blog: function(options) {
return 'val is ' + options.hash.fun;
},
equal: function(x, y) {
return x === y;
}
})
.toCompileTo('val is true');
});
it('multiple subexpressions in a hash', function() {
var string =
'{{input aria-label=(t "Name") placeholder=(t "Example User")}}';
var helpers = {
input: function(options) {
var hash = options.hash;
var ariaLabel = Handlebars.Utils.escapeExpression(hash['aria-label']);
var placeholder = Handlebars.Utils.escapeExpression(hash.placeholder);
return new Handlebars.SafeString(
'<input aria-label="' +
ariaLabel +
'" placeholder="' +
placeholder +
'" />'
);
},
t: function(defaultString) {
return new Handlebars.SafeString(defaultString);
}
};
shouldCompileTo(
string,
[{}, helpers],
'<input aria-label="Name" placeholder="Example User" />'
);
expectTemplate(
'{{input aria-label=(t "Name") placeholder=(t "Example User")}}'
)
.withHelpers({
input: function(options) {
var hash = options.hash;
var ariaLabel = Handlebars.Utils.escapeExpression(hash['aria-label']);
var placeholder = Handlebars.Utils.escapeExpression(hash.placeholder);
return new Handlebars.SafeString(
'<input aria-label="' +
ariaLabel +
'" placeholder="' +
placeholder +
'" />'
);
},
t: function(defaultString) {
return new Handlebars.SafeString(defaultString);
}
})
.toCompileTo('<input aria-label="Name" placeholder="Example User" />');
});
it('multiple subexpressions in a hash with context', function() {
var string =
'{{input aria-label=(t item.field) placeholder=(t item.placeholder)}}';
var context = {
item: {
field: 'Name',
placeholder: 'Example User'
}
};
var helpers = {
input: function(options) {
var hash = options.hash;
var ariaLabel = Handlebars.Utils.escapeExpression(hash['aria-label']);
var placeholder = Handlebars.Utils.escapeExpression(hash.placeholder);
return new Handlebars.SafeString(
'<input aria-label="' +
ariaLabel +
'" placeholder="' +
placeholder +
'" />'
);
},
t: function(defaultString) {
return new Handlebars.SafeString(defaultString);
}
};
shouldCompileTo(
string,
[context, helpers],
'<input aria-label="Name" placeholder="Example User" />'
);
expectTemplate(
'{{input aria-label=(t item.field) placeholder=(t item.placeholder)}}'
)
.withInput({
item: {
field: 'Name',
placeholder: 'Example User'
}
})
.withHelpers({
input: function(options) {
var hash = options.hash;
var ariaLabel = Handlebars.Utils.escapeExpression(hash['aria-label']);
var placeholder = Handlebars.Utils.escapeExpression(hash.placeholder);
return new Handlebars.SafeString(
'<input aria-label="' +
ariaLabel +
'" placeholder="' +
placeholder +
'" />'
);
},
t: function(defaultString) {
return new Handlebars.SafeString(defaultString);
}
})
.toCompileTo('<input aria-label="Name" placeholder="Example User" />');
});
it('in string params mode,', function() {
var template = CompilerContext.compile(
'{{snog (blorg foo x=y) yeah a=b}}',
{ stringParams: true }
);
expectTemplate('{{snog (blorg foo x=y) yeah a=b}}')
.withCompileOptions({ stringParams: true })
.withHelpers({
snog: function(a, b, options) {
equals(a, 'foo');
equals(
options.types.length,
2,
'string params for outer helper processed correctly'
);
equals(
options.types[0],
'SubExpression',
'string params for outer helper processed correctly'
);
equals(
options.types[1],
'PathExpression',
'string params for outer helper processed correctly'
);
return a + b;
},
var helpers = {
snog: function(a, b, options) {
equals(a, 'foo');
equals(
options.types.length,
2,
'string params for outer helper processed correctly'
);
equals(
options.types[0],
'SubExpression',
'string params for outer helper processed correctly'
);
equals(
options.types[1],
'PathExpression',
'string params for outer helper processed correctly'
);
return a + b;
},
blorg: function(a, options) {
equals(
options.types.length,
1,
'string params for inner helper processed correctly'
);
equals(
options.types[0],
'PathExpression',
'string params for inner helper processed correctly'
);
return a;
}
};
var result = template(
{
blorg: function(a, options) {
equals(
options.types.length,
1,
'string params for inner helper processed correctly'
);
equals(
options.types[0],
'PathExpression',
'string params for inner helper processed correctly'
);
return a;
}
})
.withInput({
foo: {},
yeah: {}
},
{ helpers: helpers }
);
equals(result, 'fooyeah');
})
.toCompileTo('fooyeah');
});
it('as hashes in string params mode', function() {
var template = CompilerContext.compile('{{blog fun=(bork)}}', {
stringParams: true
});
var helpers = {
blog: function(options) {
equals(options.hashTypes.fun, 'SubExpression');
return 'val is ' + options.hash.fun;
},
bork: function() {
return 'BORK';
}
};
var result = template({}, { helpers: helpers });
equals(result, 'val is BORK');
expectTemplate('{{blog fun=(bork)}}')
.withCompileOptions({ stringParams: true })
.withHelpers({
blog: function(options) {
equals(options.hashTypes.fun, 'SubExpression');
return 'val is ' + options.hash.fun;
},
bork: function() {
return 'BORK';
}
})
.toCompileTo('val is BORK');
});
it('subexpression functions on the context', function() {
var string = '{{foo (bar)}}!';
var context = {
bar: function() {
return 'LOL';
}
};
var helpers = {
foo: function(val) {
return val + val;
}
};
shouldCompileTo(string, [context, helpers], 'LOLLOL!');
expectTemplate('{{foo (bar)}}!')
.withInput({
bar: function() {
return 'LOL';
}
})
.withHelpers({
foo: function(val) {
return val + val;
}
})
.toCompileTo('LOLLOL!');
});
it("subexpressions can't just be property lookups", function() {
var string = '{{foo (bar)}}!';
var context = {
bar: 'LOL'
};
var helpers = {
foo: function(val) {
return val + val;
}
};
shouldThrow(function() {
shouldCompileTo(string, [context, helpers], 'LOLLOL!');
});
expectTemplate('{{foo (bar)}}!')
.withInput({
bar: 'LOL'
})
.withHelpers({
foo: function(val) {
return val + val;
}
})
.toThrow();
});
});