Skip to content

Commit

Permalink
✅ Add a prepareRenderVariables test
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Jan 29, 2024
1 parent 4049c13 commit fd33e35
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
28 changes: 28 additions & 0 deletions test/05-custom_elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,34 @@ describe('CustomElement', function() {
});
});

describe('#prepareRenderVariables()', () => {
it('should be called before rendering the template', (done) => {

let code = `
<with-prepared-variables></with-prepared-variables>
`;

var compiled = hawkejs.compile('template_test_with_prepared_variables', code);

hawkejs.render(compiled, {}, function rendered(err, res) {

if (err) {
throw err;
}

res = res.trim();

try {
assertEqualHtml(res, '<with-prepared-variables he-rendered="1"><table class="main-table"> <thead> <tr> <th>1</th> <th>2</th> <th>3</th> </tr> </thead> </table></with-prepared-variables>');
} catch (err) {
return done(err);
}
done();
});

});
});

describe('#rendered()', () => {
it('should call back when the element has been rendered', (done) => {

Expand Down
1 change: 1 addition & 0 deletions test/helpers/_load.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ module.exports = function loadHawkejs(hawkejs) {
hawkejs.load(test_base + '/helpers/nested_template_elements.js');
hawkejs.load(test_base + '/helpers/render_looper.js');
hawkejs.load(test_base + '/helpers/print_attribute.js');
hawkejs.load(test_base + '/helpers/with_prepared_variables.js');
}
15 changes: 15 additions & 0 deletions test/helpers/with_prepared_variables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const WithPreparedVariables = Fn.inherits('Hawkejs.Element', 'WithPreparedVariables');

WithPreparedVariables.setTemplateFile('elements/with_prepared_variables');
WithPreparedVariables.setAttribute('date');

WithPreparedVariables.setMethod(async function prepareRenderVariables() {

await Blast.Classes.Pledge.after(5);

let result = [1, 2, 3];

return {
numbers: result
};
});
9 changes: 9 additions & 0 deletions test/templates/elements/with_prepared_variables.hwk
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<table class="main-table">
<thead>
<tr>
{% each numbers as nr %}
<th>{{ nr }}</th>
{% /each %}
</tr>
</thead>
</table>

0 comments on commit fd33e35

Please sign in to comment.