Skip to content

Commit 9bcae9c

Browse files
committed
refactor: re-arrange tests and extract error messages and strings
1 parent ad583bd commit 9bcae9c

File tree

5 files changed

+44
-75
lines changed

5 files changed

+44
-75
lines changed

example/src/tests/unit/common.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ import {
66
import { resetTestDb } from '../db'
77
import chai from 'chai'
88

9+
export const TEST_ERROR_CODES = {
10+
EXPECT_NITRO_SQLITE_ERROR: 'Should have thrown a valid NitroSQLiteError',
11+
EXPECT_PROMISE_REJECTION: 'Should have thrown a promise rejection',
12+
} as const
13+
14+
export const TEST_ERROR_MESSAGE = 'Error from callback'
15+
export const TEST_ERROR = new Error(TEST_ERROR_MESSAGE)
16+
917
export function isNitroSQLiteError(e: unknown): e is NitroSQLiteError {
1018
return e instanceof NitroSQLiteError
1119
}

example/src/tests/unit/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { beforeEach, describe } from '../MochaRNAdapter'
22
import { setupTestDb } from './common'
3-
import registerExecuteUnitTests from './specs/execute.spec'
4-
import registerTransactionUnitTests from './specs/transaction.spec'
5-
import registerExecuteBatchUnitTests from './specs/executeBatch.spec'
3+
import registerExecuteUnitTests from './specs/operations/execute.spec'
4+
import registerTransactionUnitTests from './specs/operations/transaction.spec'
5+
import registerExecuteBatchUnitTests from './specs/operations/executeBatch.spec'
66
import registerTypeORMUnitTestsSpecs from './specs/typeorm.spec'
7+
import registerDatabaseQueueUnitTests from './specs/DatabaseQueue.spec'
78

89
export function registerUnitTests() {
910
beforeEach(setupTestDb)
@@ -13,6 +14,8 @@ export function registerUnitTests() {
1314
registerTransactionUnitTests()
1415
registerExecuteBatchUnitTests()
1516
})
17+
18+
registerDatabaseQueueUnitTests()
1619
}
1720

