Skip to content

Commit cd43dfd

Browse files
committed
cache many components per container
The way the cache was set up, we could only have one render element per test. Which, mind you, is the most common use-case, but it can create surprises. Fixes #205
1 parent 6889d54 commit cd43dfd

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/__tests__/auto-cleanup.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,17 @@ describe('auto-cleanup', () => {
1313
expect(document.body.innerHTML).toEqual('')
1414
})
1515
})
16+
17+
describe('cleanup of two components', () => {
18+
// This just verifies that by importing STL in an
19+
// environment which supports afterEach (like jest)
20+
// we'll get automatic cleanup between tests.
21+
test('first', () => {
22+
render(Comp, { props: { name: 'world' } })
23+
render(Comp, { props: { name: 'universe' } })
24+
})
25+
26+
test('second', () => {
27+
expect(document.body.innerHTML).toEqual('')
28+
})
29+
})

src/pure.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
} from '@testing-library/dom'
66
import { tick } from 'svelte'
77

8-
const containerCache = new Map()
8+
const containerCache = new Set()
99
const componentCache = new Set()
1010

1111
const svelteComponentOptions = [
@@ -59,7 +59,7 @@ const render = (
5959
...checkProps(options),
6060
})
6161

62-
containerCache.set(container, { target, component })
62+
containerCache.add({ container, target, component })
6363
componentCache.add(component)
6464

6565
component.$$.on_destroy.push(() => {
@@ -79,7 +79,7 @@ const render = (
7979
...checkProps(options),
8080
})
8181

82-
containerCache.set(container, { target, component })
82+
containerCache.add({ container, target, component })
8383
componentCache.add(component)
8484

8585
component.$$.on_destroy.push(() => {
@@ -93,16 +93,16 @@ const render = (
9393
}
9494
}
9595

96-
const cleanupAtContainer = (container) => {
97-
const { target, component } = containerCache.get(container)
96+
const cleanupAtContainer = (cached) => {
97+
const { target, component } = cached
9898

9999
if (componentCache.has(component)) component.$destroy()
100100

101101
if (target.parentNode === document.body) {
102102
document.body.removeChild(target)
103103
}
104104

105-
containerCache.delete(container)
105+
containerCache.delete(cached)
106106
}
107107

108108
const cleanup = () => {

0 commit comments

Comments
 (0)