-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add no-scope api to ScopeProvider
- Loading branch information
David Maskasky
committed
May 23, 2024
1 parent
2954118
commit 1563d17
Showing
3 changed files
with
91 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { render } from '@testing-library/react'; | ||
import { atom, useAtomValue } from 'jotai'; | ||
import { getTextContents } from '../utils'; | ||
import { ScopeProvider } from '../../src/index'; | ||
|
||
let i = 1; | ||
const AtomA = atom(() => i++); | ||
const AtomB = atom((get) => get(AtomA)); | ||
|
||
const Child = ({ level }: { level?: string }) => { | ||
const valueA = useAtomValue(AtomA); | ||
const valueB = useAtomValue(AtomB); | ||
return ( | ||
<div className={level}> | ||
Atom A is not scoped so its value should always be 1 | ||
<div className="valueA">{valueA}</div> | ||
Atom B is scoped, so its will use the implicitly scoped Atom A | ||
<div className="valueB">{valueB}</div> | ||
</div> | ||
); | ||
}; | ||
|
||
/* | ||
AtomA | ||
S0[]: AtomA0 | ||
S1[AtomA!]: AtomA! | ||
S2[]: AtomA! | ||
*/ | ||
const App = () => { | ||
return ( | ||
<div className="App"> | ||
<Child level="level0" /> | ||
<ScopeProvider atoms={[AtomB]} noScope={[AtomA]} debugName="level1"> | ||
<Child level="level1" /> | ||
<ScopeProvider atoms={[]} debugName="level2"> | ||
<Child level="level2" /> | ||
</ScopeProvider> | ||
</ScopeProvider> | ||
</div> | ||
); | ||
}; | ||
|
||
const { container } = render(<App />); | ||
|
||
describe('No Scope', () => { | ||
test('AtomA is not scoped so its value should always be 1', () => { | ||
const selectors = [ | ||
'.level0 .valueA', | ||
'.level0 .valueB', | ||
'.level1 .valueA', | ||
'.level1 .valueB', | ||
'.level2 .valueA', | ||
'.level2 .valueB', | ||
]; | ||
|
||
expect(getTextContents(container, selectors)).toEqual([ | ||
'1', // level0 valueA | ||
'1', // level0 valueB | ||
'1', // level1 valueA | ||
'2', // level1 valueB | ||
'1', // level1 valueA | ||
'2', // level1 valueB | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters