这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion test/demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const counterFamily = atomFamily({
export default function App() {
const [count, setCount] = useRecoilState(counterState)
const [count2, setCount2] = useRecoilState(counterState2)
const [count3, setCount3] = useRecoilState(counterFamily('key'))
const [count3, setCount3] = useRecoilState(counterFamily('3'))
const [count4, setCount4] = useRecoilState(counterFamily('4'))
return (
<div className="App">
<h3>Counter 1 (persist): {count}</h3>
Expand All @@ -44,6 +45,9 @@ export default function App() {
<h3>Counter 3 (persist, atomFamily): {count3}</h3>
<button onClick={() => setCount3(count3 + 1)}>Increase</button>
<button onClick={() => setCount3(count3 - 1)}>Decrease</button>
<h3>Counter 4 (persist, atomFamily): {count4}</h3>
<button onClick={() => setCount4(count4 + 1)}>Increase</button>
<button onClick={() => setCount4(count4 - 1)}>Decrease</button>
</div>
)
}
Expand Down
26 changes: 22 additions & 4 deletions test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import { recoilPersist } from '..'
import { render, fireEvent, waitFor } from '@testing-library/react'
import { fireEvent, render, waitFor } from '@testing-library/react'
import * as recoil from 'recoil'

const { updateState, RecoilPersist } = recoilPersist()
Expand All @@ -21,7 +21,7 @@ const counter2State = recoil.atom({
},
})

const counter3State = recoil.atomFamily({
const counterFamily = recoil.atomFamily({
key: 'count3',
default: 0,
persistence_UNSTABLE: {
Expand All @@ -32,14 +32,17 @@ const counter3State = recoil.atomFamily({
function Demo() {
const [count, setCount] = recoil.useRecoilState(counterState)
const [count2, setCount2] = recoil.useRecoilState(counter2State)
const [count3, setCount3] = recoil.useRecoilState(counter3State('key'))
const [count3, setCount3] = recoil.useRecoilState(counterFamily('3'))
const [count4, setCount4] = recoil.useRecoilState(counterFamily('4'))
return (
<div>
<p data-testid="count-value">{count}</p>
<p data-testid="count3-value">{count}</p>
<p data-testid="count3-value">{count3}</p>
<p data-testid="count4-value">{count4}</p>
<button onClick={() => setCount(count + 1)}>Increase</button>
<button onClick={() => setCount2(count2 + 1)}>Increase 2</button>
<button onClick={() => setCount3(count3 + 1)}>Increase 3</button>
<button onClick={() => setCount4(count4 + 1)}>Increase 4</button>
</div>
)
}
Expand Down Expand Up @@ -82,6 +85,21 @@ it('should update localStorage if using atomFamily', async () => {
})
})

it('should cope with atomFamily', async () => {
localStorage.setItem('recoil-persist', JSON.stringify({ count3: 1 }))
const { getByTestId } = render(
<recoil.RecoilRoot initializeState={updateState}>
<RecoilPersist />
<Demo />
</recoil.RecoilRoot>,
)
await waitFor(() => expect(getByTestId('count3-value').innerHTML).toBe('1'))
await waitFor(() => expect(getByTestId('count4-value').innerHTML).toBe('0'))
expect(JSON.parse(localStorage.getItem('recoil-persist'))).toStrictEqual({
count3: 1,
})
})

it('should update sessionStorage', async () => {
const { updateState, RecoilPersist } = recoilPersist([], {
storage: sessionStorage,
Expand Down