-
-
Notifications
You must be signed in to change notification settings - Fork 233
/
extension-manager.spec.tsx
146 lines (122 loc) Β· 3.97 KB
/
extension-manager.spec.tsx
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { EMPTY_PARAGRAPH_NODE, Tags } from '@remirror/core-constants';
import { Cast } from '@remirror/core-helpers';
import { Attrs, EditorState, NodeExtensionSpec } from '@remirror/core-types';
import { PortalContainer } from '@remirror/react-portals';
import { createTestManager, extensions } from '@remirror/test-fixtures';
import { defaultRemirrorThemeValue } from '@remirror/ui';
import { EditorView } from 'prosemirror-view';
import React, { FC } from 'react';
import { Extension } from '../extension';
import { ExtensionManager, isExtensionManager } from '../extension-manager';
import { NodeExtension } from '../node-extension';
export const helpers = {
getState: Cast(jest.fn(() => state)),
portalContainer: new PortalContainer(),
getTheme: () => defaultRemirrorThemeValue,
};
const innerMock = jest.fn();
const mock = jest.fn((_: Attrs) => innerMock);
const getInformation = jest.fn(() => 'information');
const SSRComponent: FC<Attrs> = () => <div />;
class DummyExtension extends Extension {
public name = 'dummy';
public tags = ['simple', Tags.LastNodeCompatible];
public commands() {
return { dummy: mock };
}
public helpers() {
return {
getInformation,
};
}
public attributes() {
return {
class: 'custom',
};
}
}
class BigExtension extends NodeExtension {
public name = 'big' as const;
public schema: NodeExtensionSpec = {
toDOM: () => ['h1', 0],
};
get defaultOptions() {
return {
SSRComponent,
};
}
}
const dummy = new DummyExtension();
const big = new BigExtension();
let manager = ExtensionManager.create([
...extensions,
{ extension: dummy, priority: 1 },
{ extension: big, priority: 10 },
]);
let state: EditorState;
let view: EditorView;
beforeEach(() => {
manager = ExtensionManager.create([
...extensions,
{ extension: dummy, priority: 1 },
{ extension: big, priority: 10 },
]);
manager.init(helpers);
state = manager.createState({ content: EMPTY_PARAGRAPH_NODE });
view = new EditorView(document.createElement('div'), {
state,
editable: () => true,
});
manager.initView(view);
});
test('commands', () => {
const attrs = { a: 'a' };
manager.data.actions.dummy(attrs);
expect(mock).toHaveBeenCalledWith(attrs);
expect(innerMock).toHaveBeenCalledWith(state, view.dispatch, view);
});
test('helpers', () => {
const val = manager.data.helpers.getInformation();
expect(val).toBe('information');
expect(getInformation).toHaveBeenCalled();
});
describe('#properties', () => {
it('should sort extensions by priority', () => {
expect(manager.extensions).toHaveLength(11);
expect(manager.extensions[0]).toEqual(dummy);
expect(manager.extensions[10]).toEqual(big);
});
it('should throw if data accessed without init', () => {
const testManager = createTestManager();
expect(() => testManager.data).toThrowErrorMatchingInlineSnapshot(
`"Extension Manager must be initialized before attempting to access the data"`,
);
});
it('should provide the schema at instantiation', () => {
expect(createTestManager().schema).toBeTruthy();
});
it('should provide access to `attributes`', () => {
expect(manager.attributes.class).toInclude('custom');
});
it('should provide access to `components`', () => {
expect(manager.components.big).toEqual(SSRComponent);
});
});
test('isExtensionManager', () => {
expect(isExtensionManager({})).toBeFalse();
expect(isExtensionManager(manager)).toBeTrue();
});
describe('#isEqual', () => {
it('should be equal for the same instance', () => {
expect(manager.isEqual(manager)).toBeTrue();
});
it('should be equal for different instances but identical otherwise', () => {
expect(createTestManager().isEqual(createTestManager())).toBeTrue();
});
it('should not be equal for different managers', () => {
expect(manager.isEqual(createTestManager())).toBeFalse();
});
it('should not be equal for different objects', () => {
expect(manager.isEqual({})).toBeFalse();
});
});