@neo4j-cypher/react-codemirror 2.0.0-next.1 → 2.0.0-next.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,7 +12,7 @@ test('respects preloaded history', async ({ page, mount }) => {
12
12
  await mount(
13
13
  <CypherEditor
14
14
  value={initialValue}
15
- initialHistory={['first', 'second']}
15
+ history={['first', 'second']}
16
16
  onExecute={() => {
17
17
  /* needed to turn on history movements */
18
18
  }}
@@ -39,23 +39,29 @@ test('can execute queries and see them in history', async ({ page, mount }) => {
39
39
  const initialValue = `MATCH (n)
40
40
  RETURN n;`;
41
41
 
42
- let exectutedQueries = 0;
43
- const onExecute = () => {
44
- exectutedQueries++;
42
+ const history: string[] = [];
43
+ const onExecute = (cmd: string) => {
44
+ history.unshift(cmd);
45
45
  };
46
46
 
47
- await mount(<CypherEditor value={initialValue} onExecute={onExecute} />);
47
+ const editor = await mount(
48
+ <CypherEditor
49
+ value={initialValue}
50
+ history={history}
51
+ onExecute={onExecute}
52
+ />,
53
+ );
48
54
 
49
55
  // Execute initial query
50
56
  await editorPage.getEditor().press('Control+Enter');
51
57
  await editorPage.getEditor().press('Meta+Enter');
52
- expect(exectutedQueries).toBe(1);
58
+ expect(history.length).toBe(1);
53
59
 
54
60
  // Ensure query execution doesn't fire if the query is only whitespace
55
61
  await editorPage.getEditor().fill(' ');
56
62
  await editorPage.getEditor().press('Control+Enter');
57
63
  await editorPage.getEditor().press('Meta+Enter');
58
- expect(exectutedQueries).toBe(1);
64
+ expect(history.length).toBe(1);
59
65
 
60
66
  // Ensure only enter doesn't execute query
61
67
  await editorPage.getEditor().fill('multiline');
@@ -64,15 +70,25 @@ RETURN n;`;
64
70
  await editorPage.getEditor().press('Enter');
65
71
  await editorPage.getEditor().press('Enter');
66
72
  await page.keyboard.type('entry');
67
- expect(exectutedQueries).toBe(1);
73
+ expect(history.length).toBe(1);
74
+
68
75
  await editorPage.getEditor().press('Control+Enter');
69
76
  await editorPage.getEditor().press('Meta+Enter');
70
- expect(exectutedQueries).toBe(2);
77
+ expect(history.length).toBe(2);
78
+
79
+ // rerender with new history
80
+ await editor.update(
81
+ <CypherEditor
82
+ value={initialValue}
83
+ history={history}
84
+ onExecute={onExecute}
85
+ />,
86
+ );
71
87
 
72
88
  // type a new query and make sure it's not lost when navigating history
73
89
  await editorPage.getEditor().fill('draft');
74
90
  await expect(page.getByText('draft')).toBeVisible();
75
- expect(exectutedQueries).toBe(2);
91
+ expect(history.length).toBe(2);
76
92
 
77
93
  // Navigate to the top of the editor before navigating history
78
94
  await editorPage.getEditor().press('ArrowLeft');
@@ -110,7 +126,7 @@ test('can navigate with cmd+up as well', async ({ page, mount }) => {
110
126
  await mount(
111
127
  <CypherEditor
112
128
  value={initialValue}
113
- initialHistory={[
129
+ history={[
114
130
  `one
115
131
  multiline
116
132
  entry
@@ -142,3 +158,39 @@ entry
142
158
  await editorPage.getEditor().press(metaDown);
143
159
  await expect(page.getByText('multiline')).toBeVisible();
144
160
  });
161
+
162
+ test('test onExecute', async ({ page, mount }) => {
163
+ const editorPage = new CypherEditorPage(page);
164
+ const isMac = process.platform === 'darwin';
165
+ const execButton = isMac ? 'Meta+Enter' : 'Control+Enter';
166
+
167
+ const initialValue = 'MATCH (n) RETURN n;';
168
+ const history = [];
169
+
170
+ const onExecute = (cmd: string) => {
171
+ history.unshift(cmd);
172
+ };
173
+
174
+ const cypherEditor = await mount(
175
+ <CypherEditor value={initialValue} onExecute={onExecute} />,
176
+ );
177
+ await editorPage.getEditor().press(execButton);
178
+ expect(history).toEqual([initialValue]);
179
+
180
+ await editorPage.getEditor().press(execButton);
181
+ expect(history).toEqual([initialValue, initialValue]);
182
+
183
+ // can update onExecute
184
+ let newExecRan = false;
185
+ const newOnExecute = () => {
186
+ newExecRan = true;
187
+ };
188
+ await cypherEditor.update(
189
+ <CypherEditor value={initialValue} onExecute={newOnExecute} />,
190
+ );
191
+
192
+ await editorPage.getEditor().press(execButton);
193
+ expect(newExecRan).toEqual(true);
194
+ // old value should still be only 2
195
+ expect(history).toEqual([initialValue, initialValue]);
196
+ });
@@ -16,7 +16,6 @@ test('Prop lint set to false disables syntax validation', async ({
16
16
  });
17
17
  });
18
18
 
19
- // TODO
20
19
  test.skip('Can turn linting back on', async ({ page, mount }) => {
21
20
  const editorPage = new CypherEditorPage(page);
22
21
  const query = 'METCH (n) RETURN n';
@@ -77,11 +77,11 @@ const historyState = StateField.define<HistoryState>({
77
77
  };
78
78
  }
79
79
  }
80
- } else if (effect.is(pushToHistory)) {
80
+ } else if (effect.is(replaceHistory)) {
81
81
  return {
82
82
  ...value,
83
83
  index: DRAFT_ENTRY_INDEX,
84
- history: [effect.value, ...value.history],
84
+ history: effect.value,
85
85
  };
86
86
  }
87
87
  }
@@ -91,7 +91,7 @@ const historyState = StateField.define<HistoryState>({
91
91
 
92
92
  type HistoryNavigation = 'BACK' | 'FORWARDS';
93
93
  const moveInHistory = StateEffect.define<HistoryNavigation>();
94
- const pushToHistory = StateEffect.define<string>();
94
+ export const replaceHistory = StateEffect.define<string[]>();
95
95
 
96
96
  function navigateHistory(view: EditorView, direction: HistoryNavigation) {
97
97
  view.dispatch(
@@ -116,39 +116,16 @@ function navigateHistory(view: EditorView, direction: HistoryNavigation) {
116
116
  }
117
117
 
118
118
  type ReplProps = {
119
- onExecute: (cmd: string) => void;
120
- initialHistory?: string[];
121
- onNewHistoryEntry?: (historyEntry: string) => void;
119
+ history?: string[];
122
120
  };
123
121
 
124
- export const replMode = ({
125
- onExecute,
126
- onNewHistoryEntry,
127
- initialHistory,
128
- }: ReplProps): Extension[] => {
122
+ export const replMode = ({ history }: ReplProps): Extension[] => {
129
123
  return [
130
124
  historyState.init(() => ({
131
125
  ...historyInitialState,
132
- history: initialHistory,
126
+ history,
133
127
  })),
134
128
  keymap.of([
135
- {
136
- key: 'Ctrl-Enter',
137
- mac: 'Mod-Enter',
138
- preventDefault: true,
139
- run: (view) => {
140
- const doc = view.state.doc.toString();
141
- if (doc.trim() !== '') {
142
- onExecute?.(doc);
143
- onNewHistoryEntry?.(doc);
144
- view.dispatch({
145
- effects: pushToHistory.of(doc),
146
- });
147
- }
148
-
149
- return true;
150
- },
151
- },
152
129
  {
153
130
  key: 'ArrowUp',
154
131
  preventDefault: true,
@@ -1,8 +0,0 @@
1
- import { Extension } from '@codemirror/state';
2
- type ReplProps = {
3
- onExecute: (cmd: string) => void;
4
- initialHistory?: string[];
5
- onNewHistoryEntry?: (historyEntry: string) => void;
6
- };
7
- export declare const replMode: ({ onExecute, onNewHistoryEntry, initialHistory, }: ReplProps) => Extension[];
8
- export {};