2023-11-11 17:12:20 +01:00
|
|
|
import { describe, it, expect } from 'vitest'
|
2022-11-16 11:01:31 +01:00
|
|
|
|
|
|
|
import { linkifyAndClean } from '@/utils/inputs'
|
|
|
|
|
|
|
|
describe('linkifyAndClean (clean input remains unchanged)', () => {
|
|
|
|
const testInputs = [
|
|
|
|
'just a text\nfor "test"',
|
|
|
|
'link: <a href="http://www.example.com">example</a>',
|
|
|
|
'link: <a href="http://www.example.com" target="_blank">example</a>',
|
|
|
|
]
|
|
|
|
|
|
|
|
testInputs.map((testInput) => {
|
|
|
|
it(`it returns unmodified input: '${testInput}'`, () => {
|
2023-11-11 17:12:20 +01:00
|
|
|
expect(linkifyAndClean(testInput)).toBe(testInput)
|
2022-11-16 11:01:31 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('linkifyAndClean (URL is linkified)', () => {
|
|
|
|
it('it returns URL as link with target blank', () => {
|
2023-11-11 17:12:20 +01:00
|
|
|
expect(linkifyAndClean('link: http://www.example.com')).toBe(
|
2022-11-16 11:01:31 +01:00
|
|
|
'link: <a href="http://www.example.com" target="_blank">http://www.example.com</a>'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('linkifyAndClean (input sanitization)', () => {
|
|
|
|
const testsParams = [
|
|
|
|
{
|
|
|
|
description: 'it escapes "script" tags',
|
|
|
|
inputString: "<script>alert('evil!')</script>",
|
|
|
|
expectedString: "<script>alert('evil!')</script>",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'it escapes nested tags',
|
|
|
|
inputString: '<p><b>test</b></p>',
|
|
|
|
expectedString: '<p><b>test</b></p>',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'it escapes single tag',
|
|
|
|
inputString: '<p>test',
|
2023-02-25 10:46:44 +01:00
|
|
|
expectedString: '<p>test',
|
2022-11-16 11:01:31 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'it removes css classe',
|
|
|
|
inputString: '<div class="active">test</div>',
|
|
|
|
expectedString: '<div>test</div>',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'it removes style attribute',
|
|
|
|
inputString: '<div style="display:none;">test</div>',
|
|
|
|
expectedString: '<div>test</div>',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'it keeps nested HTML link',
|
|
|
|
inputString: '<p><a href="http://www.example.com">example</a></p>',
|
|
|
|
expectedString:
|
|
|
|
'<p><a href="http://www.example.com">example</a></p>',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
testsParams.map((testParams) => {
|
|
|
|
it(testParams.description, () => {
|
2023-11-11 17:12:20 +01:00
|
|
|
expect(linkifyAndClean(testParams.inputString)).toBe(
|
2022-11-16 11:01:31 +01:00
|
|
|
testParams.expectedString
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|