test: add template abbreviation

This commit is contained in:
三咲智子 Kevin Deng 2024-04-18 01:30:56 +08:00
parent b447aceac5
commit a68445bdac
No known key found for this signature in database
1 changed files with 40 additions and 0 deletions

View File

@ -0,0 +1,40 @@
/**
* @vitest-environment jsdom
*/
const parser = new DOMParser()
function parseHTML(html: string) {
return parser.parseFromString(html, 'text/html').body.innerHTML
}
function checkAbbr(template: string, abbrevation: string, expected: string) {
// TODO do some optimzations to make sure template === abbrevation
expect(parseHTML(abbrevation)).toBe(expected)
}
test('template abbreviation', () => {
checkAbbr('<div>hello</div>', '<div>hello', '<div>hello</div>')
checkAbbr(
'<div><div>hello</div></div>',
'<div><div>hello',
'<div><div>hello</div></div>',
)
checkAbbr(
'<div><span>foo</span><span/></div>',
'<div><span>foo</span><span>',
'<div><span>foo</span><span></span></div>',
)
checkAbbr(
'<div><hr/><div/></div>',
'<div><hr><div>',
'<div><hr><div></div></div>',
)
checkAbbr(
'<div><div/><hr/></div>',
'<div><div></div><hr>',
'<div><div></div><hr></div>',
)
checkAbbr('<span/>hello', '<span></span>hello', '<span></span>hello')
})