1821
export function registerTypeORMUnitTests() {

example/src/tests/unit/specs/execute.spec.ts renamed to example/src/tests/unit/specs/operations/execute.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { chance, expect, isNitroSQLiteError } from '../common'
1+
import { chance, expect, isNitroSQLiteError } from '../../common'
22
import {
33
enableSimpleNullHandling,
44
NITRO_SQLITE_NULL,
55
} from 'react-native-nitro-sqlite'
6-
import { describe, it } from '../../MochaRNAdapter'
7-
import { testDb } from '../../db'
6+
import { describe, it } from '../../../MochaRNAdapter'
7+
import { testDb } from '../../../db'
88

99
export default function registerExecuteUnitTests() {
1010
describe('execute', () => {

example/src/tests/unit/specs/executeBatch.spec.ts renamed to example/src/tests/unit/specs/operations/executeBatch.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { chance, expect } from '../common'
1+
import { chance, expect } from '../../common'
22
import type { BatchQueryCommand } from 'react-native-nitro-sqlite'
3-
import { describe, it } from '../../MochaRNAdapter'
4-
import { testDb } from '../../db'
3+
import { describe, it } from '../../../MochaRNAdapter'
4+
import { testDb } from '../../../db'
55

66
export default function registerExecuteBatchUnitTests() {
77
describe('executeBatch', () => {

example/src/tests/unit/specs/transaction.spec.ts renamed to example/src/tests/unit/specs/operations/transaction.spec.ts

Lines changed: 24 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import { chance, expect, isNitroSQLiteError } from '../common'
2-
import { describe, it } from '../../MochaRNAdapter'
3-
import type { User } from '../../../model/User'
4-
import { testDb, testDbQueue } from '../../db'
5-
6-
const DUMMY_ERROR_NAME = 'Transaction Rejection Error'
7-
const DUMMY_ERROR_MESSAGE = 'Error from callback'
1+
import {
2+
chance,
3+
expect,
4+
isNitroSQLiteError,
5+
TEST_ERROR,
6+
TEST_ERROR_MESSAGE,
7+
TEST_ERROR_CODES,
8+
} from '../../common'
9+
import { describe, it } from '../../../MochaRNAdapter'
10+
import type { User } from '../../../../model/User'
11+
import { testDb } from '../../../db'
812

913
export default function registerTransactionUnitTests() {
1014
describe('transaction', () => {
@@ -202,19 +206,18 @@ export default function registerTransactionUnitTests() {
202206

203207
it('Transaction, rejects on callback error', async () => {
204208
const promised = testDb.transaction(async () => {
205-
throw new Error(DUMMY_ERROR_MESSAGE)
209+
throw TEST_ERROR
206210
})
207211

208212
// ASSERT: should return a promise that eventually rejects
209213
expect(promised).to.have.property('then').that.is.a('function')
210214
try {
211215
await promised
212-
expect.fail(DUMMY_ERROR_NAME)
216+
expect.fail(TEST_ERROR_CODES.EXPECT_PROMISE_REJECTION)
213217
} catch (e) {
214-
console.log(e)
215218
if (isNitroSQLiteError(e))
216-
expect(e.message).to.include(DUMMY_ERROR_MESSAGE)
217-
else expect.fail('Should have thrown a valid NitroSQLiteError')
219+
expect(e.message).to.include(TEST_ERROR_MESSAGE)
220+
else expect.fail(TEST_ERROR_CODES.EXPECT_NITRO_SQLITE_ERROR)
218221
}
219222
})
220223

@@ -226,11 +229,11 @@ export default function registerTransactionUnitTests() {
226229
expect(promised).to.have.property('then').that.is.a('function')
227230
try {
228231
await promised
229-
expect.fail(DUMMY_ERROR_NAME)
232+
expect.fail(TEST_ERROR_CODES.EXPECT_PROMISE_REJECTION)
230233
} catch (e) {
231234
if (isNitroSQLiteError(e))
232235
expect(e.message).to.include('no such table: tableThatDoesNotExist')
233-
else expect.fail('Should have thrown a valid NitroSQLiteError')
236+
else expect.fail(TEST_ERROR_CODES.EXPECT_NITRO_SQLITE_ERROR)
234237
}
235238
})
236239

@@ -302,7 +305,7 @@ export default function registerTransactionUnitTests() {
302305
const res = testDb.execute('SELECT * FROM User')
303306
expect(res.rows?._array).to.eql([])
304307
} else {
305-
expect.fail('Should have thrown a valid NitroSQLiteError')
308+
expect.fail(TEST_ERROR_CODES.EXPECT_NITRO_SQLITE_ERROR)
306309
}
307310
}
308311
})
@@ -406,18 +409,18 @@ export default function registerTransactionUnitTests() {
406409

407410
it('Async transaction, rejects on callback error', async () => {
408411
const promised = testDb.transaction(() => {
409-
throw new Error(DUMMY_ERROR_MESSAGE)
412+
throw new Error(TEST_ERROR_MESSAGE)
410413
})
411414

412415
// ASSERT: should return a promise that eventually rejects
413416
expect(promised).to.have.property('then').that.is.a('function')
414417
try {
415418
await promised
416-
expect.fail(DUMMY_ERROR_NAME)
419+
expect.fail(TEST_ERROR_CODES.EXPECT_PROMISE_REJECTION)
417420
} catch (e) {
418421
if (isNitroSQLiteError(e))
419-
expect(e.message).to.include(DUMMY_ERROR_MESSAGE)
420-
else expect.fail('Should have thrown a valid NitroSQLiteError')
422+
expect(e.message).to.include(TEST_ERROR_MESSAGE)
423+
else expect.fail(TEST_ERROR_CODES.EXPECT_NITRO_SQLITE_ERROR)
421424
}
422425
})
423426

@@ -430,57 +433,12 @@ export default function registerTransactionUnitTests() {
430433
expect(promised).to.have.property('then').that.is.a('function')
431434
try {
432435
await promised
433-
expect.fail(DUMMY_ERROR_NAME)
436+
expect.fail(TEST_ERROR_CODES.EXPECT_PROMISE_REJECTION)
434437
} catch (e) {
435438
if (isNitroSQLiteError(e))
436439
expect(e.message).to.include('no such table: tableThatDoesNotExist')
437-
else expect.fail('Should have thrown a valid NitroSQLiteError')
440+
else expect.fail(TEST_ERROR_CODES.EXPECT_NITRO_SQLITE_ERROR)
438441
}
439442
})
440-
441-
it('transaction are queued', async () => {
442-
const transaction1Promise = testDb.transaction(async (tx) => {
443-
tx.execute('SELECT * FROM [User];')
444-
445-
expect(testDbQueue.queue.length).to.equal(2)
446-
expect(testDbQueue.inProgress).to.equal(true)
447-
448-
await new Promise<void>((resolve) => setTimeout(resolve, 100))
449-
450-
tx.execute('SELECT * FROM [User];')
451-
452-
expect(testDbQueue.queue.length).to.equal(2)
453-
expect(testDbQueue.inProgress).to.equal(true)
454-
})
455-
456-
expect(testDbQueue.inProgress).to.equal(true)
457-
expect(testDbQueue.queue.length).to.equal(0)
458-
459-
const transaction2Promise = testDb.transaction(async (tx) => {
460-
tx.execute('SELECT * FROM [User];')
461-
})
462-
463-
expect(testDbQueue.queue.length).to.equal(1)
464-
expect(testDbQueue.inProgress).to.equal(true)
465-
466-
const transaction3Promise = testDb.transaction(async (tx) => {
467-
tx.execute('SELECT * FROM [User];')
468-
})
469-
470-
await transaction1Promise
471-
472-
expect(testDbQueue.queue.length).to.equal(1)
473-
expect(testDbQueue.inProgress).to.equal(true)
474-
475-
await transaction2Promise
476-
477-
expect(testDbQueue.queue.length).to.equal(0)
478-
expect(testDbQueue.inProgress).to.equal(true)
479-
480-
await transaction3Promise
481-
482-
expect(testDbQueue.queue.length).to.equal(0)
483-
expect(testDbQueue.inProgress).to.equal(false)
484-
})
485443
})
486444
}

0 commit comments

Comments
 (0)