Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/unit test #45

Merged
merged 3 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ Before you create a pull request, please check whether your commits comply with
- refactor: any code related change that is not a fix nor a feature
- chore: all changes to the repository that do not fit into any of the above categories

### Test

To run the test, execute the command `pnpm test`

### Release

- Checkout from the latest main branch.
Expand Down
34 changes: 22 additions & 12 deletions packages/core/src/test/cache.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
import { createSQLEditorInstance, EditorCache } from '..'

test('editor instance cache works fine', () => {
describe('editor instance cache works fine', () => {
const cache = new EditorCache()
const editorInst = createSQLEditorInstance({
editorId: '111',
doc: 'select * from test;'
})

expect(cache.getEditor('111')).toBe(undefined)
test('no cache added', () => {
expect(cache.getEditor('111')).toBe(undefined)
})

cache.addEditor('111', editorInst)
expect(cache.getEditor('111')).toBe(editorInst)
test('add one instance 111 to cache', () => {
cache.addEditor('111', editorInst)
expect(cache.getEditor('111')).toBe(editorInst)
})

cache.deleteEditor('222')
expect(cache.getEditor('111')).toBe(editorInst)
test('remove instance 222 from cache', () => {
cache.deleteEditor('222')
expect(cache.getEditor('111')).toBe(editorInst)
})

cache.deleteEditor('111')
expect(cache.getEditor('111')).toBe(undefined)
test('remove instance 111 from cache', () => {
cache.deleteEditor('111')
expect(cache.getEditor('111')).toBe(undefined)
})

cache.addEditor('111', editorInst)
expect(cache.getEditor('111')).toBe(editorInst)
test('clear cache', () => {
cache.addEditor('111', editorInst)
expect(cache.getEditor('111')).toBe(editorInst)

cache.clearEditors()
expect(cache.getEditor('111')).toBe(undefined)
cache.clearEditors()
expect(cache.getEditor('111')).toBe(undefined)
})
})
116 changes: 77 additions & 39 deletions packages/core/src/test/instance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,94 @@ ORDER BY sector, companies DESC;`

const DOC = `\n${LINE_1}\n\n${LINE_2}\n\n`

test('editor instance works fine', () => {
const editorInst = createSQLEditorInstance({
editorId: '111',
doc: DOC
describe('editor instance works fine', () => {
let editorInst

beforeAll(() => {
editorInst = createSQLEditorInstance({
editorId: '111',
doc: DOC
})
})

expect(editorInst.editorId).toBe('111')
test('editorId should be correct', () =>
expect(editorInst.editorId).toBe('111'))

editorInst.editorView.dispatch({ selection: { anchor: 0, head: 0 } })
describe('dispatch a empty selection action', () => {
let curSql, nearbySql, allSql

const allSql = editorInst.getAllStatements()
expect(allSql.length).toBe(2)
expect(allSql[0].content).toBe(LINE_1)
expect(allSql[1].content).toBe(LINE_2)
beforeAll(() => {
editorInst.editorView.dispatch({ selection: { anchor: 0, head: 0 } })
curSql = editorInst.getCurStatements()
nearbySql = editorInst.getNearbyStatement()
allSql = editorInst.getAllStatements()
})

let curSql = editorInst.getCurStatements()
expect(curSql[0].content).toBe('')
test('the content and length of statement is correct', () => {
expect(allSql.length).toBe(2)
expect(allSql[0].content).toBe(LINE_1)
expect(allSql[1].content).toBe(LINE_2)
})

let nearbySql = editorInst.getNearbyStatement()
expect(nearbySql?.content).toBe(LINE_1)
test('current sql is empty', () => {
expect(curSql[0].content).toBe('')
})

editorInst.editorView.dispatch({
selection: { anchor: 1, head: 2 }
test('nearby sql is line1', () => {
expect(nearbySql?.content).toBe(LINE_1)
})
})

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 }
describe('dispatch a selection action', () => {
let curSql, nearbySql

beforeAll(() => {
editorInst.editorView.dispatch({
selection: { anchor: 1, head: 2 }
})
curSql = editorInst.getCurStatements()
nearbySql = editorInst.getNearbyStatement()
})

test('current sql is line1, nearby sql is line1', () => {
expect(curSql[0].content).toBe(LINE_1)
expect(nearbySql?.content).toBe(LINE_1)
})
})

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 }
describe('dispatch a selection action', () => {
let curSql, nearbySql

beforeAll(() => {
editorInst.editorView.dispatch({
selection: { anchor: DOC.length, head: DOC.length }
})
curSql = editorInst.getCurStatements()
nearbySql = editorInst.getNearbyStatement()
})

test('current sql is empty, nearby sql is line2', () => {
expect(curSql[0].content).toBe('')
expect(nearbySql?.content).toBe(LINE_2)
})
})

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)
describe('dispatch a selection action', () => {
let curSql, nearbySql

beforeAll(() => {
editorInst.editorView.dispatch({
selection: { anchor: 2, head: 2 + LINE_1.length + 5 }
})
curSql = editorInst.getCurStatements()
nearbySql = editorInst.getNearbyStatement()
})

test('current sql length is 2, nearby sql is line1', () => {
expect(curSql.length).toBe(2)
expect(curSql[0].content).toBe(LINE_1)
expect(curSql[1].content).toBe(LINE_2)
expect(nearbySql?.content).toBe(LINE_1)
})
})
})
33 changes: 21 additions & 12 deletions packages/extensions/cur-sql/src/test/cursql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,31 @@ LIMIT

const DOC = `${LINE_1}\n${LINE_2}`

test('test getCurStatements', () => {
const editorView = new EditorView({
state: EditorState.create({
doc: DOC,
extensions: [sqlParser(), sql({ dialect: MySQL }), curSql()]
describe('test getCurStatements', () => {
let editorView: EditorView = new EditorView()

beforeAll(() => {
editorView = new EditorView({
state: EditorState.create({
doc: DOC,
extensions: [sqlParser(), sql({ dialect: MySQL }), curSql()]
})
})
})

editorView.dispatch({ selection: { anchor: 0, head: 0 } })
test('select 0 to 0, the firstSqlStatement should to be LINE_1', () => {
editorView.dispatch({ selection: { anchor: 0, head: 0 } })

const firstSqlStatement = getCurStatements(editorView.state)
expect(firstSqlStatement[0].content).toBe(LINE_1)
const firstSqlStatement = getCurStatements(editorView.state)
expect(firstSqlStatement[0].content).toBe(LINE_1)
})

test('select from DOC.length to DOC.length, the lastSqlStatement should to be LINE_2', () => {
editorView.dispatch({
selection: { anchor: DOC.length, head: DOC.length }
})

editorView.dispatch({
selection: { anchor: DOC.length, head: DOC.length }
const lastSqlStatement = getCurStatements(editorView.state)
expect(lastSqlStatement[0].content).toBe(LINE_2)
})
const lastSqlStatement = getCurStatements(editorView.state)
expect(lastSqlStatement[0].content).toBe(LINE_2)
})
55 changes: 40 additions & 15 deletions packages/extensions/events/src/test/doc-change.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,50 @@ LIMIT

const DOC = `${LINE_1}\n${LINE_2}`

test('test change event', () => {
let doc = ''

const editorView = new EditorView({
state: EditorState.create({
doc: '',
extensions: [
onDocChange((_view, content) => {
doc = content
describe('test change event', () => {
describe('a empty doc, insert LINE_1', () => {
let doc = '',
editorView: EditorView = new EditorView()

beforeAll(() => {
editorView = new EditorView({
state: EditorState.create({
doc: '',
extensions: [
onDocChange((_view, content) => {
doc = content
})
]
})
]
})

editorView.dispatch({ changes: { from: 0, insert: LINE_1 } })
})

test('doc should to be LINE_1', () => expect(doc).toBe(LINE_1))
})

editorView.dispatch({ changes: { from: 0, insert: LINE_1 } })
expect(doc).toBe(LINE_1)
describe('a doc has LINE_1 and insert LINE_2 after LINE_1', () => {
let doc = '',
editorView: EditorView = new EditorView()

beforeAll(() => {
editorView = new EditorView({
state: EditorState.create({
doc: LINE_1,
extensions: [
onDocChange((_view, content) => {
doc = content
})
]
})
})

editorView.dispatch({
changes: { from: LINE_1.length, insert: `\n${LINE_2}` }
})
})

editorView.dispatch({
changes: { from: LINE_1.length, insert: `\n${LINE_2}` }
test('doc should to be DOC', () => expect(doc).toBe(DOC))
})
expect(doc).toBe(DOC)
})
55 changes: 33 additions & 22 deletions packages/extensions/events/src/test/selection-change.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { EditorState } from '@codemirror/state'

import { onSelectionChange, SelectionRange } from '..'

jest.useFakeTimers()

const LINE_1 = 'USE game;'
const LINE_2 = `SELECT
*
Expand All @@ -15,31 +13,44 @@ LIMIT

const DOC = `${LINE_1}\n${LINE_2}`

test('test selection change event', async () => {
describe('test selection change event', () => {
let selRanges: SelectionRange[] = []

const editorView = new EditorView({
state: EditorState.create({
doc: '',
extensions: [
onSelectionChange((_view, ranges) => {
selRanges = ranges
})
]
let editorView: EditorView = new EditorView()

beforeAll(() => {
jest.useFakeTimers()

editorView = new EditorView({
state: EditorState.create({
doc: '',
extensions: [
onSelectionChange((_view, ranges) => {
selRanges = ranges
})
]
})
})

editorView.dispatch({ changes: { from: 0, insert: DOC } })
})

editorView.dispatch({ changes: { from: 0, insert: DOC } })
afterAll(() => jest.useRealTimers())

test('select 0 to LINE_1.length, the selectionRange is correct', async () => {
editorView.dispatch({ selection: { anchor: 0, head: LINE_1.length } })
await jest.advanceTimersByTime(100)

editorView.dispatch({ selection: { anchor: 0, head: LINE_1.length } })
await jest.advanceTimersByTime(100)
expect(selRanges[0].from).toBe(0)
expect(selRanges[0].to).toBe(LINE_1.length)
expect(selRanges[0].from).toBe(0)
expect(selRanges[0].to).toBe(LINE_1.length)
})

test('select LINE_1.length to DOC.length, the selectionRange is correct', async () => {
editorView.dispatch({
selection: { anchor: LINE_1.length, head: DOC.length }
})
await jest.advanceTimersByTime(100)

editorView.dispatch({
selection: { anchor: LINE_1.length, head: DOC.length }
expect(selRanges[0].from).toBe(LINE_1.length)
expect(selRanges[0].to).toBe(DOC.length)
})
await jest.advanceTimersByTime(100)
expect(selRanges[0].from).toBe(LINE_1.length)
expect(selRanges[0].to).toBe(DOC.length)
})
Loading