Skip to content

Commit 7c7b37f

Browse files
committed
test(overmind-svelte): add tests
1 parent a949eb0 commit 7c7b37f

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<script>
2+
3+
export let store
4+
5+
$: count = $store.state.count
6+
let doubled = undefined
7+
store.reaction(
8+
(state) => state.count,
9+
(value) => {
10+
doubled = value * 2
11+
},
12+
{
13+
immediate: true
14+
}
15+
)
16+
17+
</script>
18+
19+
<p>Count: {count}</p>
20+
<p>Doubled: {doubled}</p>
21+
<button id="increase" on:click={() => store.actions.increase()}>Increase</button>
22+
<button id="decrease" on:click={() => store.actions.decrease()}>Increase</button>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
2+
import '@testing-library/jest-dom/extend-expect'
3+
import { render, fireEvent } from '@testing-library/svelte'
4+
5+
import { Overmind } from 'overmind'
6+
7+
import { createMixin } from './'
8+
import CountTest from './CountTest.svelte'
9+
10+
11+
const app = {
12+
state: {
13+
count: 0
14+
},
15+
actions: {
16+
increase({ state }) {
17+
state.count++;
18+
},
19+
decrease({ state }) {
20+
state.count--;
21+
}
22+
}
23+
}
24+
25+
describe('Svelte', () => {
26+
test('should create mixin', () => {
27+
const overmind = new Overmind(app)
28+
const mixin = createMixin(overmind)
29+
30+
expect('state' in mixin)
31+
})
32+
33+
test('should expose subscribe', () => {
34+
const overmind = new Overmind(app)
35+
const mixin = createMixin(overmind)
36+
37+
expect(typeof mixin.subscribe === 'function')
38+
})
39+
40+
test('should display current state', () => {
41+
const overmind = new Overmind(app)
42+
const mixin = createMixin(overmind)
43+
44+
const { getByText, getAllByRole } = render(CountTest, { store: mixin })
45+
expect(getByText('Count: 0')).toBeInTheDocument()
46+
})
47+
48+
test('should update state by button click', async () => {
49+
const overmind = new Overmind(app)
50+
const mixin = createMixin(overmind)
51+
52+
const { getAllByRole } = render(CountTest, { store: mixin })
53+
const [increaseBtn, ] = getAllByRole('button')
54+
55+
await fireEvent.click(increaseBtn)
56+
await fireEvent.click(increaseBtn)
57+
58+
expect(mixin.state.count === 2)
59+
})
60+
61+
test('should update view on state change', async () => {
62+
const overmind = new Overmind(app)
63+
const mixin = createMixin(overmind)
64+
65+
const { getByText, getAllByRole } = render(CountTest, { store: mixin })
66+
const [increaseBtn, decreaseBtn] = getAllByRole('button')
67+
68+
await fireEvent.click(increaseBtn)
69+
70+
expect(getByText('Count: 1')).toBeInTheDocument()
71+
})
72+
73+
test('should update view on reaction', async () => {
74+
const overmind = new Overmind(app)
75+
const mixin = createMixin(overmind)
76+
77+
const { getByText, getAllByRole } = render(CountTest, { store: mixin })
78+
const [increaseBtn, ] = getAllByRole('button')
79+
80+
expect(getByText('Doubled: 0')).toBeInTheDocument()
81+
82+
await fireEvent.click(increaseBtn)
83+
await fireEvent.click(increaseBtn)
84+
85+
expect(getByText('Doubled: 4')).toBeInTheDocument()
86+
})
87+
})

0 commit comments

Comments
 (0)