Skip to content

Commit

Permalink
wait for svg before firing onLoad, add transformSrc test
Browse files Browse the repository at this point in the history
  • Loading branch information
robinscholz committed May 13, 2020
1 parent 7bdf066 commit d331626
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
"svg",
"inline"
],
"repository": {
"type": "git",
"url": "https://github.com/robinscholz/svelte-inline-svg"
},
"author": "dev@studioscholz.info",
"license": "MIT",
"devDependencies": {
Expand Down
8 changes: 4 additions & 4 deletions src/inline-svg.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script>
import { onMount, createEventDispatcher } from 'svelte'
import { onMount, createEventDispatcher, tick } from 'svelte'
const dispatch = createEventDispatcher()
Expand Down Expand Up @@ -49,7 +49,7 @@
request.responseText,
'text/xml'
)
let svgEl = result.getElementsByTagName('svg')[0]
let svgEl = result.querySelector('svg')
if (svgEl) {
// Apply transformation
svgEl = transformSrc(svgEl)
Expand Down Expand Up @@ -77,15 +77,14 @@
if (isLoaded) {
isLoaded = false
dispatch('unloaded')
// this.$emit('unloaded');
}
// download
cache[src] = download(src)
}
// inline svg when cached promise resolves
cache[src]
.then((svg) => {
.then(async (svg) => {
// copy attrs
const attrs = svg.attributes
for (let i = attrs.length - 1; i >= 0; i--) {
Expand All @@ -94,6 +93,7 @@
// copy inner html
svgContent = svg.innerHTML
// render svg element
await tick()
isLoaded = true
dispatch('loaded')
})
Expand Down
37 changes: 31 additions & 6 deletions src/test/inline-svg.test.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,52 @@
import { render } from '@testing-library/svelte'
import { render, waitFor } from '@testing-library/svelte'
import InlineSVG from '../inline-svg.svelte'

const src = 'https://raw.githubusercontent.com/robinscholz/svelte-inline-svg/master/src/test/test.svg'
const src =
'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA1MCA1MCI+CiAgPHJlY3Qgd2lkdGg9IjUwIiBoZWlnaHQ9IjUwIi8+Cjwvc3ZnPg=='

describe('inline-svg', () => {
test('render component correctly', () => {
const { container } = render(InlineSVG, {
props: {
src: src
}
src: src,
},
})

expect(container).toContainHTML('<svg')
expect(container).toContainHTML('svg')
})

test('has width set', () => {
const { container } = render(InlineSVG, {
props: {
src: src,
width: 50,
}
},
})
const element = container.querySelector('svg')
expect(element).toHaveAttribute('width')
})

test('transformSrc is working', async () => {
const doc = document
const transform = (svg) => {
let point = doc.createElementNS('http://www.w3.org/2000/svg', 'circle')
point.setAttributeNS(null, 'cx', '20')
point.setAttributeNS(null, 'cy', '20')
point.setAttributeNS(null, 'r', '10')
point.setAttributeNS(null, 'fill', 'red')
svg.appendChild(point)
return svg
}
const { container } = render(InlineSVG, {
props: {
src: src,
transformSrc: transform,
},
})

const element = container.querySelector('svg')
await waitFor(() => {
expect(element).toContainHTML('circle')
})
})
})
6 changes: 3 additions & 3 deletions src/test/test.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d331626

Please sign in to comment.