Skip to content

Commit

Permalink
test: add cyclical class test
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregoor committed Oct 3, 2023
1 parent 13b4093 commit 46c0879
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion tests/class.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { StrictMode, useEffect, useRef } from 'react'
import { fireEvent, render, waitFor } from '@testing-library/react'
import { it } from 'vitest'
import { expect, it } from 'vitest'
import { proxy, useSnapshot } from 'valtio'

it('simple class without methods', async () => {
Expand Down Expand Up @@ -355,3 +355,46 @@ it('no extra re-renders with getters', async () => {
getByText('sum: 2 (2)')
})
})

it('handles cyclical classes', () => {
class Slot {
constructor(
readonly parent: Matryoshka,
public child?: Matryoshka
) {
child?.setParentSlot(this)
}

set(child: Matryoshka) {
this.child = child
child.setParentSlot(this)
}
}

class Matryoshka {
parentSlot?: Slot
childSlot = new Slot(this)

get child() {
return this.childSlot.child
}

constructor(child?: Matryoshka) {
if (child) {
this.childSlot = new Slot(this, child)
}
}

setParentSlot(slot: Slot) {
this.parentSlot = slot
}
}

const store = proxy(new Matryoshka(new Matryoshka()))
// M1(M2)

store.childSlot.set(new Matryoshka(store.child))
// M1(M3(M2)))

expect(store.child?.child).toBeTruthy()
})

0 comments on commit 46c0879

Please sign in to comment.