|
|
import React from 'react' |
|
|
import { createEditor, Transforms } from 'slate' |
|
|
import { act, renderHook } from '@testing-library/react' |
|
|
import { Slate, withReact, Editable, useSlateSelector } from '../src' |
|
|
import _ from 'lodash' |
|
|
|
|
|
describe('useSlateSelector', () => { |
|
|
test('should use equality function when selector changes', async () => { |
|
|
const editor = withReact(createEditor()) |
|
|
const initialValue = [{ type: 'block', children: [{ text: 'test' }] }] |
|
|
|
|
|
const callback1 = jest.fn(() => []) |
|
|
const callback2 = jest.fn(() => []) |
|
|
|
|
|
const { result, rerender } = renderHook( |
|
|
({ callback }) => useSlateSelector(callback, _.isEqual), |
|
|
{ |
|
|
initialProps: { |
|
|
callback: callback1, |
|
|
}, |
|
|
wrapper: ({ children }) => ( |
|
|
<Slate editor={editor} initialValue={initialValue}> |
|
|
<Editable /> |
|
|
{children} |
|
|
</Slate> |
|
|
), |
|
|
} |
|
|
) |
|
|
|
|
|
|
|
|
expect(callback1).toBeCalledTimes(2) |
|
|
|
|
|
const firstResult = result.current |
|
|
|
|
|
await act(async () => { |
|
|
Transforms.insertText(editor, '!', { at: { path: [0, 0], offset: 4 } }) |
|
|
}) |
|
|
|
|
|
|
|
|
expect(callback1).toBeCalledTimes(3) |
|
|
|
|
|
|
|
|
expect(firstResult).toBe(result.current) |
|
|
|
|
|
|
|
|
expect(callback2).toBeCalledTimes(0) |
|
|
|
|
|
|
|
|
rerender({ callback: callback2 }) |
|
|
|
|
|
|
|
|
expect(callback1).toBeCalledTimes(3) |
|
|
|
|
|
|
|
|
expect(callback2).toBeCalledTimes(1) |
|
|
|
|
|
|
|
|
expect(firstResult).toBe(result.current) |
|
|
}) |
|
|
}) |
|
|
|