-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from tidbcloud/chore/add-test-case
chore: add test for sql-parser/save-helper/core
- Loading branch information
Showing
11 changed files
with
366 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { createSQLEditorInstance, EditorCache } from '..' | ||
|
||
test('editor instance cache works fine', () => { | ||
const cache = new EditorCache() | ||
const editorInst = createSQLEditorInstance({ | ||
editorId: '111', | ||
doc: 'select * from test;' | ||
}) | ||
|
||
expect(cache.getEditor('111')).toBe(undefined) | ||
|
||
cache.addEditor('111', editorInst) | ||
expect(cache.getEditor('111')).toBe(editorInst) | ||
|
||
cache.deleteEditor('222') | ||
expect(cache.getEditor('111')).toBe(editorInst) | ||
|
||
cache.deleteEditor('111') | ||
expect(cache.getEditor('111')).toBe(undefined) | ||
|
||
cache.addEditor('111', editorInst) | ||
expect(cache.getEditor('111')).toBe(editorInst) | ||
|
||
cache.clearEditors() | ||
expect(cache.getEditor('111')).toBe(undefined) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,64 @@ | ||
import { createSQLEditorInstance } from '../editor-instance' | ||
import { createSQLEditorInstance } from '..' | ||
|
||
function sum(a: number, b: number) { | ||
return a + b | ||
} | ||
const LINE_1 = `USE sp500insight;` | ||
const LINE_2 = `SELECT sector, industry, COUNT(*) AS companies | ||
FROM companies c | ||
WHERE c.stock_symbol IN (SELECT stock_symbol FROM index_compositions WHERE index_symbol = "SP500") | ||
GROUP BY sector, industry | ||
ORDER BY sector, companies DESC;` | ||
|
||
test('adds 1 + 2 to equal 3', () => { | ||
expect(sum(1, 2)).toBe(3) | ||
}) | ||
const DOC = `\n${LINE_1}\n\n${LINE_2}\n\n` | ||
|
||
test('create instance successfully', () => { | ||
const instance = createSQLEditorInstance({ | ||
test('editor instance works fine', () => { | ||
const editorInst = createSQLEditorInstance({ | ||
editorId: '111', | ||
doc: '222' | ||
doc: DOC | ||
}) | ||
|
||
expect(editorInst.editorId).toBe('111') | ||
|
||
editorInst.editorView.dispatch({ selection: { anchor: 0, head: 0 } }) | ||
|
||
const allSql = editorInst.getAllStatements() | ||
expect(allSql.length).toBe(2) | ||
expect(allSql[0].content).toBe(LINE_1) | ||
expect(allSql[1].content).toBe(LINE_2) | ||
|
||
let curSql = editorInst.getCurStatements() | ||
expect(curSql[0].content).toBe('') | ||
|
||
let nearbySql = editorInst.getNearbyStatement() | ||
expect(nearbySql?.content).toBe(LINE_1) | ||
|
||
editorInst.editorView.dispatch({ | ||
selection: { anchor: 1, head: 2 } | ||
}) | ||
|
||
curSql = editorInst.getCurStatements() | ||
expect(curSql[0].content).toBe(LINE_1) | ||
|
||
nearbySql = editorInst.getNearbyStatement() | ||
expect(nearbySql?.content).toBe(LINE_1) | ||
|
||
editorInst.editorView.dispatch({ | ||
selection: { anchor: DOC.length, head: DOC.length } | ||
}) | ||
expect(instance.editorId).toBe('111') | ||
|
||
curSql = editorInst.getCurStatements() | ||
expect(curSql[0].content).toBe('') | ||
|
||
nearbySql = editorInst.getNearbyStatement() | ||
expect(nearbySql?.content).toBe(LINE_2) | ||
|
||
editorInst.editorView.dispatch({ | ||
selection: { anchor: 2, head: 2 + LINE_1.length + 5 } | ||
}) | ||
|
||
curSql = editorInst.getCurStatements() | ||
expect(curSql.length).toBe(2) | ||
expect(curSql[0].content).toBe(LINE_1) | ||
expect(curSql[1].content).toBe(LINE_2) | ||
|
||
nearbySql = editorInst.getNearbyStatement() | ||
expect(nearbySql?.content).toBe(LINE_1) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
import { EditorView } from '@codemirror/view' | ||
import { EditorState } from '@codemirror/state' | ||
|
||
import { saveHelper } from '..' | ||
|
||
const LINE_1 = `USE sp500insight;` | ||
const LINE_2 = `SELECT * from companies LIMIT 10;` | ||
|
||
const INIT_DOC = `\n${LINE_1}\n\n` | ||
|
||
function delay(ms: number) { | ||
return new Promise((resolve) => setTimeout(resolve, ms)) | ||
} | ||
|
||
test('test auto save after content changes without any delay', async () => { | ||
let latestContent = '' | ||
const editorView = new EditorView({ | ||
state: EditorState.create({ | ||
doc: INIT_DOC, | ||
extensions: [ | ||
saveHelper({ | ||
save(view) { | ||
latestContent = view.state.doc.toString() | ||
}, | ||
delay: 0 | ||
}) | ||
] | ||
}) | ||
}) | ||
|
||
// dispatch a change transaction to update the content | ||
editorView.dispatch({ changes: { from: 0, insert: LINE_2 } }) | ||
|
||
expect(latestContent).toBe(``) | ||
|
||
await delay(0) | ||
expect(latestContent).toBe(`${LINE_2}${INIT_DOC}`) | ||
}) | ||
|
||
test('test auto save after content changes without 1s delay', async () => { | ||
let latestContent = '' | ||
const editorView = new EditorView({ | ||
state: EditorState.create({ | ||
doc: INIT_DOC, | ||
extensions: [ | ||
saveHelper({ | ||
save(view) { | ||
latestContent = view.state.doc.toString() | ||
}, | ||
delay: 1000 | ||
}) | ||
] | ||
}) | ||
}) | ||
|
||
// dispatch a change transaction to update the content | ||
editorView.dispatch({ changes: { from: 0, insert: LINE_2 } }) | ||
|
||
expect(latestContent).toBe(``) | ||
|
||
await delay(100) | ||
expect(latestContent).toBe(``) | ||
|
||
await delay(1000) | ||
expect(latestContent).toBe(`${LINE_2}${INIT_DOC}`) | ||
}) | ||
|
||
test('test turn off auto save', async () => { | ||
let latestContent = '' | ||
const editorView = new EditorView({ | ||
state: EditorState.create({ | ||
doc: INIT_DOC, | ||
extensions: [ | ||
saveHelper({ | ||
auto: false, | ||
save(view) { | ||
latestContent = view.state.doc.toString() | ||
}, | ||
delay: 0 | ||
}) | ||
] | ||
}) | ||
}) | ||
|
||
// dispatch a change transaction to update the content | ||
editorView.dispatch({ changes: { from: 0, insert: LINE_2 } }) | ||
|
||
await delay(100) | ||
expect(latestContent).toBe(``) | ||
}) | ||
|
||
test('test manual save with default hotkey', () => { | ||
let latestContent = '' | ||
const editorView = new EditorView({ | ||
state: EditorState.create({ | ||
doc: INIT_DOC, | ||
extensions: [ | ||
saveHelper({ | ||
auto: false, | ||
save(view) { | ||
latestContent = view.state.doc.toString() | ||
} | ||
}) | ||
] | ||
}) | ||
}) | ||
|
||
// dispatch a change transaction to update the content | ||
editorView.dispatch({ changes: { from: 0, insert: LINE_2 } }) | ||
|
||
// TODO | ||
// press Mod-s | ||
|
||
expect(latestContent).toBe(``) | ||
}) | ||
|
||
test('test manual save with a non-default hotkey', () => { | ||
let latestContent = '' | ||
const editorView = new EditorView({ | ||
state: EditorState.create({ | ||
doc: INIT_DOC, | ||
extensions: [ | ||
saveHelper({ | ||
auto: false, | ||
hotkey: 'Mod-i', | ||
save(view) { | ||
latestContent = view.state.doc.toString() | ||
} | ||
}) | ||
] | ||
}) | ||
}) | ||
|
||
// dispatch a change transaction to update the content | ||
editorView.dispatch({ changes: { from: 0, insert: LINE_2 } }) | ||
|
||
// TODO | ||
// press Mod-i | ||
|
||
expect(latestContent).toBe(``) | ||
}) | ||
|
||
test('test turn off manual save', () => { | ||
let latestContent = '' | ||
const editorView = new EditorView({ | ||
state: EditorState.create({ | ||
doc: INIT_DOC, | ||
extensions: [ | ||
saveHelper({ | ||
auto: false, | ||
hotkey: '', | ||
save(view) { | ||
latestContent = view.state.doc.toString() | ||
} | ||
}) | ||
] | ||
}) | ||
}) | ||
|
||
// dispatch a change transaction to update the content | ||
editorView.dispatch({ changes: { from: 0, insert: LINE_2 } }) | ||
|
||
// TODO | ||
// press Mod-i | ||
|
||
expect(latestContent).toBe(``) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.