test(aio): add tests for renderAttributes helper
This commit is contained in:
parent
c757e5794f
commit
2eca6e67e1
|
@ -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('');
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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('');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue