UNPKG

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