Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@uppy/core: add type tests #5153

Merged
merged 2 commits into from
May 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions packages/@uppy/core/src/types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { expectTypeOf, test } from 'vitest'

import type { Body, InternalMetadata, Meta } from '@uppy/utils/lib/UppyFile'
import Uppy from './Uppy'
import UIPlugin, { type UIPluginOptions } from './UIPlugin'

interface Opts extends UIPluginOptions {
foo: string
}
class TestPlugin<M extends Meta, B extends Body> extends UIPlugin<Opts, M, B> {
constructor(uppy: Uppy<M, B>, opts?: Opts) {
super(uppy, opts)
this.id = 'TestPlugin'
this.type = 'acquirer'
}
}

test('can use Uppy class without generics', async () => {
const core = new Uppy()
expectTypeOf(core).toEqualTypeOf<Uppy<Meta, Body>>()
})

test('can .use() a plugin', async () => {
const core = new Uppy().use(TestPlugin)
expectTypeOf(core).toEqualTypeOf<Uppy<Meta, Body>>()
})

test('Meta and Body generic move through the Uppy class', async () => {
type M = { foo: string }
type B = { bar: string }
const core = new Uppy<M, B>()

core.addUploader(() => Promise.resolve())

core.on('complete', (result) => {
expectTypeOf(result.successful?.[0]?.response?.body).toEqualTypeOf<
B | undefined
>()
})

const id = core.addFile({
source: 'vi',
name: 'foo.jpg',
type: 'image/jpeg',
data: new Blob(),
})

expectTypeOf(core.getFile(id).meta).toEqualTypeOf<InternalMetadata & M>()

await core.upload()
})