Skip to content

Commit aed1d4b

Browse files
committed
feature: add 'accessors' option
1 parent 5336903 commit aed1d4b

File tree

2 files changed

+100
-81
lines changed

2 files changed

+100
-81
lines changed

src/__tests__/render.test.js

Lines changed: 99 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,108 @@
1-
import {
2-
act,
3-
render as stlRender
4-
} from '..'
5-
import Comp from './fixtures/Comp'
6-
import CompDefault from './fixtures/Comp.html'
1+
import { act, render as stlRender } from "..";
2+
import Comp from "./fixtures/Comp";
3+
import CompDefault from "./fixtures/Comp.html";
74

8-
describe('render', () => {
9-
let props
5+
describe("render", () => {
6+
let props;
107

11-
const render = () => {
12-
return stlRender(Comp, {
13-
target: document.body,
14-
props
15-
})
16-
}
8+
const render = (additional = {}) => {
9+
return stlRender(Comp, {
10+
target: document.body,
11+
props,
12+
...additional,
13+
});
14+
};
1715

18-
beforeEach(() => {
19-
props = {
20-
name: 'World'
21-
}
22-
})
16+
beforeEach(() => {
17+
props = {
18+
name: "World",
19+
};
20+
});
2321

24-
test('renders component into the document', () => {
25-
const { getByText } = render()
22+
test("renders component into the document", () => {
23+
const { getByText } = render();
2624

27-
expect(getByText('Hello World!')).toBeInTheDocument()
28-
})
25+
expect(getByText("Hello World!")).toBeInTheDocument();
2926

30-
// Dear reader, this is not something you generally want to do in your tests.
31-
test('programmatically change props', async () => {
32-
const { component, getByText } = render()
27+
});
3328

34-
expect(getByText('Hello World!')).toBeInTheDocument()
29+
// Dear reader, this is not something you generally want to do in your tests.
30+
test("programmatically change props", async () => {
31+
const { component, getByText } = render();
32+
33+
expect(getByText("Hello World!")).toBeInTheDocument();
3534

3635
await act(() => {
37-
component.$set({ name: 'Worlds' })
38-
})
39-
40-
expect(getByText('Hello Worlds!')).toBeInTheDocument()
41-
})
42-
43-
test('should accept props directly', () => {
44-
const { getByText } = stlRender(Comp, { name: 'World' })
45-
expect(getByText('Hello World!')).toBeInTheDocument()
46-
})
47-
48-
test('should accept svelte component options', () => {
49-
const target = document.createElement('div')
50-
const div = document.createElement('div')
51-
document.body.appendChild(target)
52-
target.appendChild(div)
53-
const { container } = stlRender(Comp, {
54-
target,
55-
anchor: div,
56-
props: { name: 'World' },
57-
context: new Map([['name', 'context']])
58-
})
59-
expect(container).toMatchSnapshot()
60-
})
61-
62-
test('should throw error when mixing svelte component options and props', () => {
63-
expect(() => {
64-
stlRender(Comp, { anchor: '', name: 'World' })
65-
}).toThrow(/Unknown options were found/)
66-
})
67-
68-
test('should return a container object, which contains the DOM of the rendered component', () => {
69-
const { container } = render()
70-
71-
expect(container.innerHTML).toBe(document.body.innerHTML)
72-
})
73-
74-
test('correctly find component constructor on the default property', () => {
75-
const { getByText } = render(CompDefault, { props: { name: 'World' } })
76-
77-
expect(getByText('Hello World!')).toBeInTheDocument()
78-
})
79-
80-
test("accept the 'context' option", () => {
81-
const { getByText } = stlRender(Comp, {
82-
props: {
83-
name: 'Universe'
84-
},
85-
context: new Map([['name', 'context']])
86-
})
87-
88-
expect(getByText('we have context')).toBeInTheDocument()
89-
})
90-
})
36+
component.$set({ name: "Worlds" });
37+
});
38+
39+
expect(getByText("Hello Worlds!")).toBeInTheDocument();
40+
41+
});
42+
43+
test("change props with accessors", async () => {
44+
const { component, getByText } = render({ accessors: true });
45+
46+
expect(getByText("Hello World!")).toBeInTheDocument();
47+
48+
expect(component.name).toBe("World");
49+
50+
await act(() => {
51+
component.value = "Planet";
52+
});
53+
54+
expect(getByText("Hello World!")).toBeInTheDocument();
55+
56+
});
57+
58+
test("should accept props directly", () => {
59+
const { getByText } = stlRender(Comp, { name: "World" });
60+
expect(getByText("Hello World!")).toBeInTheDocument();
61+
});
62+
63+
test("should accept svelte component options", () => {
64+
const target = document.createElement("div");
65+
const div = document.createElement("div");
66+
document.body.appendChild(target);
67+
target.appendChild(div);
68+
const { container } = stlRender(Comp, {
69+
target,
70+
anchor: div,
71+
props: { name: "World" },
72+
context: new Map([["name", "context"]]),
73+
});
74+
expect(container).toMatchSnapshot();
75+
});
76+
77+
test("should throw error when mixing svelte component options and props", () => {
78+
expect(() => {
79+
stlRender(Comp, { anchor: "", name: "World" });
80+
}).toThrow(/Unknown options were found/);
81+
});
82+
83+
test("should return a container object, which contains the DOM of the rendered component", () => {
84+
const { container } = render();
85+
86+
expect(container.innerHTML).toBe(document.body.innerHTML);
87+
88+
});
89+
90+
test("correctly find component constructor on the default property", () => {
91+
const { getByText } = render(CompDefault, { props: { name: "World" } });
92+
93+
expect(getByText("Hello World!")).toBeInTheDocument();
94+
95+
});
96+
97+
test("accept the 'context' option", () => {
98+
const { getByText } = stlRender(Comp, {
99+
props: {
100+
name: "Universe",
101+
},
102+
context: new Map([["name", "context"]]),
103+
});
104+
105+
expect(getByText("we have context")).toBeInTheDocument();
106+
107+
});
108+
});

src/pure.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const containerCache = new Map()
55
const componentCache = new Set()
66

77
const svelteComponentOptions = [
8+
'accessors',
89
'anchor',
910
'props',
1011
'hydrate',

0 commit comments

Comments
 (0)