test(aio): add tests for renderAttributes helper

This commit is contained in:
Peter Bacon Darwin 2017-05-10 14:44:43 +01:00 committed by Pete Bacon Darwin
parent c757e5794f
commit 2eca6e67e1
2 changed files with 28 additions and 2 deletions

View File

@ -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('');
}
};

View File

@ -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('');
});
});
});