Bladeren bron

@uppy/core: add type tests (#5153)

Merlijn Vos 10 maanden geleden
bovenliggende
commit
28767b4cf4
1 gewijzigde bestanden met toevoegingen van 51 en 0 verwijderingen
  1. 51 0
      packages/@uppy/core/src/types.test.ts

+ 51 - 0
packages/@uppy/core/src/types.test.ts

@@ -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()
+})