From 3b49166745b497708c35fc0899b78e29f2842467 Mon Sep 17 00:00:00 2001
From: baurine <2008.hbl@gmail.com>
Date: Fri, 28 Jun 2024 15:53:12 +0800
Subject: [PATCH 1/2] test: use jest delay api
---
packages/extensions/save-helper/src/test/1.test.ts | 12 +++++-------
1 file changed, 5 insertions(+), 7 deletions(-)
diff --git a/packages/extensions/save-helper/src/test/1.test.ts b/packages/extensions/save-helper/src/test/1.test.ts
index 023b2c5..1fdd31d 100644
--- a/packages/extensions/save-helper/src/test/1.test.ts
+++ b/packages/extensions/save-helper/src/test/1.test.ts
@@ -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 = ''
@@ -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}`)
})
@@ -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}`)
})
@@ -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(``)
})
From c56daaff9759a2eaaf5491d45c1223f2e35dc134 Mon Sep 17 00:00:00 2001
From: baurine <2008.hbl@gmail.com>
Date: Fri, 28 Jun 2024 17:46:15 +0800
Subject: [PATCH 2/2] chore: test simple test case for SQLEditor react
component
---
packages/react/src/sql-editor.tsx | 3 +++
packages/react/src/test/react-component.test.tsx | 14 --------------
packages/react/src/test/sql-editor.test.tsx | 16 ++++++++++++++++
3 files changed, 19 insertions(+), 14 deletions(-)
delete mode 100644 packages/react/src/test/react-component.test.tsx
create mode 100644 packages/react/src/test/sql-editor.test.tsx
diff --git a/packages/react/src/sql-editor.tsx b/packages/react/src/sql-editor.tsx
index 585c5e3..ad2e93a 100644
--- a/packages/react/src/sql-editor.tsx
+++ b/packages/react/src/sql-editor.tsx
@@ -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'
diff --git a/packages/react/src/test/react-component.test.tsx b/packages/react/src/test/react-component.test.tsx
deleted file mode 100644
index a77e638..0000000
--- a/packages/react/src/test/react-component.test.tsx
+++ /dev/null
@@ -1,14 +0,0 @@
-import { render, screen } from '@testing-library/react'
-// import SQLEditor from '../sql-editor'
-import '@testing-library/jest-dom'
-// @ts-ignore
-import React from 'react'
-
-const Example = () => {
- return
-}
-
-test('test with react-testing-library', () => {
- render()
- expect(screen.getByRole('textbox')).toBeEnabled()
-})
diff --git a/packages/react/src/test/sql-editor.test.tsx b/packages/react/src/test/sql-editor.test.tsx
new file mode 100644
index 0000000..fbce9d4
--- /dev/null
+++ b/packages/react/src/test/sql-editor.test.tsx
@@ -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
+}
+
+test('test with react-testing-library', () => {
+ render()
+ expect(screen.getByText('select')).toBeEnabled()
+ expect(screen.getByText('from')).toBeEnabled()
+})