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

feat: extension events add test case #33

Merged
merged 2 commits into from
Jun 28, 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
6 changes: 5 additions & 1 deletion packages/extensions/events/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@
"author": "",
"license": "MIT",
"devDependencies": {
"@codemirror/lang-sql": "^6.6.4",
"@codemirror/view": "^6.26.3",
"@codemirror/state": "^6.4.1",
"@rollup/plugin-typescript": "^11.1.6",
"rollup": "^4.18.0",
"tslib": "^2.6.3",
"typescript": "^5.4.5"
},
"peerDependencies": {
"@codemirror/view": "^6.26.3"
"@codemirror/lang-sql": "^6.6.4",
"@codemirror/view": "^6.26.3",
"@codemirror/state": "^6.4.1"
},
"dependencies": {
"@tidbcloud/codemirror-extension-cur-sql": "workspace:^",
Expand Down
6 changes: 2 additions & 4 deletions packages/extensions/events/src/change.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { EditorView, ViewUpdate } from '@codemirror/view'

type ChangeHelperOptions = {
onChange: (sql: string, view?: EditorView) => void
}
type ChangeHelperOptions = (sql: string, view?: EditorView) => void

const changeHandler = (change: (sql: string, view?: EditorView) => void) => {
return EditorView.updateListener.of((update: ViewUpdate) => {
Expand All @@ -13,6 +11,6 @@ const changeHandler = (change: (sql: string, view?: EditorView) => void) => {
})
}

export const onChange = ({ onChange }: ChangeHelperOptions) => {
export const onChange = (onChange: ChangeHelperOptions) => {
return [changeHandler(onChange)]
}
10 changes: 3 additions & 7 deletions packages/extensions/events/src/focus-change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { EditorView, ViewUpdate } from '@codemirror/view'
import { getCurStatements } from '@tidbcloud/codemirror-extension-cur-sql'
import { SqlStatement } from '@tidbcloud/codemirror-extension-sql-parser'

type FocusChangeHelperOptions = {
onFocusChange: (curSql: SqlStatement[]) => void
}
type FocusChangeHelperOptions = (curSql: SqlStatement[]) => void

const focusChangeHandler = (change: (curSql: SqlStatement[]) => void) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel this extension name onFocusChange doesn't correspond with what it does. According its name, it should be a common extension works with any kind content, not just SQL.

let timer: number | undefined
Expand All @@ -28,8 +26,6 @@ const focusChangeHandler = (change: (curSql: SqlStatement[]) => void) => {
})
}

export const focusChangeHelper = ({
onFocusChange
}: FocusChangeHelperOptions) => {
return [focusChangeHandler(onFocusChange)]
export const onFocusChange = (onFocusChangeCb: FocusChangeHelperOptions) => {
return [focusChangeHandler(onFocusChangeCb)]
}
16 changes: 8 additions & 8 deletions packages/extensions/events/src/selection-change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ export interface SelectionRange {
to: number
}

type SelectionChangeHelperOptions = {
onFocusChange: (curSql: SelectionRange[]) => void
}
type SelectionChangeHelperOptions = (selectionRange: SelectionRange[]) => void

const selectionChangeHandler = (change: (curSql: SelectionRange[]) => void) => {
const selectionChangeHandler = (
change: (selectionRange: SelectionRange[]) => void
) => {
let timer: number | undefined
let first = true

Expand All @@ -36,8 +36,8 @@ const selectionChangeHandler = (change: (curSql: SelectionRange[]) => void) => {
})
}

export const onSelectionChange = ({
onFocusChange
}: SelectionChangeHelperOptions) => {
return [selectionChangeHandler(onFocusChange)]
export const onSelectionChange = (
onSelectionChange: SelectionChangeHelperOptions
) => {
return [selectionChangeHandler(onSelectionChange)]
}
37 changes: 37 additions & 0 deletions packages/extensions/events/src/test/on-change.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { EditorView } from '@codemirror/view'
import { EditorState } from '@codemirror/state'

import { onChange } from '..'

const LINE_1 = 'USE game;'
const LINE_2 = `SELECT
*
from
game.all_audio_language
LIMIT
10;`

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

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

const editorView = new EditorView({
state: EditorState.create({
doc: '',
extensions: [
onChange((sql) => {
curSql = sql
})
]
})
})

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

editorView.dispatch({
changes: { from: LINE_1.length, insert: `\n${LINE_2}` }
})
expect(curSql).toBe(DOC)
})
53 changes: 53 additions & 0 deletions packages/extensions/events/src/test/on-focus-change.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { EditorView } from '@codemirror/view'
import { EditorState } from '@codemirror/state'

import { MySQL, sql } from '@codemirror/lang-sql'
import {
sqlParser,
SqlStatement
} from '@tidbcloud/codemirror-extension-sql-parser'
import { curSql } from '@tidbcloud/codemirror-extension-cur-sql'

import { onFocusChange } from '..'

jest.useFakeTimers()

const LINE_1 = 'USE game;'
const LINE_2 = `SELECT
*
from
game.all_audio_language
LIMIT
10;`

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

test('test focuse change event', async () => {
let sqlStatement: SqlStatement[] = []

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

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

editorView.dispatch({ selection: { anchor: 0, head: 0 } })
await jest.advanceTimersByTime(100)
expect(sqlStatement.length).toBe(1)
expect(sqlStatement[0].content).toBe(LINE_1)

editorView.dispatch({ selection: { anchor: DOC.length, head: DOC.length } })
await jest.advanceTimersByTime(100)
expect(sqlStatement.length).toBe(1)
expect(sqlStatement[0].content).toBe(LINE_2)
})
52 changes: 52 additions & 0 deletions packages/extensions/events/src/test/on-selection-change.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { EditorView } from '@codemirror/view'
import { EditorState } from '@codemirror/state'

import { MySQL, sql } from '@codemirror/lang-sql'
import { sqlParser } from '@tidbcloud/codemirror-extension-sql-parser'
import { curSql } from '@tidbcloud/codemirror-extension-cur-sql'

import { onSelectionChange, SelectionRange } from '..'

jest.useFakeTimers()

const LINE_1 = 'USE game;'
const LINE_2 = `SELECT
*
from
game.all_audio_language
LIMIT
10;`

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

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

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

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

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

editorView.dispatch({
selection: { anchor: LINE_1.length, head: DOC.length }
})
await jest.advanceTimersByTime(100)
expect(sqlStatement[0].from).toBe(LINE_1.length)
expect(sqlStatement[0].to).toBe(DOC.length)
})
3 changes: 2 additions & 1 deletion packages/extensions/events/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"compilerOptions": {
"outDir": "./dist"
},
"include": ["./src"]
"include": ["./src"],
"exclude": ["./src/**/*.test.ts"]
}
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.