Skip to content

Commit 44f1dbd

Browse files
committed
add test for example
1 parent 1e844ee commit 44f1dbd

File tree

4 files changed

+84
-8
lines changed

4 files changed

+84
-8
lines changed

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
"@types/react-dom": "^18.2.17",
1313
"react": "^18.2.0",
1414
"react-dom": "^18.2.0",
15-
"react-scoped-provider": "^1.3.2",
1615
"react-scripts": "5.0.1",
16+
"react-state-view-controller": "latest",
1717
"rxjs": "^7.8.1",
1818
"typescript": "^4.9.5",
1919
"web-vitals": "^2.1.4"

example/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const ControllerConsumer = () => {
3434
}
3535

3636
const ListenerConsumer = () => {
37-
const controller = useListener(MultiCounterController, (state) => console.log(state))
37+
const controller = useListener(MultiCounterController, (state) => {})
3838
console.log('render listner')
3939
return <h2 data-testid='controller-listener'>Listenner mounted</h2>
4040
}

example/tests/App.test.tsx

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,86 @@
1-
import { getByTestId, render } from '@testing-library/react'
1+
import { act, fireEvent, getByTestId, render } from '@testing-library/react'
22
import App from '../src/App'
33

44
test('inject success', () => {
55
const { container } = render(<App />)
6-
const controllerExistElement = getByTestId(container, 'controller-exist-assertion-text')
7-
const text = controllerExistElement.textContent
6+
const controllerConsumer = getByTestId(container, 'controller-consumer')
7+
const text = controllerConsumer.textContent
88
const expected = 'Controller existed!'
99
expect(text).toBe(expected)
1010
})
11+
12+
test('Listener', () => {
13+
const { container } = render(<App />)
14+
const listenerEl = getByTestId(container, 'controller-listener')
15+
const text = listenerEl.textContent
16+
const expected = `Listenner mounted`
17+
expect(text).toBe(expected)
18+
})
19+
20+
test('Counter', () => {
21+
const { container } = render(<App />)
22+
const btnEl = getByTestId(container, 'counter-btn')
23+
24+
let counterTextEl = getByTestId(container, 'counter-text')
25+
let text = counterTextEl.textContent
26+
let expected = `Counter: ${0}`
27+
expect(text).toBe(expected)
28+
29+
act(() => {
30+
fireEvent.click(btnEl)
31+
})
32+
counterTextEl = getByTestId(container, 'counter-text')
33+
text = counterTextEl.textContent
34+
expected = `Counter: ${1}`
35+
expect(text).toBe(expected)
36+
37+
act(() => {
38+
fireEvent.click(btnEl)
39+
})
40+
counterTextEl = getByTestId(container, 'counter-text')
41+
text = counterTextEl.textContent
42+
expected = `Counter: ${2}`
43+
expect(text).toBe(expected)
44+
45+
act(() => {
46+
fireEvent.click(btnEl)
47+
})
48+
counterTextEl = getByTestId(container, 'counter-text')
49+
text = counterTextEl.textContent
50+
expected = `Counter: ${3}`
51+
expect(text).toBe(expected)
52+
})
53+
54+
test('Counter2', () => {
55+
const { container } = render(<App />)
56+
const btnEl = getByTestId(container, 'counter2-btn')
57+
58+
let counterTextEl = getByTestId(container, 'counter2-text')
59+
let text = counterTextEl.textContent
60+
let expected = `Counter2: ${0}`
61+
expect(text).toBe(expected)
62+
63+
act(() => {
64+
fireEvent.click(btnEl)
65+
})
66+
counterTextEl = getByTestId(container, 'counter2-text')
67+
text = counterTextEl.textContent
68+
expected = `Counter2: ${1}`
69+
expect(text).toBe(expected)
70+
71+
act(() => {
72+
fireEvent.click(btnEl)
73+
})
74+
counterTextEl = getByTestId(container, 'counter2-text')
75+
text = counterTextEl.textContent
76+
expected = `Counter2: ${2}`
77+
expect(text).toBe(expected)
78+
79+
act(() => {
80+
fireEvent.click(btnEl)
81+
})
82+
counterTextEl = getByTestId(container, 'counter2-text')
83+
text = counterTextEl.textContent
84+
expected = `Counter2: ${3}`
85+
expect(text).toBe(expected)
86+
})

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5464,9 +5464,9 @@ react-is@^18.0.0:
54645464
integrity sha512-xWGDIW6x921xtzPkhiULtthJHoJvBbF3q26fzloPCK0hsvxtPVelvftw3zjbHWSkR2km9Z+4uxbDDK/6Zw9B8w==
54655465

54665466
react-scoped-provider@latest:
5467-
version "1.3.2"
5468-
resolved "https://registry.yarnpkg.com/react-scoped-provider/-/react-scoped-provider-1.3.2.tgz#5ae31e5d7328840f7c658f9a475c0882db36c122"
5469-
integrity sha512-lbIYppTWd+C8mKpWms6A4xiw/UZtUQ6k2qv7Qzt0Q63ydTofzf3siAOU+cY64db3MysOZfdvLtMuDyR+rS1KUQ==
5467+
version "1.3.3"
5468+
resolved "https://registry.yarnpkg.com/react-scoped-provider/-/react-scoped-provider-1.3.3.tgz#2b4a60ffafdf0bf65d46952f1d450ab15a9aa072"
5469+
integrity sha512-0tI+F6kvZBE8wEY/0ScWDwvqqUUAD051yHjGQsskWxEiTXUf+mTSbTmjLR28BbBEi5tf1ZoEaD/IfvF4xVpDGw==
54705470

54715471
react@^18.2.0:
54725472
version "18.2.0"

0 commit comments

Comments
 (0)