2.1.5 • Published 5 months ago

jest-remirror v2.1.5

Weekly downloads
551
License
MIT
Repository
github
Last release
5 months ago

npm Dependencies (path) NPM GitHub issues by-label GitHub pull requests by-label

Installation

Ensure the following is installed as it will be responsible for creating the test editor

yarn add @testing-library/react
yarn add jest-remirror

Getting started

Quick setup

For a quick and simple setup add the following to your jest.config.js file.

/* jest.config.js */

module.exports = {
  //...
  setupFilesAfterEnv: ['jest-remirror/environment'],
  testEnvironment: 'jsdom', // Required for dom manipulation
};

This will automatically

  • inject the required JSDOM polyfills
  • ensure that @testing-library/react cleans up the DOM after each test
  • Add the jest assertions toEqualRemirrorDocument and toMatchRemirrorSnapshot.

If you are using typescript then add this to your tsconfig.json file for type support.

{
  "compilerOptions": {
    "types": ["jest-remirror"]
  }
}

Manual setup

Create a jest.framework.dom.ts file and add the following

/* jest.framework.dom.ts */

import { jsdomExtras, jsdomPolyfill, remirrorMatchers } from 'jest-remirror';

/* Auto cleanup DOM after each test */
require('@testing-library/react/cleanup-after-each');

/* Add jest-remirror assertions */
expect.extend(remirrorMatchers);

/* Polyfills for jsdom */
jsdomPolyfill();

/* Extras for prosemirror testing */
jsdomExtras();

In your jest.config.js file add this to the configuration

/* jest.config.js */

module.exports = {
  //...
  setupFilesAfterEnv: ['<rootDir>/jest.framework.dom.ts'],
  testEnvironment: 'jsdom', // Required for dom manipulation
};

The problem

Testing contenteditable is really difficult, especially with jsdom. There are certain events that can't be fired and it's often hard to conceptualize how the test result translates to the actual user experience.

A solution

jest-remirror makes rendering the remirror editor painless so that you can test that your extensions are:

  • having the intended effect on the HTML output
  • calling the correct callbacks

Under the hood jest-remirror leans heavily on @testing-library/react to render an instance of your test editor to the dom and provide a number of utilities exposed when calling the renderEditor method.

Example

import { renderEditor } from 'jest-remirror';
import { Mention, MentionOptions } from '@remirror/extension-mention';

/**
 * A utility method to help make writing the tests easier. It runs renderEditor to inject the editor into the dom and pass along any params.
 */
const create = (params: MentionOptions<'mentionAt'> = { name: 'mentionAt' }) =>
  renderEditor({
    attrNodes: [new Mention({ name: 'mentionAt', mentionClassName: 'custom', ...params })],
  });

describe('Mention#command', () => {
  let {
    nodes: { doc, paragraph },
    view,
    attrNodes: { mentionAt },
    actions,
    add,
  } = create();

  beforeEach(() => {
    ({
      nodes: { doc, paragraph },
      view,
      attrNodes: { mentionAt },
      actions,
      add,
    } = create());
  });

  it('replaces text at the current position', () => {
    const { actions } = add(doc(paragraph('This is ', '<cursor>')));
    const atMention = mentionAt({ id: 'test', label: '@test' });

    // Run the command the command
    actions.createMention(attrs);

    expect(view.state).toContainRemirrorDocument(paragraph('This is an ', atMention()));
  });
});

describe('plugin', () => {
  const options = {
    name: 'mentionAt' as 'mentionAt',
    mentionClassName: 'custom',
  };

  const mocks = {
    onEnter: jest.fn(),
    onChange: jest.fn(),
    onKeyDown: jest.fn(),
    onExit: jest.fn(),
  };

  it('uses default noop callbacks', () => {
    const id = 'mention';
    const label = `@${id}`;
    const {
      add,
      nodes: { doc, paragraph: p },
      view,
    } = renderEditor({
      attrNodes: [new Mention(options)],
    });

    add(doc(p('<cursor>'))).insertText(`This ${label} `);
    expect(view.state).toContainRemirrorDocument(p(`This ${label} `));
  });

  it('injects the mention at the correct place', () => {
    const id = 'mention';
    const label = `@${id}`;
    const {
      add,
      nodes: { doc, paragraph: p },
      attrNodes: { mentionAt },
      view,
    } = renderEditor(
      {
        attrNodes: [
          new Mention({
            ...options,
            ...mocks,
            onExit: ({ command, query }) => {
              command({ id: query!, label: `@${query}`, appendText: '' });
            },
          }),
        ],
      },
      {},
    );

    const mentionNode = mentionAt({ id, label });

    add(doc(p('<cursor>'))).insertText(`This ${label} `);
    expect(view.state).toContainRemirrorDocument(p('This ', mentionNode(), ' '));
    expect(mocks.onEnter).toHaveBeenCalledTimes(1);
    expect(mocks.onChange).toHaveBeenCalledTimes(id.length - 1);
    expect(mocks.onKeyDown).toHaveBeenCalledTimes(id.length);
  });
});

