Skip to content

Commit c898380

Browse files
committed
fix(core): Added processor tests
1 parent ec550d9 commit c898380

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export function normaliseArguments<T>(
201201
* Function used to process file uploads.
202202
*
203203
*/
204-
function processor<T>(uploadHandler: IUploadHandler<T>) {
204+
export function processor<T>(uploadHandler: IUploadHandler<T>) {
205205
return function({
206206
argumentName,
207207
upload,
@@ -213,7 +213,7 @@ function processor<T>(uploadHandler: IUploadHandler<T>) {
213213
argumentName: argumentName,
214214
upload: res,
215215
}))
216-
} else if (upload !== undefined) {
216+
} else if (upload !== undefined && upload !== null) {
217217
return upload.then(uploadHandler).then(res => ({
218218
argumentName: argumentName,
219219
upload: res,

test.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
isGraphQLArgumentType,
99
uploadTypeIdentifier,
1010
normaliseArguments,
11+
processor as uploadPorcessor,
1112
} from './'
1213

1314
// Helpers
@@ -435,3 +436,72 @@ test('Normalizes response correctly', async t => {
435436
arg2: { value: 'z' },
436437
})
437438
})
439+
440+
test('Processor handles single file correctly', async t => {
441+
const uploadHandler = ({ stream, filename, mimetype, encoding }) =>
442+
new Promise(resolve =>
443+
setTimeout(
444+
() => resolve(`${stream}${filename}${mimetype}${encoding}`),
445+
10,
446+
),
447+
)
448+
449+
const res = await uploadPorcessor(uploadHandler)({
450+
argumentName: 'test',
451+
upload: new Promise(resolve =>
452+
resolve({ stream: 's', filename: 'f', mimetype: 'm', encoding: 'e' }),
453+
),
454+
})
455+
456+
t.deepEqual(res, {
457+
argumentName: 'test',
458+
upload: 'sfme',
459+
})
460+
})
461+
462+
test('Processor handles multiple files correctly', async t => {
463+
const uploadHandler = ({ stream, filename, mimetype, encoding }) =>
464+
new Promise(resolve =>
465+
setTimeout(
466+
() => resolve(`${stream}${filename}${mimetype}${encoding}`),
467+
10,
468+
),
469+
)
470+
471+
const file = x =>
472+
new Promise(resolve =>
473+
resolve({
474+
stream: `s${x}`,
475+
filename: `f${x}`,
476+
mimetype: `m${x}`,
477+
encoding: `e${x}`,
478+
}),
479+
)
480+
481+
const res = await uploadPorcessor(uploadHandler)({
482+
argumentName: 'test',
483+
upload: [file(1), file(2)],
484+
})
485+
486+
t.deepEqual(res, {
487+
argumentName: 'test',
488+
upload: ['s1f1m1e1', 's2f2m2e2'],
489+
})
490+
})
491+
492+
test('Processor handles no file correctly', async t => {
493+
const uploadHandler = ({ stream, filename, mimetype, encoding }) =>
494+
new Promise(resolve =>
495+
setTimeout(
496+
() => resolve(`${stream}${filename}${mimetype}${encoding}`),
497+
10,
498+
),
499+
)
500+
501+
const res = await uploadPorcessor(uploadHandler)({
502+
argumentName: 'test',
503+
upload: null,
504+
})
505+
506+
t.is(res, null)
507+
})

0 commit comments

Comments
 (0)