Skip to content

Commit

Permalink
Configure and add type tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dac09 committed Sep 3, 2024
1 parent 15cb4ec commit 305381a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"check:package": "concurrently npm:check:attw yarn publint",
"setup:test": "npx prisma db push --accept-data-loss --schema ./src/__tests__/unit-test-schema.prisma",
"test": "vitest run",
"test:types": "tstyche",
"test:watch": "vitest watch"
},
"dependencies": {
Expand All @@ -64,6 +65,7 @@
"concurrently": "8.2.2",
"esbuild": "0.23.0",
"publint": "0.2.10",
"tstyche": "2.1.1",
"tsx": "4.17.0",
"typescript": "5.5.4",
"vitest": "2.0.5"
Expand Down
62 changes: 62 additions & 0 deletions packages/storage/src/__typetests__/types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { expect, test } from 'tstyche'

import { MemoryStorage } from '../adapters/MemoryStorage/MemoryStorage.js'
import { setupStorage } from '../index.js'
import type { UploadsConfig } from '../prismaExtension.js'

const uploadsConfig: UploadsConfig = {
dummy: {
fields: 'uploadField',
},
dumbo: {
fields: ['firstUpload', 'secondUpload'],
},
}

const { saveFiles } = setupStorage({
uploadsConfig,
storageAdapter: new MemoryStorage({
baseDir: '/tmp',
}),
})

// const prismaClient = new PrismaClient().$extends(storagePrismaExtension)

test('only configured models have savers', async () => {
expect(saveFiles).type.toHaveProperty('forDummy')
expect(saveFiles).type.toHaveProperty('forDumbo')

saveFiles.forDummy({})
// @ts-expect-error
saveFiles.forNoUploadFields({})

// These weren't configured above
expect(saveFiles).type.not.toHaveProperty('forNoUploadFields')
expect(saveFiles).type.not.toHaveProperty('forBook')
// expect(saveFiles).type.not.toHaveProperty('forBookCover')
})

test('UploadsConfig accepts all available models with their fields', async () => {
expect<UploadsConfig>().type.toHaveProperty('dummy')
expect<UploadsConfig>().type.toHaveProperty('dumbo')
expect<UploadsConfig>().type.toHaveProperty('book')
expect<UploadsConfig>().type.toHaveProperty('bookCover')
expect<UploadsConfig>().type.toHaveProperty('noUploadFields')

expect<UploadsConfig['dumbo']>().type.toBeAssignableWith<{
fields: ['firstUpload'] // one of the fields, but not all of them
}>()

expect<UploadsConfig['dumbo']>().type.toBeAssignableWith<{
fields: ['firstUpload', 'secondUpload'] // one of the fields, but not all of them
}>()

expect<UploadsConfig['bookCover']>().type.toBeAssignableWith<{
fields: ['photo']
}>()

// If you give it something else, it won't accept it
expect<UploadsConfig['bookCover']>().type.not.toBeAssignableWith<{
fields: ['bazinga']
}>()
})
2 changes: 1 addition & 1 deletion packages/storage/vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const __dirname = path.dirname(__filename)

export default defineConfig({
test: {
exclude: [...configDefaults.exclude, '**/fixtures'],
exclude: [...configDefaults.exclude, '**/fixtures', '**/__typetests__'],
deps: {
interopDefault: false,
},
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8615,6 +8615,7 @@ __metadata:
esbuild: "npm:0.23.0"
mime-types: "npm:2.1.35"
publint: "npm:0.2.10"
tstyche: "npm:2.1.1"
tsx: "npm:4.17.0"
typescript: "npm:5.5.4"
ulid: "npm:2.3.0"
Expand Down

0 comments on commit 305381a

Please sign in to comment.