Acknowledgements

This package borrows very heavily from @atlaskit/editor-test-helpers

0.0.0-pr2222.1

5 months ago

0.0.0-pr2223.1

5 months ago

0.0.0-pr2169.1

6 months ago

0.0.0-pr2169.2

6 months ago

3.0.0-beta.1

6 months ago

3.0.0-beta.3

6 months ago

3.0.0-beta.2

6 months ago

3.0.0-beta.5

6 months ago

3.0.0-beta.4

6 months ago

3.0.0-beta.0

7 months ago

0.0.0-pr2166.2

7 months ago

0.0.0-pr2166.3

7 months ago

0.0.0-pr2166.1

7 months ago

0.0.0-pr2166.4

7 months ago

0.0.0-pr2118.1

10 months ago

0.0.0-pr2100.1

11 months ago

0.0.0-pr2128.2

10 months ago

0.0.0-pr2128.3

10 months ago

0.0.0-pr2128.4

10 months ago

0.0.0-pr2128.1

10 months ago

2.1.5

1 year ago

2.1.4

1 year ago

2.1.3

1 year ago

0.0.0-pr1948.1

1 year ago

0.0.0-pr1938.1

1 year ago

2.1.2

1 year ago

2.1.1

1 year ago

2.0.10

1 year ago

2.1.0

1 year ago

2.0.7

2 years ago

2.0.9

1 year ago

2.0.8

2 years ago

0.0.0-pr1942.1

1 year ago

0.0.0-pr1966.1

1 year ago

0.0.0-pr1922.1

2 years ago

0.0.0-pr1922.2

2 years ago

0.0.0-pr1887.1

2 years ago

0.0.0-pr1881.1

2 years ago

0.0.0-pr1881.2

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.5

2 years ago

0.0.0-pr1879.1

2 years ago

2.0.4

2 years ago

2.0.6

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

0.0.0-pr1873.1

2 years ago

2.0.0-beta.15

2 years ago

2.0.0-beta.14

2 years ago

2.0.0-beta.19

2 years ago

2.0.0-beta.18

2 years ago

2.0.0-beta.17

2 years ago

2.0.0-beta.16

2 years ago

0.0.0-pr1862.1

2 years ago

0.0.0-pr1885.1

2 years ago

0.0.0-pr1713.12

2 years ago

0.0.0-pr1713.11

2 years ago

0.0.0-pr1713.10

2 years ago

2.0.0-beta.9

2 years ago

2.0.0-beta.8

2 years ago

2.0.0-beta.7

2 years ago

2.0.0-beta.2

2 years ago

1.0.44

2 years ago

2.0.0-beta.1

2 years ago

1.0.43

2 years ago

2.0.0-beta.0

2 years ago

1.0.42

2 years ago

2.0.0-beta.6

2 years ago

1.0.48

2 years ago

2.0.0-beta.5

2 years ago

1.0.47

2 years ago

2.0.0-beta.4

2 years ago

1.0.46

2 years ago

2.0.0-beta.3

2 years ago

1.0.45

2 years ago

0.0.0-pr1713.2

2 years ago

1.0.49

2 years ago

0.0.0-pr1713.1

2 years ago

0.0.0-pr1713.8

2 years ago

0.0.0-pr1713.7

2 years ago

0.0.0-pr1713.9

2 years ago

0.0.0-pr1713.4

2 years ago

0.0.0-pr1713.3

2 years ago

0.0.0-pr1713.6

2 years ago

0.0.0-pr1713.5

2 years ago

0.0.0-pr1654.1

2 years ago

2.0.0-beta.11

