From bf7aaaaf7032223e9606b18c089c111f2d0277e4 Mon Sep 17 00:00:00 2001 From: Murderlon Date: Wed, 8 May 2024 13:17:57 +0200 Subject: [PATCH 1/2] @uppy/core: add type tests --- packages/@uppy/core/src/types.test.ts | 45 +++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 packages/@uppy/core/src/types.test.ts diff --git a/packages/@uppy/core/src/types.test.ts b/packages/@uppy/core/src/types.test.ts new file mode 100644 index 0000000000..6d92e2ad81 --- /dev/null +++ b/packages/@uppy/core/src/types.test.ts @@ -0,0 +1,45 @@ +import { expectTypeOf, test } from 'vitest' + +import type { Body, InternalMetadata, Meta } from '@uppy/utils/lib/UppyFile' +import Uppy from './Uppy' +import UIPlugin, { UIPluginOptions } from './UIPlugin' + +interface Opts extends UIPluginOptions { + foo: string +} +class TestPlugin extends UIPlugin {} + +test('can use Uppy class without generics', async () => { + const core = new Uppy() + expectTypeOf(core).toEqualTypeOf>() +}) + +test('can .use() a plugin', async () => { + const core = new Uppy().use(TestPlugin) + expectTypeOf(core).toEqualTypeOf>() +}) + +test('Meta and Body generic move through the Uppy class', async () => { + type M = { foo: string } + type B = { bar: string } + const core = new Uppy() + + 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() + + await core.upload() +}) From 78a79ed35fe0800cc80170526634465c48fb3db1 Mon Sep 17 00:00:00 2001 From: Murderlon Date: Wed, 8 May 2024 14:36:50 +0200 Subject: [PATCH 2/2] Fixup --- packages/@uppy/core/src/types.test.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/@uppy/core/src/types.test.ts b/packages/@uppy/core/src/types.test.ts index 6d92e2ad81..e73cea2f4c 100644 --- a/packages/@uppy/core/src/types.test.ts +++ b/packages/@uppy/core/src/types.test.ts @@ -2,12 +2,18 @@ import { expectTypeOf, test } from 'vitest' import type { Body, InternalMetadata, Meta } from '@uppy/utils/lib/UppyFile' import Uppy from './Uppy' -import UIPlugin, { UIPluginOptions } from './UIPlugin' +import UIPlugin, { type UIPluginOptions } from './UIPlugin' interface Opts extends UIPluginOptions { foo: string } -class TestPlugin extends UIPlugin {} +class TestPlugin extends UIPlugin { + constructor(uppy: Uppy, opts?: Opts) { + super(uppy, opts) + this.id = 'TestPlugin' + this.type = 'acquirer' + } +} test('can use Uppy class without generics', async () => { const core = new Uppy()