Skip to content

Commit 52b0011

Browse files
committed
test with example
1 parent abf0967 commit 52b0011

File tree

4 files changed

+66
-49
lines changed

4 files changed

+66
-49
lines changed

example/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
"@types/react-dom": "^18.2.17",
1313
"react": "^18.2.0",
1414
"react-dom": "^18.2.0",
15-
"react-state-view-controller": "latest",
15+
"react-scoped-provider": "^1.3.2",
1616
"react-scripts": "5.0.1",
17+
"rxjs": "^7.8.1",
1718
"typescript": "^4.9.5",
1819
"web-vitals": "^2.1.4"
1920
},

example/src/App.tsx

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,73 @@
11
import { useState } from 'react'
22
import logo from './logo.svg'
33
import './App.css'
4-
import { MultiProvider, Provider, useNamedProvider, useProvider } from 'react-scoped-provider'
4+
import {
5+
Controller,
6+
ControllerProvider,
7+
useBuilder,
8+
useListener,
9+
useProvider,
10+
useSelector,
11+
} from 'react-state-view-controller'
512

6-
class Counter {
13+
type MultiCounterState = {
714
count: number
8-
constructor(count: number) {
9-
this.count = count
15+
count2: number
16+
}
17+
18+
class MultiCounterController extends Controller<MultiCounterState> {
19+
constructor() {
20+
super({ count: 0, count2: 0 })
21+
}
22+
23+
inc() {
24+
this.emit({ count: this.state.count + 1 })
1025
}
26+
inc2() {
27+
this.emit({ count2: this.state.count2 + 1 })
28+
}
29+
}
30+
31+
const ControllerConsumer = () => {
32+
const controller = useProvider(MultiCounterController)
33+
return <h2 data-testid='controller-consumer'>{controller !== undefined ? 'Controller existed!' : ''}</h2>
1134
}
1235

13-
type CustomData = {
14-
field: number
15-
field2: string
36+
const ListenerConsumer = () => {
37+
const controller = useListener(MultiCounterController, (state) => console.log(state))
38+
console.log('render listner')
39+
return <h2 data-testid='controller-listener'>Listenner mounted</h2>
1640
}
1741

18-
const Consumer = () => {
19-
const count = useProvider(Number)
20-
const text = useProvider(String)
21-
const truth = useProvider(Boolean)
22-
const counter = useProvider(Counter)
23-
const customData = useNamedProvider<CustomData>('customData')
42+
const CounterConsumer = () => {
43+
const [state, controller] = useBuilder(MultiCounterController)
44+
console.log('render builder')
2445
return (
2546
<div>
26-
<h2 data-testid='counter-value'>This should update on btn clicked: {count}</h2>
27-
<h2 data-testid='string'>Provided String: {text}</h2>
28-
<h2 data-testid='boolean'>Provided Boolean: {truth ? 'true' : 'false'}</h2>
29-
<h2 data-testid='counter-create'>This should NOT update on btn clicked: {counter.count}</h2>
30-
<h2 data-testid='custom-data'>
31-
Custom data: {customData.field}-{customData.field2}
32-
</h2>
47+
<h2 data-testid='counter-text'>Counter: {state.count}</h2>
48+
<button onClick={() => controller.inc()} data-testid='counter-btn'>
49+
Inc counter
50+
</button>
51+
</div>
52+
)
53+
}
54+
55+
const Counter2Consumer = () => {
56+
const [state, controller] = useSelector(MultiCounterController, (state) => state.count2)
57+
console.log('render-selector')
58+
return (
59+
<div>
60+
<h2 data-testid='counter2-text'>Counter2: {state}</h2>
61+
<button onClick={() => controller.inc2()} data-testid='counter2-btn'>
62+
Inc counter2
63+
</button>
3364
</div>
3465
)
3566
}
3667

3768
function App() {
38-
const [count, setCount] = useState(0)
39-
const customData: CustomData = {
40-
field: 3,
41-
field2: 'hello',
42-
}
4369
return (
44-
<MultiProvider
45-
providers={[
46-
<Provider source={count} />,
47-
<Provider source={'test-string'} />,
48-
<Provider source={true} />,
49-
<Provider source={() => new Counter(count + 1)} />,
50-
<Provider source={customData} name='customData' />,
51-
]}
52-
>
70+
<ControllerProvider source={() => new MultiCounterController()}>
5371
<div className='App'>
5472
<header className='App-header'>
5573
<img src={logo} className='App-logo' alt='logo' />
@@ -59,13 +77,13 @@ function App() {
5977
<a className='App-link' href='https://reactjs.org' target='_blank' rel='noopener noreferrer'>
6078
Learn React
6179
</a>
62-
<button className='App-button' data-testid='btn' onClick={() => setCount((value) => value + 1)}>
63-
Inc
64-
</button>
65-
<Consumer />
80+
<ControllerConsumer />
81+
<ListenerConsumer />
82+
<CounterConsumer />
83+
<Counter2Consumer />
6684
</header>
6785
</div>
68-
</MultiProvider>
86+
</ControllerProvider>
6987
)
7088
}
7189

example/src/index.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ import App from './App'
55
import reportWebVitals from './reportWebVitals'
66

77
const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement)
8-
root.render(
9-
<React.StrictMode>
10-
<App />
11-
</React.StrictMode>,
12-
)
8+
root.render(<App />)
139

1410
// If you want to start measuring performance in your app, pass a function
1511
// to log results (for example: reportWebVitals(console.log))

example/tests/App.test.tsx

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

44
test('inject success', () => {
5-
render(<App />)
6-
7-
expect(true).toBe(true);
5+
const { container } = render(<App />)
6+
const controllerExistElement = getByTestId(container, 'controller-exist-assertion-text')
7+
const text = controllerExistElement.textContent
8+
const expected = 'Controller existed!'
9+
expect(text).toBe(expected)
810
})

0 commit comments

Comments
 (0)