2 years ago

2.0.0-beta.10

2 years ago

2.0.0-beta.13

2 years ago

2.0.0-beta.12

2 years ago

0.0.0-pr1801.2

2 years ago

0.0.0-pr1801.1

2 years ago

0.0.0-pr1801.3

2 years ago

0.0.0-pr1629.1

2 years ago

0.0.0-pr1608.1

2 years ago

1.0.39

2 years ago

1.0.38

2 years ago

1.0.40

2 years ago

1.0.41

2 years ago

0.0.0-pr1581.2

2 years ago

0.0.0-pr1581.3

2 years ago

0.0.0-pr1581.1

2 years ago

1.0.37

2 years ago

0.0.0-pr1558.1

2 years ago

0.0.0-pr1586.1

2 years ago

0.0.0-pr1552.3

2 years ago

0.0.0-pr1552.2

2 years ago

0.0.0-pr1552.1

2 years ago

0.0.0-pr1549.1

2 years ago

1.0.33

2 years ago

1.0.32

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.36

2 years ago

1.0.35

2 years ago

1.0.34

2 years ago

0.0.0-pr1545.1

2 years ago

0.0.0-pr1532.1

2 years ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

0.0.0-pr1496.1

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

0.0.0-pr1452.1

2 years ago

0.0.0-pr1463.1

2 years ago

0.0.0-pr1367.1

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

0.0.0-pr1392.1

2 years ago

0.0.0-pr1365.1

2 years ago

1.0.15

2 years ago

1.0.14

3 years ago

1.0.13

3 years ago

0.0.0-pr1344.1

3 years ago

0.0.0-pr1340.1

3 years ago

1.0.11

3 years ago

1.0.12

3 years ago

0.0.0-pr1310.1

3 years ago

0.0.0-pr1305.1

3 years ago

1.0.10

3 years ago

0.0.0-pr1242.4

3 years ago

0.0.0-pr1242.2

3 years ago

0.0.0-pr1242.3

3 years ago

0.0.0-pr1242.1

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

0.0.0-pr1174.1

3 years ago

1.0.5

3 years ago

0.0.0-pr1161.2

3 years ago

0.0.0-pr1161.1

3 years ago

0.0.0-pr1158.1

3 years ago

1.0.4

3 years ago

0.0.0-pr1102.1

3 years ago

1.0.3

3 years ago

0.0.0-pr1031.1

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.0.0-pr706.36

3 years ago

0.0.0-pr997.1

3 years ago

0.0.0-pr993.1

3 years ago

0.0.0-pr706.35

3 years ago

0.0.0-pr706.34

3 years ago

0.0.0-pr706.33

3 years ago

0.0.0-pr706.32

3 years ago

0.0.0-pr950.7

3 years ago

0.0.0-pr950.6

3 years ago

0.0.0-pr950.5

3 years ago

0.0.0-pr706.31

3 years ago

0.0.0-pr706.29

3 years ago

0.0.0-pr706.30

3 years ago

0.0.0-pr963.1

3 years ago

0.0.0-pr959.1

3 years ago

0.0.0-pr706.28

3 years ago

0.0.0-pr965.1

3 years ago

0.0.0-pr960.1

3 years ago

0.0.0-pr950.4

3 years ago

0.0.0-pr956.1

3 years ago

0.0.0-pr706.27

3 years ago

0.0.0-pr950.3

3 years ago

0.0.0-pr950.2

3 years ago

0.0.0-pr706.20

3 years ago

0.0.0-pr706.21

3 years ago

0.0.0-pr706.22

3 years ago

0.0.0-pr706.23

3 years ago

0.0.0-pr706.24

3 years ago

0.0.0-pr706.25

3 years ago

0.0.0-pr706.26

3 years ago

0.0.0-pr950.1

3 years ago

0.0.0-pr930.1

3 years ago

0.0.0-pr941.2

3 years ago

0.0.0-pr941.1

3 years ago

0.0.0-pr706.19

3 years ago

0.0.0-pr706.18

3 years ago

0.0.0-pr920.1

3 years ago

0.0.0-pr905.4

3 years ago

0.0.0-pr905.3

3 years ago

0.0.0-pr922.1

3 years ago

0.0.0-pr706.17

3 years ago

0.0.0-pr919.1

3 years ago

0.0.0-pr919.2

3 years ago

