-
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.
- Loading branch information
David Maskasky
committed
Sep 17, 2024
1 parent
694518e
commit fd3a124
Showing
4 changed files
with
215 additions
and
2 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
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
153 changes: 153 additions & 0 deletions
153
__tests__/derive/understandingAtomState/understandingAtomState.test.ts
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,153 @@ | ||
import { atom, createStore } from '../../../jotai'; | ||
import { assertIsDevStore } from '../../utils'; | ||
|
||
const store = createStore(); | ||
assertIsDevStore(store); | ||
const stateMap = store.dev4_get_internal_weak_map(); | ||
type AtomState = NonNullable<ReturnType<typeof stateMap.get>>; | ||
|
||
const atomA = atom(0); | ||
atomA.debugLabel = 'atomA'; | ||
const atomB = atom((get) => String(get(atomA))); | ||
atomB.debugLabel = 'atomB'; | ||
let atomAState: AtomState; | ||
let atomBState: AtomState; | ||
let unsub: () => void; | ||
|
||
it('sets d when an atom has a dependency', () => { | ||
store.get(atomB); | ||
|
||
atomAState = stateMap.get(atomA)!; | ||
atomBState = stateMap.get(atomB)!; | ||
/* | ||
AtomA state: | ||
{ d: Map(0) {}, p: Set(0) {}, n: 1, v: 0 } | ||
AtomB state: | ||
{ | ||
d: Map(1) { atomA => 1 }, | ||
p: Set(0) {}, | ||
n: 1, | ||
v: '0', | ||
} | ||
*/ | ||
expect(atomBState.d.has(atomA)).toBe(true); | ||
}); | ||
|
||
it('mounts the atoms when an atom has a subscriber', () => { | ||
function onAUnmount() {} | ||
function onAMount() { | ||
return onAUnmount; | ||
} | ||
atomA.onMount = onAMount; | ||
|
||
function subscribeB() {} | ||
unsub = store.sub(atomB, subscribeB); | ||
|
||
/* | ||
AtomA state: | ||
{ | ||
d: Map(0) {}, | ||
p: Set(0) {}, | ||
n: 1, | ||
v: 0, | ||
m: { | ||
l: Set(0) {}, | ||
d: Set(0) {}, | ||
t: Set(1) { [atomB] } | ||
u: [Function onAUnmount] | ||
} | ||
} | ||
AtomB state: | ||
{ | ||
d: Map(1) { atomA => 1 }, | ||
p: Set(0) {}, | ||
n: 1, | ||
v: '0', | ||
m: { | ||
l: Set(1) { [Function: subscribeB] }, | ||
d: Set(1) { [atomA] }, | ||
t: Set(0) {} | ||
} | ||
} | ||
*/ | ||
|
||
expect(atomBState.d.has(atomA)).toBe(true); | ||
expect(atomBState.m!.d.has(atomA)).toBe(true); | ||
expect(atomAState.m!.t.has(atomB)).toBe(true); | ||
expect(atomBState.m!.l.has(subscribeB)).toBe(true); | ||
expect(atomAState.m!.u!).toBe(onAUnmount); | ||
}); | ||
|
||
it('increments the epoch number when an atom is updated', () => { | ||
store.set(atomA, 1); | ||
/* | ||
AtomA state: | ||
{ | ||
d: Map(0) {}, | ||
p: Set(0) {}, | ||
n: 2, | ||
v: 1, | ||
m: { | ||
l: Set(0) {}, | ||
d: Set(0) {}, | ||
t: Set(1) { [atomB] } | ||
u: [Function onAUnmount] | ||
} | ||
} | ||
AtomB state: | ||
{ | ||
d: Map(1) { atomA => 1 }, | ||
p: Set(0) {}, | ||
n: 2, | ||
v: '1', | ||
m: { | ||
l: Set(1) { [Function: subscribeB] }, | ||
d: Set(1) { [atomA] }, | ||
t: Set(0) {} | ||
} | ||
} | ||
*/ | ||
|
||
expect(atomAState.n).toBe(2); | ||
expect(atomBState.n).toBe(2); | ||
}); | ||
|
||
it('unmounts the atoms when there are no subscribers', () => { | ||
unsub(); | ||
/* | ||
AtomA state: | ||
{ d: Map(0) {}, p: Set(0) {}, n: 1, v: 0 } | ||
AtomB state: | ||
{ | ||
d: Map(1) { atomA => 1 }, | ||
p: Set(0) {}, | ||
n: 1, | ||
v: '0', | ||
} | ||
*/ | ||
expect(atomBState.m).toBeUndefined(); | ||
expect(atomAState.m).toBeUndefined(); | ||
}); | ||
|
||
it('does not increment the epoch number when an atom is not mounted', () => { | ||
store.set(atomA, 2); | ||
/* | ||
AtomA state: | ||
{ d: Map(0) {}, p: Set(0) {}, n: 3, v: 2 } | ||
AtomB state: | ||
{ | ||
d: Map(1) { atomA => 1 }, | ||
p: Set(0) {}, | ||
n: 2, | ||
v: '1', | ||
} | ||
*/ | ||
|
||
expect(atomAState.n).toBe(3); | ||
expect(atomBState.n).toBe(2); | ||
}); |
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