From fd33e35d4b7164000312e2ea3ca5c6f9716b5185 Mon Sep 17 00:00:00 2001 From: Jelle De Loecker Date: Mon, 29 Jan 2024 18:36:57 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Add=20a=20`prepareRenderVariables`?= =?UTF-8?q?=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/05-custom_elements.js | 28 +++++++++++++++++++ test/helpers/_load.js | 1 + test/helpers/with_prepared_variables.js | 15 ++++++++++ .../elements/with_prepared_variables.hwk | 9 ++++++ 4 files changed, 53 insertions(+) create mode 100644 test/helpers/with_prepared_variables.js create mode 100644 test/templates/elements/with_prepared_variables.hwk diff --git a/test/05-custom_elements.js b/test/05-custom_elements.js index de4b9ed4..b16489e9 100644 --- a/test/05-custom_elements.js +++ b/test/05-custom_elements.js @@ -833,6 +833,34 @@ describe('CustomElement', function() { }); }); + describe('#prepareRenderVariables()', () => { + it('should be called before rendering the template', (done) => { + + let code = ` + + `; + + 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, '
1 2 3
'); + } catch (err) { + return done(err); + } + done(); + }); + + }); + }); + describe('#rendered()', () => { it('should call back when the element has been rendered', (done) => { diff --git a/test/helpers/_load.js b/test/helpers/_load.js index c2d7e3cc..1a6ad1d4 100644 --- a/test/helpers/_load.js +++ b/test/helpers/_load.js @@ -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'); } \ No newline at end of file diff --git a/test/helpers/with_prepared_variables.js b/test/helpers/with_prepared_variables.js new file mode 100644 index 00000000..73a5946c --- /dev/null +++ b/test/helpers/with_prepared_variables.js @@ -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 + }; +}); \ No newline at end of file diff --git a/test/templates/elements/with_prepared_variables.hwk b/test/templates/elements/with_prepared_variables.hwk new file mode 100644 index 00000000..1a34376e --- /dev/null +++ b/test/templates/elements/with_prepared_variables.hwk @@ -0,0 +1,9 @@ + + + + {% each numbers as nr %} + + {% /each %} + + +
{{ nr }}
\ No newline at end of file