UNPKG

3.14 kBPlain TextView Raw
1import { expectError, expectType } from 'tsd'
2import Uppy = require('../')
3import DefaultStore = require('@uppy/store-default')
4
5{
6 const uppy = Uppy<Uppy.StrictTypes>()
7 uppy.addFile({
8 data: new Blob([new ArrayBuffer(1024)], {
9 type: 'application/octet-stream'
10 })
11 })
12
13 uppy.upload().then(result => {
14 expectType<Uppy.UploadedUppyFile<{}, {}>>(result.successful[0])
15 expectType<Uppy.FailedUppyFile<{}, {}>>(result.failed[0])
16 })
17}
18
19{
20 const store = DefaultStore()
21 const uppy = Uppy<Uppy.StrictTypes>({ store })
22}
23
24{
25 const uppy = Uppy<Uppy.StrictTypes>()
26 // this doesn't exist but type checking works anyway :)
27 const f = uppy.getFile('virtual')
28 if (f && f.progress && f.progress.uploadStarted === null) {
29 f.progress.uploadStarted = Date.now()
30 }
31
32 if (f && f.response && f.response.status === 200) {
33 expectType(f.response.body)
34 }
35 expectType<number>(f.response!.status)
36}
37
38{
39 type Meta = {}
40 type ResponseBody = {
41 averageColor: string
42 }
43 const uppy = Uppy<Uppy.StrictTypes>()
44 const f = uppy.getFile<Meta, ResponseBody>('virtual')!
45 expectType<ResponseBody>(f.response!.body)
46}
47
48{
49 const uppy = Uppy<Uppy.StrictTypes>()
50 uppy.addFile({
51 name: 'empty.json',
52 data: new Blob(['null'], { type: 'application/json' }),
53 meta: { path: 'path/to/file' }
54 })
55}
56
57{
58 interface SomeOptions extends Uppy.PluginOptions {
59 types: 'are checked'
60 }
61 class SomePlugin extends Uppy.Plugin<SomeOptions> {}
62 const untypedUppy = Uppy()
63 untypedUppy.use(SomePlugin, { types: 'are unchecked' })
64 const typedUppy = Uppy<Uppy.StrictTypes>()
65 expectError(typedUppy.use(SomePlugin, { types: 'are unchecked' }))
66 typedUppy.use(SomePlugin, { types: 'are checked' })
67
68 // strictly-typed instance can be cast to a loosely-typed instance
69 const widenUppy: Uppy.Uppy = Uppy<Uppy.StrictTypes>()
70 // and disables the type checking
71 widenUppy.use(SomePlugin, { random: 'nonsense' })
72}
73
74{
75 const uppy = Uppy()
76 // can emit events with internal event types
77 uppy.emit('upload')
78 uppy.emit('complete', () => {})
79 uppy.emit('error', () => {})
80
81 // can emit events with custom event types
82 uppy.emit('dashboard:modal-closed', () => {})
83
84 // can register listners for internal events
85 uppy.on('upload', () => {})
86 uppy.on('complete', () => {})
87 uppy.on('error', () => {})
88
89 // can register listners on custom events
90 uppy.on('dashboard:modal-closed', () => {})
91}
92
93{
94 const uppy = Uppy()
95 uppy.setOptions({
96 restrictions: {
97 allowedFileTypes: ['.png']
98 }
99 })
100 expectError(uppy.setOptions({ restrictions: false }))
101 expectError(uppy.setOptions({ unknownKey: false }))
102}
103
104{
105 interface TestOptions extends Uppy.PluginOptions {
106 testOption: string
107 }
108 class TestPlugin extends Uppy.Plugin<TestOptions> {}
109
110 const strict = Uppy<Uppy.StrictTypes>().use(TestPlugin, { testOption: 'hello' })
111 ;(strict.getPlugin('TestPlugin') as TestPlugin).setOptions({ testOption: 'world' })
112 expectError((strict.getPlugin('TestPlugin') as TestPlugin).setOptions({ testOption: 0 }))
113 expectError((strict.getPlugin('TestPlugin') as TestPlugin).setOptions({ unknownKey: false }))
114}