0.0.0-pr706.16

3 years ago

0.0.0-pr911.2

3 years ago

0.0.0-pr905.2

3 years ago

0.0.0-pr877.9

3 years ago

0.0.0-pr706.15

3 years ago

0.0.0-pr911.1

3 years ago

0.0.0-pr877.8

3 years ago

0.0.0-pr905.1

3 years ago

0.0.0-pr877.7

3 years ago

0.0.0-pr706.14

3 years ago

0.0.0-pr901.1

3 years ago

0.0.0-pr877.5

3 years ago

0.0.0-pr877.6

3 years ago

0.0.0-pr706.13

3 years ago

0.0.0-pr877.4

3 years ago

0.0.0-pr706.12

3 years ago

0.0.0-pr877.2

3 years ago

0.0.0-pr877.3

3 years ago

0.0.0-pr706.11

3 years ago

0.0.0-pr885.1

3 years ago

0.0.0-pr706.9

3 years ago

0.0.0-pr877.1

3 years ago

0.0.0-pr706.10

3 years ago

0.0.0-pr706.8

3 years ago

0.0.0-pr706.7

3 years ago

0.0.0-pr706.6

3 years ago

0.0.0-pr706.5

3 years ago

0.0.0-pr706.4

3 years ago

0.0.0-pr706.3

3 years ago

0.0.0-pr862.2

3 years ago

0.0.0-pr862.1

3 years ago

0.0.0-pr706.2

3 years ago

0.0.0-pr706.1

3 years ago

1.0.0-pr706

3 years ago

1.0.0-next.60

3 years ago

1.0.0-next.59

3 years ago

1.0.0-next.58

3 years ago

1.0.0-next.57

3 years ago

1.0.0-next.56

3 years ago

1.0.0-next.55

3 years ago

1.0.0-next.54

3 years ago

1.0.0-next.53

3 years ago

1.0.0-next.52

4 years ago

1.0.0-next.51

4 years ago

1.0.0-next.50

4 years ago

1.0.0-next.49

4 years ago

1.0.0-next.48

4 years ago

1.0.0-next.47

4 years ago

1.0.0-next.46

4 years ago

1.0.0-next.44

4 years ago

1.0.0-next.45

4 years ago

1.0.0-next.43

4 years ago

1.0.0-next.42

4 years ago

1.0.0-next.41

4 years ago

1.0.0-next.40

4 years ago

1.0.0-next.39

4 years ago

1.0.0-next.38

4 years ago

1.0.0-next.37

4 years ago

1.0.0-next.36

4 years ago

1.0.0-next.35

4 years ago

1.0.0-next.34

4 years ago

1.0.0-next.33

4 years ago

1.0.0-next.32

4 years ago

1.0.0-next.31

4 years ago

1.0.0-next.29

4 years ago

1.0.0-next.28

4 years ago

1.0.0-next.26

4 years ago

1.0.0-next.25

4 years ago

1.0.0-next.24

4 years ago

1.0.0-next.22

4 years ago

1.0.0-next.21

4 years ago

1.0.0-next.20

4 years ago

1.0.0-next.17

4 years ago

1.0.0-next.16

4 years ago

1.0.0-next.15

4 years ago

1.0.0-next.10

4 years ago

1.0.0-next.6

4 years ago

1.0.0-next.5

4 years ago

1.0.0-next.4

4 years ago

1.0.0-next.0

4 years ago

1.0.0-next.1

4 years ago

0.13.1

4 years ago

0.11.1

4 years ago

0.11.0

4 years ago

0.8.1

4 years ago

0.8.0

4 years ago

0.7.4

4 years ago

0.7.3

4 years ago

0.7.2

4 years ago

0.7.1

4 years ago

0.7.0

4 years ago

0.6.5

4 years ago

0.6.4

5 years ago

0.6.3

5 years ago

0.6.2

5 years ago

0.6.1

5 years ago

0.6.0

5 years ago

0.5.0

5 years ago

0.4.2-canary.2

5 years ago

0.4.2-canary.1

5 years ago

0.4.2-ci.8

5 years ago

0.4.2-canary.0

5 years ago

0.4.0

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago

0.0.1-alpha.11

5 years ago

0.0.1-alpha.10

5 years ago

0.0.1-alpha.9

5 years ago

0.0.1-alpha.7

5 years ago