Skip to content

Commit

Permalink
test: extend asset sanitize test with a file that collides after sani…
Browse files Browse the repository at this point in the history
…tation
  • Loading branch information
dominikg committed Aug 18, 2022
1 parent b0e2516 commit 0e1a349
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 22 deletions.
33 changes: 20 additions & 13 deletions playground/assets-sanitize/__tests__/assets-sanitize.spec.ts
@@ -1,20 +1,27 @@
import { expect, test } from 'vitest'
import { getBg, isBuild, listAssets, page } from '~utils'
import { getBg, isBuild, page, readManifest } from '~utils'

if (!isBuild) {
test('importing asset with special char in filename works', async () => {
expect(await getBg('.circle-bg')).toContain('+circle.svg')
expect(await page.textContent('.circle-bg')).toMatch('+circle.svg')
test('importing asset with special char in filename works in dev', async () => {
expect(await getBg('.plus-circle')).toContain('+circle.svg')
expect(await page.textContent('.plus-circle')).toMatch('+circle.svg')
expect(await getBg('.underscore-circle')).toContain('_circle.svg')
expect(await page.textContent('.underscore-circle')).toMatch('_circle.svg')
})
} else {
const expected_asset_name_RE = /_circle\.[\w]+\.svg/
test('importing asset with special char in filename works', async () => {
expect(await getBg('.circle-bg')).toMatch(expected_asset_name_RE)
expect(await page.textContent('.circle-bg')).toMatch(expected_asset_name_RE)
})
test('asset with special char in filename gets sanitized', async () => {
const svgs = listAssets().filter((a) => a.endsWith('.svg'))
expect(svgs[0]).toMatch(expected_asset_name_RE)
expect(svgs.length).toBe(1)
test('importing asset with special char in filename works in build', async () => {
const manifest = readManifest()
const plusCircleAsset = manifest['+circle.svg'].file
const underscoreCircleAsset = manifest['_circle.svg'].file
expect(await getBg('.plus-circle')).toMatch(plusCircleAsset)
expect(await page.textContent('.plus-circle')).toMatch(plusCircleAsset)
expect(await getBg('.underscore-circle')).toMatch(underscoreCircleAsset)
expect(await page.textContent('.underscore-circle')).toMatch(
underscoreCircleAsset
)
expect(plusCircleAsset).toMatch('/_circle')
expect(underscoreCircleAsset).toMatch('/_circle')
expect(plusCircleAsset).not.toBe(underscoreCircleAsset)
expect(Object.keys(manifest).length).toBe(3) // 2 svg, 1 index.js
})
}
3 changes: 3 additions & 0 deletions playground/assets-sanitize/_circle.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 10 additions & 5 deletions playground/assets-sanitize/index.html
@@ -1,6 +1,11 @@
<script type="module" src="./index.js"></script>
<h1>test element below should show a circle and it's url</h1>
<div
class="circle-bg"
style="background-repeat: no-repeat; padding-left: 2rem"
></div>
<style>
.test-el {
background-repeat: no-repeat;
padding-left: 2rem;
margin-bottom: 1rem;
}
</style>
<h1>test elements below should show circles and their url</h1>
<div class="test-el plus-circle"></div>
<div class="test-el underscore-circle"></div>
13 changes: 9 additions & 4 deletions playground/assets-sanitize/index.js
@@ -1,4 +1,9 @@
import svg from './+circle.svg'
const el = document.body.querySelector('.circle-bg')
el.style.backgroundImage = `url(${svg})`
el.textContent = svg
import plusCircle from './+circle.svg'
import underscoreCircle from './_circle.svg'
function setData(classname, file) {
const el = document.body.querySelector(classname)
el.style.backgroundImage = `url(${file})`
el.textContent = file
}
setData('.plus-circle', plusCircle)
setData('.underscore-circle', underscoreCircle)

0 comments on commit 0e1a349

Please sign in to comment.