File size: 1,711 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* eslint-disable import/no-deprecated */
import React from 'react'
import { Transforms, createEditor } from 'slate'
import { render, act } from '@testing-library/react'
import '@testing-library/jest-dom'
import { Slate, withReact, Editable, useSlateWithV } from '../src'

describe('useSlateWithV', () => {
  const ShowVersion = () => {
    const { v } = useSlateWithV()
    return <>V = {v}</>
  }

  it('tracks a global `v` counter for the editor', async () => {
    const editor = withReact(createEditor())
    const initialValue = [{ type: 'block', children: [{ text: 'test' }] }]

    const { getByText, rerender } = render(
      <Slate editor={editor} initialValue={initialValue}>
        <Editable />
        <p>
          First: <ShowVersion key={1} />
        </p>
        <p>
          Second: <ShowVersion key={2} />
        </p>
      </Slate>
    )

    expect(getByText('First: V = 0')).toBeInTheDocument()
    expect(getByText('Second: V = 0')).toBeInTheDocument()

    await act(async () => {
      Transforms.insertText(editor, '!', { at: { path: [0, 0], offset: 4 } })
    })

    expect(getByText('First: V = 1')).toBeInTheDocument()
    expect(getByText('Second: V = 1')).toBeInTheDocument()

    rerender(
      <Slate editor={editor} initialValue={initialValue}>
        <Editable />
        <p>
          First: <ShowVersion key={1} />
        </p>
        <p>
          Second: <ShowVersion key={2} />
        </p>
        <p>
          Third: <ShowVersion key={3} />
        </p>
      </Slate>
    )

    expect(getByText('First: V = 1')).toBeInTheDocument()
    expect(getByText('Second: V = 1')).toBeInTheDocument()
    expect(getByText('Third: V = 1')).toBeInTheDocument()
  })
})