From 2eca6e67e104bb5cb4262e58072e2b76cc2bf943 Mon Sep 17 00:00:00 2001 From: Peter Bacon Darwin Date: Wed, 10 May 2017 14:44:43 +0100 Subject: [PATCH] test(aio): add tests for renderAttributes helper --- aio/tools/transforms/helpers/utils.js | 5 ++++- aio/tools/transforms/helpers/utils.spec.js | 25 +++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/aio/tools/transforms/helpers/utils.js b/aio/tools/transforms/helpers/utils.js index 9b05ab05b9..945a210eed 100644 --- a/aio/tools/transforms/helpers/utils.js +++ b/aio/tools/transforms/helpers/utils.js @@ -92,6 +92,9 @@ module.exports = { }, renderAttributes(attrMap) { - return Object.keys(attrMap).map(key => (attrMap[key] === true) ? ` ${key}` : ` ${key}="${attrMap[key].replace(/"/g, '"')}"`).join(''); + return Object.keys(attrMap).map(key => + attrMap[key] === false ? '' : + attrMap[key] === true ? ` ${key}` : + ` ${key}="${attrMap[key].replace(/"/g, '"')}"`).join(''); } }; diff --git a/aio/tools/transforms/helpers/utils.spec.js b/aio/tools/transforms/helpers/utils.spec.js index ce0369b7b7..952336e92d 100644 --- a/aio/tools/transforms/helpers/utils.spec.js +++ b/aio/tools/transforms/helpers/utils.spec.js @@ -1,4 +1,4 @@ -const { mapObject, parseAttributes } = require('./utils'); +const { mapObject, parseAttributes, renderAttributes } = require('./utils'); describe('utils', () => { describe('mapObject', () => { @@ -73,4 +73,27 @@ describe('utils', () => { ); }); }); + + describe('renderAttributes', () => { + it('should convert key-value map to a strong that can be used in HTML', () => { + expect(renderAttributes({ foo: 'bar', moo: 'car' })).toEqual(' foo="bar" moo="car"'); + }); + + it('should handle boolean values', () => { + expect(renderAttributes({ foo: 'bar', loo: true, moo: false })) + .toEqual(' foo="bar" loo'); + }); + + it('should escape double quotes inside the value', () => { + expect(renderAttributes({ foo: 'bar "car"' })).toEqual(' foo="bar "car""'); + }); + + it('should not escape single quotes inside the value', () => { + expect(renderAttributes({ foo: 'bar \'car\'' })).toEqual(' foo="bar \'car\'"'); + }); + + it('should handle an empty object', () => { + expect(renderAttributes({ })).toEqual(''); + }); + }); });