From 0efb7d7e67befc3cb9b0bd18d92bad417fbd1417 Mon Sep 17 00:00:00 2001 From: Paul Robert Lloyd Date: Sun, 11 Feb 2024 01:14:33 +0000 Subject: [PATCH] Return params if no stored data --- lib/decorate.js | 5 +---- test/lib/decorate.js | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/decorate.js b/lib/decorate.js index a5b5a7a..131db66 100644 --- a/lib/decorate.js +++ b/lib/decorate.js @@ -25,10 +25,7 @@ exports.decorate = function (params, keyPath, componentName) { } // Get stored value from session data, else local data - const storedValue = _.get(data, keyPath) || _.get(this.ctx, keyPath) - if (!storedValue) { - return '' - } + const storedValue = _.get(data, keyPath) || _.get(this.ctx, keyPath) || '' params.id = params.id ? params.id : keyPath.join('-') params.name = params.name diff --git a/test/lib/decorate.js b/test/lib/decorate.js index 4271c84..ec9e033 100644 --- a/test/lib/decorate.js +++ b/test/lib/decorate.js @@ -36,8 +36,11 @@ test('Returns form component without decorate attribute', () => { test('Returns form component without any session data', () => { const result = env.render('input.njk', {}) - assert.match(result, /id=""/) - assert.match(result, /name=""/) + assert.match(result, /Email address/) + assert.match(result, /for="account-email-address"/) + assert.match(result, /id="account-email-address"/) + assert.match(result, /name="\[account\]\[email-address\]"/) + assert.doesNotMatch(result, /value/) }) test('Decorates form component from session data', (t) => { @@ -49,9 +52,20 @@ test('Decorates form component from session data', (t) => { assert.match(result, /value="test@example.org"/) }) +test('Returns form component without any local data', () => { + const result = env.render('input-locals.njk', {}) + + assert.match(result, /Name/) + assert.match(result, /for="name"/) + assert.match(result, /id="name"/) + assert.match(result, /name="\[name\]"/) + assert.doesNotMatch(result, /value/) +}) + test('Decorates form component from local data', (t) => { const result = env.render('input-locals.njk', data) + assert.match(result, /Name/) assert.match(result, /for="name"/) assert.match(result, /id="name"/) assert.match(result, /name="\[name\]"/)