Skip to content

Commit

Permalink
lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
KDSBrowne committed Aug 14, 2024
1 parent a61fb53 commit 15b4b1d
Showing 1 changed file with 96 additions and 96 deletions.
192 changes: 96 additions & 96 deletions packages/state/src/lib/react/track.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RenderResult, act, render } from '@testing-library/react'
import { createRef, forwardRef, memo, useEffect, useImperativeHandle } from 'react'
import { ReactTestRenderer, act, create } from 'react-test-renderer'
import { atom } from '../core/Atom'
import { track } from './track'

Expand All @@ -16,40 +16,38 @@ test("tracked components are memo'd", async () => {
)
})

let view: ReactTestRenderer
let view: RenderResult | undefined
await act(() => {
view = create(<Component a="a" b="b" c="c" />)
view = render(<Component a="a" b="b" c="c" />)
})

expect(view!.toJSON()).toMatchInlineSnapshot(`
Array [
"a",
"b",
"c",
]
`)
if (view) {
expect(view.container.textContent).toMatchInlineSnapshot(`
"a
b
c"
`)

expect(numRenders).toBe(1)
expect(numRenders).toBe(1)

await act(() => {
view!.update(<Component a="a" b="b" c="c" />)
})
await act(() => {
view!.rerender(<Component a="a" b="b" c="c" />)
})

expect(numRenders).toBe(1)
expect(numRenders).toBe(1)

await act(() => {
view!.update(<Component a="a" b="b" c="d" />)
})
await act(() => {
view!.rerender(<Component a="a" b="b" c="d" />)
})

expect(numRenders).toBe(2)
expect(numRenders).toBe(2)

expect(view!.toJSON()).toMatchInlineSnapshot(`
Array [
"a",
"b",
"d",
]
`)
expect(view.container.textContent).toMatchInlineSnapshot(`
"a
b
d"
`)
}
})

test("it's fine to call track on components that are already memo'd", async () => {
Expand All @@ -67,40 +65,38 @@ test("it's fine to call track on components that are already memo'd", async () =
})
)

let view: ReactTestRenderer
let view: RenderResult | undefined
await act(() => {
view = create(<Component a="a" b="b" c="c" />)
view = render(<Component a="a" b="b" c="c" />)
})

expect(view!.toJSON()).toMatchInlineSnapshot(`
Array [
"a",
"b",
"c",
]
`)
if (view) {
expect(view.container.textContent).toMatchInlineSnapshot(`
"a
b
c"
`)

expect(numRenders).toBe(1)
expect(numRenders).toBe(1)

await act(() => {
view!.update(<Component a="a" b="b" c="c" />)
})
await act(() => {
view!.rerender(<Component a="a" b="b" c="c" />)
})

expect(numRenders).toBe(1)
expect(numRenders).toBe(1)

await act(() => {
view!.update(<Component a="a" b="b" c="d" />)
})
await act(() => {
view!.rerender(<Component a="a" b="b" c="d" />)
})

expect(numRenders).toBe(2)
expect(numRenders).toBe(2)

expect(view!.toJSON()).toMatchInlineSnapshot(`
Array [
"a",
"b",
"d",
]
`)
expect(view.container.textContent).toMatchInlineSnapshot(`
"a
b
d"
`)
}
})

test('tracked components can use refs', async () => {
Expand All @@ -113,44 +109,48 @@ test('tracked components can use refs', async () => {

const ref = createRef<{ handle: string }>()

let view: ReactTestRenderer
let view: RenderResult | undefined
await act(() => {
view = create(<Component prop="hello" ref={ref} />)
view = render(<Component prop="hello" ref={ref} />)
})

expect(view!.toJSON()).toMatchInlineSnapshot('"output"')
if (view) {
expect(view.container.textContent).toMatchInlineSnapshot('"output"')

expect(ref.current?.handle).toBe('hello')
expect(ref.current?.handle).toBe('hello')

await act(() => {
view.update(<Component prop="world" ref={ref} />)
})
await act(() => {
view!.rerender(<Component prop="world" ref={ref} />)
})

expect(view!.toJSON()).toMatchInlineSnapshot('"output"')
expect(view.container.textContent).toMatchInlineSnapshot('"output"')

expect(ref.current?.handle).toBe('world')
expect(ref.current?.handle).toBe('world')
}
})

test('tracked components update when the state they refernce updates', async () => {
test('tracked components update when the state they reference updates', async () => {
const a = atom('a', 1)

const C = track(function Component() {
return <>{a.get()}</>
})

let view: ReactTestRenderer
let view: RenderResult | undefined

await act(() => {
view = create(<C />)
view = render(<C />)
})

expect(view!.toJSON()).toMatchInlineSnapshot(`"1"`)
if (view) {
expect(view.container.textContent).toMatchInlineSnapshot(`"1"`)

await act(() => {
a.set(2)
})
await act(() => {
a.set(2)
})

expect(view!.toJSON()).toMatchInlineSnapshot(`"2"`)
expect(view.container.textContent).toMatchInlineSnapshot(`"2"`)
}
})

test('things referenced in effects do not trigger updates', async () => {
Expand All @@ -165,21 +165,23 @@ test('things referenced in effects do not trigger updates', async () => {
return <>hi</>
})

let view: ReactTestRenderer
let view: RenderResult | undefined

await act(() => {
view = create(<Component />)
view = render(<Component />)
})

expect(view!.toJSON()).toMatchInlineSnapshot(`"hi"`)
expect(numRenders).toBe(1)
if (view) {
expect(view.container.textContent).toMatchInlineSnapshot(`"hi"`)
expect(numRenders).toBe(1)

await act(() => {
a.set(2)
})
await act(() => {
a.set(2)
})

expect(numRenders).toBe(1)
expect(view!.toJSON()).toMatchInlineSnapshot(`"hi"`)
expect(numRenders).toBe(1)
expect(view.container.textContent).toMatchInlineSnapshot(`"hi"`)
}
})

test("tracked zombie-children don't throw", async () => {
Expand All @@ -200,28 +202,26 @@ test("tracked zombie-children don't throw", async () => {
return <>{value}</>
})

let view: ReactTestRenderer
let view: RenderResult | undefined
await act(() => {
view = create(<Parent />)
view = render(<Parent />)
})

expect(view!.toJSON()).toMatchInlineSnapshot(`
Array [
"1",
"2",
"3",
]
`)
if (view) {
expect(view.container.textContent).toMatchInlineSnapshot(`
"1
2
3"
`)

// remove id 'b' creating a zombie-child
await act(() => {
theAtom?.update(({ b: _, ...rest }) => rest)
})
// remove id 'b' creating a zombie-child
await act(() => {
theAtom?.update(({ b: _, ...rest }) => rest)
})

expect(view!.toJSON()).toMatchInlineSnapshot(`
Array [
"1",
"3",
]
`)
expect(view.container.textContent).toMatchInlineSnapshot(`
"1
3"
`)
}
})

0 comments on commit 15b4b1d

Please sign in to comment.