Skip to content

Commit a8dbf45

Browse files
pngwnbenmonro
pngwn
authored andcommitted
fix: support for components exported with .default. (#34)
1 parent fb20651 commit a8dbf45

File tree

6 files changed

+41
-3
lines changed

6 files changed

+41
-3
lines changed

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const config = Object.assign(jestConfig, {
66
transform: {
77
...jestConfig.transform,
88
'^.+\\.svelte$': 'jest-transform-svelte',
9+
'^.+\\.html$': 'svelte-test/transform',
910
},
1011
transformIgnorePatterns: [
1112
...jestConfig.transformIgnorePatterns,

package-lock.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"rollup-plugin-svelte": "^5.0.3",
2424
"rollup-plugin-terser": "^4.0.4",
2525
"sirv-cli": "^0.4.0",
26-
"svelte": "^3.0.0"
26+
"svelte": "^3.0.0",
27+
"svelte-test": "^0.3.0"
2728
},
2829
"peerDependencies": {
2930
"svelte": "3.x"
@@ -58,4 +59,4 @@
5859
"dist",
5960
"public"
6061
]
61-
}
62+
}

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export const render = (Component, {target, ...options} = {}) => {
77
target = document.body.appendChild(document.createElement('div'))
88
}
99

10-
const component = new Component({
10+
const ComponentConstructor = Component.default || Component
11+
const component = new ComponentConstructor({
1112
...options,
1213
target,
1314
})

tests/example/App.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<script>
2+
export let name;
3+
4+
let buttonText = "Button Text";
5+
6+
function handleClick() {
7+
buttonText = "Button Clicked";
8+
}
9+
</script>
10+
11+
<style>
12+
h1 {
13+
color: purple;
14+
}
15+
</style>
16+
17+
<h1>Hello {name}!</h1>
18+
19+
<button on:click={handleClick}>{buttonText}</button>

tests/render.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
prettyDOM,
88
} from '../src'
99
import App from './example/App.svelte'
10+
import App2 from './example/App.html'
1011
import 'jest-dom/extend-expect'
1112

1213
afterEach(cleanup)
@@ -82,4 +83,10 @@ describe('render', () => {
8283

8384
cleanup()
8485
})
86+
87+
test('correctly find component constructor on the default property', () => {
88+
const {getByText} = render(App2, {props: {name: 'world'}})
89+
90+
expect(getByText('Hello world!')).toBeInTheDocument()
91+
})
8592
})

0 commit comments

Comments
 (0)