summaryrefslogtreecommitdiff
path: root/spec/frontend/editor/source_editor_extension_spec.js
blob: 78453aaa491c4f25672d82f4008f4efb7b504118 (plain)
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
61
62
63
64
65
66
import EditorExtension from '~/editor/source_editor_extension';
import { EDITOR_EXTENSION_DEFINITION_ERROR } from '~/editor/constants';
import * as helpers from './helpers';

describe('Editor Extension', () => {
  const dummyObj = { foo: 'bar' };

  it.each`
    definition   | setupOptions
    ${undefined} | ${undefined}
    ${undefined} | ${{}}
    ${undefined} | ${dummyObj}
    ${{}}        | ${dummyObj}
    ${dummyObj}  | ${dummyObj}
  `(
    'throws when definition = $definition and setupOptions = $setupOptions',
    ({ definition, setupOptions }) => {
      const constructExtension = () => new EditorExtension({ definition, setupOptions });
      expect(constructExtension).toThrowError(EDITOR_EXTENSION_DEFINITION_ERROR);
    },
  );

  it.each`
    definition                  | setupOptions | expectedName
    ${helpers.SEClassExtension} | ${undefined} | ${'SEClassExtension'}
    ${helpers.SEClassExtension} | ${{}}        | ${'SEClassExtension'}
    ${helpers.SEClassExtension} | ${dummyObj}  | ${'SEClassExtension'}
    ${helpers.SEFnExtension}    | ${undefined} | ${'SEFnExtension'}
    ${helpers.SEFnExtension}    | ${{}}        | ${'SEFnExtension'}
    ${helpers.SEFnExtension}    | ${dummyObj}  | ${'SEFnExtension'}
    ${helpers.SEConstExt}       | ${undefined} | ${'SEConstExt'}
    ${helpers.SEConstExt}       | ${{}}        | ${'SEConstExt'}
    ${helpers.SEConstExt}       | ${dummyObj}  | ${'SEConstExt'}
  `(
    'correctly creates extension for definition = $definition and setupOptions = $setupOptions',
    ({ definition, setupOptions, expectedName }) => {
      const extension = new EditorExtension({ definition, setupOptions });
      // eslint-disable-next-line new-cap
      const constructedDefinition = new definition();

      expect(extension).toEqual(
        expect.objectContaining({
          extensionName: expectedName,
          setupOptions,
        }),
      );
      expect(extension.obj.constructor.prototype).toBe(constructedDefinition.constructor.prototype);
    },
  );

  describe('api', () => {
    it.each`
      definition                  | expectedKeys
      ${helpers.SEClassExtension} | ${['shared', 'classExtMethod']}
      ${helpers.SEFnExtension}    | ${['fnExtMethod']}
      ${helpers.SEConstExt}       | ${['constExtMethod']}
      ${helpers.SEExtWithoutAPI}  | ${[]}
    `('correctly returns API for $definition', ({ definition, expectedKeys }) => {
      const extension = new EditorExtension({ definition });
      const expectedApi = Object.fromEntries(
        expectedKeys.map((key) => [key, expect.any(Function)]),
      );
      expect(extension.api).toEqual(expect.objectContaining(expectedApi));
    });
  });
});