From 78ccfc0633f489bb9c590ed452275bbdd3422cb8 Mon Sep 17 00:00:00 2001 From: Peter Siska Date: Thu, 5 Oct 2023 13:25:52 +0200 Subject: [PATCH] Add more tests --- test/utils.test.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/test/utils.test.js b/test/utils.test.js index dfe7ddb..c5b6ccf 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -303,3 +303,40 @@ test('replacements with window[] trimming (case insensitive)', async t => { t.deepEqual(replacements, replacementsShould); } }); + +test('can use spaces in default values', async t => { + const defaultValue = 'foo bar'; + let [replaced, replacements] = await replaceVariables(`\${VAR:-${defaultValue}}`); + t.is(replaced, defaultValue); + t.deepEqual(replacements, [{ from: `\${VAR:-${defaultValue}}`, to: defaultValue, count: 1 }]); +}); + +test('can use spaces in default values (prefix)', async t => { + const defaultValue = 'foo bar'; + const [replaced, replacements] = await replaceVariables(`\${PREFIX_VAR:-${defaultValue}}`, {}, 'PREFIX_'); + t.is(replaced, defaultValue); + t.deepEqual(replacements, [{ from: `\${PREFIX_VAR:-${defaultValue}}`, to: defaultValue, count: 1 }]); +}); + +test('can use spaces in default values (window)', async t => { + const defaultValue = 'foo bar'; + const [replaced, replacements] = await replaceVariables(`window["$\{VAR:-${defaultValue}}"]`, {}, '', true); + t.is(replaced, `"${defaultValue}"`); + t.deepEqual(replacements, [{ from: `window["$\{VAR:-${defaultValue}}"]`, to: `"${defaultValue}"`, count: 1 }]); +}); + +test('can replace variable placeholders that have spaces in default values', async t => { + const defaultValue = 'foo bar'; + const variable = 'bar baz'; + const [replaced, replacements] = await replaceVariables(`\${VAR:-${defaultValue}}`, { VAR: variable }); + t.is(replaced, variable); + t.deepEqual(replacements, [{ from: `\${VAR:-${defaultValue}}`, to: variable, count: 1 }]); +}); + +test('can replace variable placeholders that have spaces in default values (prefix)', async t => { + const defaultValue = 'foo bar'; + const variable = 'bar baz'; + const [replaced, replacements] = await replaceVariables(`\${PREFIX_VAR:-${defaultValue}}`, { PREFIX_VAR: variable }, 'PREFIX_'); + t.is(replaced, variable); + t.deepEqual(replacements, [{ from: `\${PREFIX_VAR:-${defaultValue}}`, to: variable, count: 1 }]); +});