This commit is contained in:
Matt Rider 2019-05-24 08:48:52 -03:00
parent 51d25c1c9a
commit 39d4771ce1
9 changed files with 51 additions and 44 deletions

View File

@ -36,6 +36,6 @@ export const resolvers = {
...socialMedia.Mutation,
...notifications.Mutation,
...comments.Mutation,
...users.Mutation
}
...users.Mutation,
},
}

View File

@ -1,7 +1,7 @@
import cheerio from 'cheerio'
const ID_REGEX = /\/profile\/([\w\-.!~*'"(),]+)/g
export default function (content) {
export default function(content) {
if (!content) return []
const $ = cheerio.load(content)
const urls = $('.mention')

View File

@ -7,10 +7,10 @@ const storeUpload = ({ createReadStream, fileLocation }) =>
createReadStream()
.pipe(createWriteStream(`public${fileLocation}`))
.on('finish', resolve)
.on('error', reject)
.on('error', reject),
)
export default async function fileUpload (params, { file, url }, uploadCallback = storeUpload) {
export default async function fileUpload(params, { file, url }, uploadCallback = storeUpload) {
const upload = params[file]
if (upload) {

View File

@ -10,8 +10,8 @@ describe('fileUpload', () => {
filename: 'avatar.jpg',
mimetype: 'image/jpeg',
encoding: '7bit',
createReadStream: jest.fn()
}
createReadStream: jest.fn(),
},
}
uploadCallback = jest.fn()
})
@ -34,21 +34,30 @@ describe('fileUpload', () => {
})
it('creates a url safe name', async () => {
params.uploadAttribute.filename = '/path/to/awkward?/ file-location/?foo- bar-avatar.jpg?foo- bar'
params.uploadAttribute.filename =
'/path/to/awkward?/ file-location/?foo- bar-avatar.jpg?foo- bar'
await fileUpload(params, { file: 'uploadAttribute', url: 'attribute' }, uploadCallback)
expect(params.attribute).toMatch(/^\/uploads\/\d+-foo-bar-avatar$/)
})
describe('in case of duplicates', () => {
it('creates unique names to avoid overwriting existing files', async () => {
const { attribute: first } = await fileUpload({
...params
}, { file: 'uploadAttribute', url: 'attribute' }, uploadCallback)
const { attribute: first } = await fileUpload(
{
...params,
},
{ file: 'uploadAttribute', url: 'attribute' },
uploadCallback,
)
await new Promise(resolve => setTimeout(resolve, 1000))
const { attribute: second } = await fileUpload({
...params
}, { file: 'uploadAttribute', url: 'attribute' }, uploadCallback)
const { attribute: second } = await fileUpload(
{
...params,
},
{ file: 'uploadAttribute', url: 'attribute' },
uploadCallback,
)
expect(first).not.toEqual(second)
})
})

View File

@ -10,6 +10,6 @@ export default {
CreateUser: async (object, params, context, resolveInfo) => {
params = await fileUpload(params, { file: 'avatarUpload', url: 'avatar' })
return neo4jgraphql(object, params, context, resolveInfo, false)
}
}
},
},
}

View File

@ -67,7 +67,7 @@ describe('users', () => {
it('with no name', async () => {
const variables = {
id: 'u47',
name: null
name: null,
}
const expected = 'Username must be at least 3 characters long!'
await expect(client.request(mutation, variables)).rejects.toThrow(expected)

View File

@ -18,10 +18,10 @@ import gql from 'graphql-tag'
export default {
components: {
vueDropzone
vueDropzone,
},
props: {
user: { type: Object, default: null }
user: { type: Object, default: null },
},
data() {
return {
@ -29,20 +29,18 @@ export default {
url: this.vddrop,
maxFilesize: 0.5,
previewTemplate: this.template(),
dictDefaultMessage: ''
}
dictDefaultMessage: '',
},
}
},
computed: {
backgroundImage() {
const { avatar } = this.user || {}
const userAvatar = avatar.startsWith('/')
? avatar.replace('/', '/api/')
: avatar
const userAvatar = avatar.startsWith('/') ? avatar.replace('/', '/api/') : avatar
return {
backgroundImage: `url(${userAvatar})`
backgroundImage: `url(${userAvatar})`,
}
}
},
},
methods: {
template() {
@ -88,15 +86,15 @@ export default {
`,
variables: {
avatarUpload,
id: this.user.id
}
id: this.user.id,
},
})
.then(() => {
this.$toast.success(this.$t('user.avatar.submitted'))
})
.catch(error => this.$toast.error(error.message))
}
}
},
},
}
</script>
<style>

View File

@ -16,22 +16,22 @@ describe('Upload', () => {
mutate: jest
.fn()
.mockResolvedValueOnce({
data: { UpdateUser: { id: 'upload1', avatar: '/upload/avatar.jpg' } }
data: { UpdateUser: { id: 'upload1', avatar: '/upload/avatar.jpg' } },
})
.mockRejectedValue({
message: 'File upload unsuccessful! Whatcha gonna do?'
})
message: 'File upload unsuccessful! Whatcha gonna do?',
}),
},
$toast: {
success: jest.fn(),
error: jest.fn()
}
error: jest.fn(),
},
}
const propsData = {
user: {
avatar: '/api/generic.jpg'
}
avatar: '/api/generic.jpg',
},
}
const file = {
@ -39,17 +39,17 @@ describe('Upload', () => {
previewElement: {
classList: {
remove: jest.fn(),
add: jest.fn()
add: jest.fn(),
},
querySelectorAll: jest.fn().mockReturnValue([
{
alt: '',
style: {
'background-image': '/api/generic.jpg'
}
}
])
}
'background-image': '/api/generic.jpg',
},
},
]),
},
}
const dataUrl = 'avatar.jpg'

View File

@ -344,7 +344,7 @@ export default {
HcLoadMore,
HcEmpty,
ContentMenu,
HcUpload
HcUpload,
},
transition: {
name: 'slide-up',