Skip to content

Commit

Permalink
fix(core): Added processor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maticzav committed Jul 11, 2018
1 parent ec550d9 commit c898380
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export function normaliseArguments<T>(
* Function used to process file uploads.
*
*/
function processor<T>(uploadHandler: IUploadHandler<T>) {
export function processor<T>(uploadHandler: IUploadHandler<T>) {
return function({
argumentName,
upload,
Expand All @@ -213,7 +213,7 @@ function processor<T>(uploadHandler: IUploadHandler<T>) {
argumentName: argumentName,
upload: res,
}))
} else if (upload !== undefined) {
} else if (upload !== undefined && upload !== null) {
return upload.then(uploadHandler).then(res => ({
argumentName: argumentName,
upload: res,
Expand Down
70 changes: 70 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
isGraphQLArgumentType,
uploadTypeIdentifier,
normaliseArguments,
processor as uploadPorcessor,
} from './'

// Helpers
Expand Down Expand Up @@ -435,3 +436,72 @@ test('Normalizes response correctly', async t => {
arg2: { value: 'z' },
})
})

test('Processor handles single file correctly', async t => {
const uploadHandler = ({ stream, filename, mimetype, encoding }) =>
new Promise(resolve =>
setTimeout(
() => resolve(`${stream}${filename}${mimetype}${encoding}`),
10,
),
)

const res = await uploadPorcessor(uploadHandler)({
argumentName: 'test',
upload: new Promise(resolve =>
resolve({ stream: 's', filename: 'f', mimetype: 'm', encoding: 'e' }),
),
})

t.deepEqual(res, {
argumentName: 'test',
upload: 'sfme',
})
})

test('Processor handles multiple files correctly', async t => {
const uploadHandler = ({ stream, filename, mimetype, encoding }) =>
new Promise(resolve =>
setTimeout(
() => resolve(`${stream}${filename}${mimetype}${encoding}`),
10,
),
)

const file = x =>
new Promise(resolve =>
resolve({
stream: `s${x}`,
filename: `f${x}`,
mimetype: `m${x}`,
encoding: `e${x}`,
}),
)

const res = await uploadPorcessor(uploadHandler)({
argumentName: 'test',
upload: [file(1), file(2)],
})

t.deepEqual(res, {
argumentName: 'test',
upload: ['s1f1m1e1', 's2f2m2e2'],
})
})

test('Processor handles no file correctly', async t => {
const uploadHandler = ({ stream, filename, mimetype, encoding }) =>
new Promise(resolve =>
setTimeout(
() => resolve(`${stream}${filename}${mimetype}${encoding}`),
10,
),
)

const res = await uploadPorcessor(uploadHandler)({
argumentName: 'test',
upload: null,
})

t.is(res, null)
})

0 comments on commit c898380

Please sign in to comment.