Skip to content

Commit 3c9d6ce

Browse files
committed
chore: refactor tests and update vitest main config
1 parent 6b77ebe commit 3c9d6ce

File tree

9 files changed

+1115
-896
lines changed

9 files changed

+1115
-896
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { beforeEach, describe, expect, it } from 'vitest'
2+
3+
import { selectAllRows } from '@/graphql/__tests__/mutations/tiles/tiles-pg-helper'
4+
5+
import { createTableRow, createTableRows } from '../table-row-functions'
6+
7+
import {
8+
createMultipleTestRows,
9+
createTestRowData,
10+
createTestSetup,
11+
TestSetup,
12+
} from './table-row-test-utils'
13+
14+
describe('table-row-functions: create operations', () => {
15+
let setup: TestSetup
16+
17+
beforeEach(async () => {
18+
setup = await createTestSetup()
19+
})
20+
21+
describe('createTableRow', () => {
22+
it('should create a single row with the provided data', async () => {
23+
const data = createTestRowData(setup.testColumnIds)
24+
25+
const result = await createTableRow({
26+
tableId: setup.testTable.id,
27+
data,
28+
})
29+
30+
const dbRows = await selectAllRows(setup.testTable.id)
31+
expect(dbRows).toHaveLength(1)
32+
expect(dbRows[0].rowId).toBe(result.rowId)
33+
})
34+
35+
it('should fail if the data is invalid', async () => {
36+
const data = createTestRowData(['invalid-column-id'])
37+
38+
await expect(
39+
createTableRow({
40+
tableId: setup.testTable.id,
41+
data,
42+
}),
43+
).rejects.toThrow()
44+
})
45+
46+
it('should cast the data to the correct type', async () => {
47+
const data = createTestRowData(setup.testColumnIds)
48+
49+
// @ts-expect-error - we want to test the casting
50+
data[setup.testColumnIds[0]] = 123
51+
52+
await expect(
53+
createTableRow({
54+
tableId: setup.testTable.id,
55+
data,
56+
}),
57+
).resolves.not.toThrow()
58+
59+
const dbRows = await selectAllRows(setup.testTable.id)
60+
expect(dbRows).toHaveLength(1)
61+
expect(dbRows[0][setup.testColumnIds[0]]).toBe('123')
62+
})
63+
})
64+
65+
describe('createTableRows', () => {
66+
it('should create multiple rows with the provided data', async () => {
67+
const dataArray = createMultipleTestRows(3, setup.testColumnIds)
68+
69+
const rowIds = await createTableRows({
70+
tableId: setup.testTable.id,
71+
dataArray,
72+
})
73+
expect(rowIds).toHaveLength(3)
74+
75+
const dbRows = await selectAllRows(setup.testTable.id)
76+
expect(dbRows).toHaveLength(3)
77+
})
78+
79+
it('should maintain the order of rows created', async () => {
80+
const dataArray = createMultipleTestRows(3, setup.testColumnIds)
81+
82+
const rowIds = await createTableRows({
83+
tableId: setup.testTable.id,
84+
dataArray,
85+
})
86+
expect(rowIds).toHaveLength(3)
87+
88+
const dbRows = await selectAllRows(setup.testTable.id)
89+
expect(dbRows).toHaveLength(3)
90+
expect(dbRows[0][setup.testColumnIds[0]]).toBe(
91+
dataArray[0][setup.testColumnIds[0]],
92+
)
93+
expect(dbRows[1][setup.testColumnIds[0]]).toBe(
94+
dataArray[1][setup.testColumnIds[0]],
95+
)
96+
expect(dbRows[2][setup.testColumnIds[0]]).toBe(
97+
dataArray[2][setup.testColumnIds[0]],
98+
)
99+
})
100+
})
101+
})
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { ulid } from 'ulid'
2+
import { beforeEach, describe, expect, it } from 'vitest'
3+
4+
import { selectAllRows } from '@/graphql/__tests__/mutations/tiles/tiles-pg-helper'
5+
6+
import { createTableRows, deleteTableRows } from '../table-row-functions'
7+
8+
import {
9+
createMultipleTestRows,
10+
createTestSetup,
11+
TestSetup,
12+
} from './table-row-test-utils'
13+
14+
describe('table-row-functions: delete operations', () => {
15+
let setup: TestSetup
16+
17+
beforeEach(async () => {
18+
setup = await createTestSetup()
19+
})
20+
21+
describe('deleteTableRows', () => {
22+
it('should delete multiple rows by their IDs', async () => {
23+
const dataArray = createMultipleTestRows(3, setup.testColumnIds)
24+
const rowIds = await createTableRows({
25+
tableId: setup.testTable.id,
26+
dataArray,
27+
})
28+
29+
await deleteTableRows({
30+
tableId: setup.testTable.id,
31+
rowIds: [rowIds[0], rowIds[1]],
32+
})
33+
34+
const dbRows = await selectAllRows(setup.testTable.id)
35+
expect(dbRows).toHaveLength(1)
36+
expect(dbRows[0].rowId).toBe(rowIds[2])
37+
})
38+
39+
it('should not throw an error when deleting non-existent rows', async () => {
40+
const nonExistentIds = [ulid(), ulid()]
41+
42+
await expect(
43+
deleteTableRows({
44+
tableId: setup.testTable.id,
45+
rowIds: nonExistentIds,
46+
}),
47+
).resolves.not.toThrow()
48+
})
49+
})
50+
})

0 commit comments

Comments
 (0)