/
AssemblyOptions.test.js
137 lines (122 loc) · 3.98 KB
/
AssemblyOptions.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { describe, expect, it } from '@jest/globals'
import AssemblyOptions from './AssemblyOptions.js'
describe('Transloadit/AssemblyOptions', () => {
it('Validates response from getAssemblyOptions()', async () => {
const options = new AssemblyOptions([
{ name: 'testfile' },
], {
getAssemblyOptions: (file) => {
expect(file.name).toBe('testfile')
return {
params: '{"some":"json"}',
}
},
})
await expect(options.build()).rejects.toThrow(
/The `params\.auth\.key` option is required/,
)
})
it('Uses different assemblies for different params', async () => {
const data = new Uint8Array(10)
data.size = data.byteLength
const options = new AssemblyOptions([
{ name: 'a.png', data },
{ name: 'b.png', data },
{ name: 'c.png', data },
{ name: 'd.png', data },
], {
getAssemblyOptions: (file) => ({
params: {
auth: { key: 'fake key' },
steps: {
fake_step: { data: file.name },
},
},
}),
})
const assemblies = await options.build()
expect(assemblies).toHaveLength(4)
expect(assemblies[0].options.params.steps.fake_step.data).toBe('a.png')
expect(assemblies[1].options.params.steps.fake_step.data).toBe('b.png')
expect(assemblies[2].options.params.steps.fake_step.data).toBe('c.png')
expect(assemblies[3].options.params.steps.fake_step.data).toBe('d.png')
})
it('Should merge files with same parameters into one Assembly', async () => {
const data = new Uint8Array(10)
const data2 = new Uint8Array(20)
const options = new AssemblyOptions([
{ name: 'a.png', data, size: data.byteLength },
{ name: 'b.png', data, size: data.byteLength },
{ name: 'c.png', data, size: data.byteLength },
{ name: 'd.png', data: data2, size: data2.byteLength },
], {
getAssemblyOptions: (file) => ({
params: {
auth: { key: 'fake key' },
steps: {
fake_step: { data: file.size },
},
},
}),
})
const assemblies = await options.build()
expect(assemblies).toHaveLength(2)
expect(assemblies[0].fileIDs).toHaveLength(3)
expect(assemblies[1].fileIDs).toHaveLength(1)
expect(assemblies[0].options.params.steps.fake_step.data).toBe(10)
expect(assemblies[1].options.params.steps.fake_step.data).toBe(20)
})
it('Does not create an Assembly if no files are being uploaded', async () => {
const options = new AssemblyOptions([], {
getAssemblyOptions () {
throw new Error('should not create Assembly')
},
})
await expect(options.build()).resolves.toEqual([])
})
it('Creates an Assembly if no files are being uploaded but `alwaysRunAssembly` is enabled', async () => {
const options = new AssemblyOptions([], {
alwaysRunAssembly: true,
getAssemblyOptions (file) {
expect(file).toBe(null)
return {
params: {
auth: { key: 'fake key' },
template_id: 'example',
},
}
},
})
await expect(options.build()).resolves.toHaveLength(1)
})
it('Collects metadata if `fields` is an array', async () => {
function defaultGetAssemblyOptions (file, options) {
return {
params: options.params,
signature: options.signature,
fields: options.fields,
}
}
const options = new AssemblyOptions([{
id: 1,
meta: { watermark: 'Some text' },
}, {
id: 2,
meta: { watermark: 'ⓒ Transloadit GmbH' },
}], {
fields: ['watermark'],
params: {
auth: { key: 'fake key' },
},
getAssemblyOptions: defaultGetAssemblyOptions,
})
const assemblies = await options.build()
expect(assemblies).toHaveLength(2)
expect(assemblies[0].options.fields).toMatchObject({
watermark: 'Some text',
})
expect(assemblies[1].options.fields).toMatchObject({
watermark: 'ⓒ Transloadit GmbH',
})
})
})