Skip to content

Commit

Permalink
Merge pull request #35 from tidbcloud/chore/test
Browse files Browse the repository at this point in the history
chore: refine and add tests
  • Loading branch information
baurine authored Jun 29, 2024
2 parents b424839 + c56daaf commit feafdc4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
12 changes: 5 additions & 7 deletions packages/extensions/save-helper/src/test/1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ 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))
}
jest.useFakeTimers()

test('test auto save after content changes without any delay', async () => {
let latestContent = ''
Expand All @@ -33,7 +31,7 @@ test('test auto save after content changes without any delay', async () => {

expect(latestContent).toBe(``)

await delay(0)
await jest.advanceTimersByTime(0)
expect(latestContent).toBe(`${LINE_2}${INIT_DOC}`)
})

Expand All @@ -58,10 +56,10 @@ test('test auto save after content changes without 1s delay', async () => {

expect(latestContent).toBe(``)

await delay(100)
await jest.advanceTimersByTime(100)
expect(latestContent).toBe(``)

await delay(1000)
await jest.advanceTimersByTime(1000)
expect(latestContent).toBe(`${LINE_2}${INIT_DOC}`)
})

Expand All @@ -85,7 +83,7 @@ test('test turn off auto save', async () => {
// dispatch a change transaction to update the content
editorView.dispatch({ changes: { from: 0, insert: LINE_2 } })

await delay(100)
await jest.advanceTimersByTime(100)
expect(latestContent).toBe(``)
})

Expand Down
3 changes: 3 additions & 0 deletions packages/react/src/sql-editor.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// @ts-ignore
// to make the test pass, else it will report `React is not defined` error
import React from 'react'
import { useEffect, useLayoutEffect, useRef } from 'react'

import { useEditorCacheContext } from './editor-cache-context'
Expand Down
14 changes: 0 additions & 14 deletions packages/react/src/test/react-component.test.tsx

This file was deleted.

16 changes: 16 additions & 0 deletions packages/react/src/test/sql-editor.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @ts-ignore
import React from 'react'
import { render, screen } from '@testing-library/react'
import '@testing-library/jest-dom'

import { SQLEditor } from '..'

const Editor = () => {
return <SQLEditor editorId="111" doc="select * from test;" />
}

test('test with react-testing-library', () => {
render(<Editor />)
expect(screen.getByText('select')).toBeEnabled()
expect(screen.getByText('from')).toBeEnabled()
})

0 comments on commit feafdc4

Please sign in to comment.