Commit 3d588e2b authored by Jean Rabreau's avatar Jean Rabreau
Browse files

extend item store module tests

parent 3735743a
......@@ -358,15 +358,20 @@ const Item = {
itemDuplicate({commit, dispatch, getters}, target) {
const item = getters['current']
const {title: oldTitle, description} = item
const setId = target ? target.setId : item.set.id
const title = `${i18n.t('item.copy.prefix')}${oldTitle}`
const metadata = (target
? target.metadata
: item.metadata)
.map(d => d.name === 'title'
? { ...d, value: title}
: d
)
.map(d => d.name === 'title'
? { ...d, value: title}
: d
)
const setId = target ? target.setId : item.set.id
if (setId === undefined) {
throw new TypeError('setId is mandatory, see no orphan item policy')
}
const payload = {
title,
description,
......
......@@ -30,7 +30,12 @@ const set = {
templateId: 'template-uuid',
}
const thumbnails = { s: 'some.url.s', m: 'some.url.m', l: 'some.url.l' }
const thumbnails = {
s: 'some.url.s',
m: 'some.url.m',
l: 'some.url.l',
embed: 'some.url.embed'
}
const file = {
id: '54534',
......@@ -119,11 +124,40 @@ describe('store/item', () => {
}, 'specific error')
})
})
describe('itemCSVImport', () => {
let actionResponse
const csvFile = new File(
["header1,header2\nvalue1,value2"],
"items.csv",
{type: "text/csv"}
)
beforeEach(async () => {
authStub.post.returns(Promise.resolve({data: {createdItems: 5}}))
await store.dispatch('item/itemCSVImport', csvFile).then(
response => actionResponse = response
)
})
it('should post to specific item api action', () => {
expect(authStub.post.calledOnce, 'post called').to.be.true
expect(authStub.post.firstCall.args[0]).to.equal('items/import_csv/', 'api url')
})
it('should post specific formData object', () => {
const postPayload = authStub.post.firstCall.args[1]
expect(postPayload).to.be.instanceOf(FormData, 'payload class')
expect(postPayload.get('Content-Type')).to.equal("text/csv")
expect(postPayload.get('csv')).to.equal(csvFile)
expect(postPayload.get('set_id')).to.equal(setId)
})
it('should return created items count from response', () => {
expect(actionResponse).to.equal(5)
})
})
describe('itemDuplicate', () => {
const oldTitle = 'previous title'
beforeEach(() => {
store.state.item.current = {
canEdit: false,
canBeShared: false,
creator,
description: 'previous description',
id: '1536',
......@@ -167,30 +201,68 @@ describe('store/item', () => {
error: axiosError
}, 'specific error')
})
describe('item payload', () => {
let payload, title
beforeEach(() => {
store.dispatch('item/itemDuplicate')
payload = authStub.post.firstCall.args[1]
title = payload.title
})
it('should have consistant title values', () => {
const metaTitle = payload.metadata.find(d => d.name === 'title').value
expect(title === metaTitle, 'same titles').to.be.true
describe('item creation payload', () => {
describe('without given target', () => {
let payload, title
beforeEach(() => {
store.dispatch('item/itemDuplicate')
payload = authStub.post.firstCall.args[1]
title = payload.title
})
it('should have consistant title values', () => {
const metaTitle = payload.metadata.find(d => d.name === 'title').value
expect(title === metaTitle, 'same titles').to.be.true
})
it('should have prefixed titles', () => {
const prefix = i18n.t('item.copy.prefix')
expect(title.startsWith(prefix), 'title prefix').to.be.true
})
it('should refer to old title', () => {
expect(title.endsWith(oldTitle), 'title prefix').to.be.true
})
it('should let other metadata unchanged', () => {
expect(payload.metadata.filter(d => d.name !== 'title'))
.to.eql(metadata.filter(d => d.name !== 'title'))
})
it('should belong to the same set', () => {
expect(payload.setId).to.be.equal(set.id, 'same id')
})
})
it('should have prefixed titles', () => {
const prefix = i18n.t('item.copy.prefix')
expect(title.startsWith(prefix), 'title prefix').to.be.true
})
it('should refer to old title', () => {
expect(title.endsWith(oldTitle), 'title prefix').to.be.true
})
it('should let other metadata unchanged', () => {
expect(payload.metadata.filter(d => d.name !== 'title'))
.to.eql(metadata.filter(d => d.name !== 'title'))
})
it('should belong to the same set', () => {
expect(payload.setId).to.be.equal(set.id, 'same id')
describe('with given target', () => {
const duplicateInTarget = target => () => store.dispatch('item/itemDuplicate', target)
const partialMetadata = [{
name: 'title',
value: 'overridden'
}]
const launchCopy = duplicateInTarget({
metadata: partialMetadata,
setId: 'targetedSet'
})
it('should throw error if target has only setId', () => {
expect(duplicateInTarget({setId: 'x'})).to.throw(TypeError)
})
it('should throw error if target has only metadata', () => {
expect(duplicateInTarget({metadata: []})).to.throw(TypeError)
})
it('should have consistant title values', () => {
launchCopy()
const { title, metadata } = authStub.post.firstCall.args[1]
const metaTitle = metadata.find(d => d.name === 'title').value
expect(title === metaTitle, 'same titles').to.be.true
expect(title, 'item.title overrode').to.not.contain('overridden')
})
it('should post only given metadata', () => {
launchCopy()
const { metadata } = authStub.post.firstCall.args[1]
expect(metadata).to.have.lengthOf(1)
expect(Object.keys(metadata[0])).to.have.lengthOf(2)
})
it('should should create item in given set', () => {
launchCopy()
const { setId } = authStub.post.firstCall.args[1]
expect(setId).to.equal('targetedSet')
})
})
})
})
......@@ -499,6 +571,80 @@ describe('store/item', () => {
})
})
})
describe('rtiUpload', () => {
const itemId = '436a-6c03'
const file = new File(
['content'],
'rti.zip',
{type: 'application/zip'})
const mediaFile = {
id: '3674-43a6',
info: null,
}
beforeEach(() => {
store.state.item.current = {
id: itemId,
mediaFiles: [],
}
authStub.post.resolves({ data: mediaFile })
})
it('should call item api', async () => {
await store.dispatch('item/rtiUpload', file)
expect(authStub.post.calledOnce, 'api called').to.be.true
expect(authStub.post.firstCall.args[0]).to.equal(
`items/${itemId}/mediafiles/upload_rti/`,
'url check')
})
it('should post formData with file', async () => {
await store.dispatch('item/rtiUpload', file)
const payload = authStub.post.firstCall.args[1]
expect(payload).to.be.instanceOf(FormData, 'payload class')
expect(payload.get('Content-Type')).to.equal("application/zip")
expect(payload.get('file')).to.equal(file)
})
it('should add the created media file to current item', async () => {
await store.dispatch('item/rtiUpload', file)
expect(store.getters['item/files']).to.have.lengthOf(1, 'media file added')
expect(store.getters['item/files'][0].id).to.equal('3674-43a6', 'new file id')
})
it('should notify on success', async () => {
await store.dispatch('item/rtiUpload', file)
expect(dispatchSpy.calledTwice, 'extra action').to.be.true
const [action, message] = dispatchSpy.secondCall.args
expect(action).to.equal('dialog/displayInfo', 'notify info')
expect(message).to.equal(i18n.t('file.upload.success', {filename: file.name}), 'specific error')
})
it('should return 100 on success', async () => {
let actionResponse
await store.dispatch('item/rtiUpload', file)
.then(response => {
actionResponse = response
})
expect(actionResponse).to.equal(100)
})
it('should notify failure', async () => {
const axiosError = new Error('api call failed')
authStub.post.returns(Promise.reject(axiosError))
await store.dispatch('item/rtiUpload', file)
expect(dispatchSpy.calledTwice, 'extra action').to.be.true
const [action, {error, message}] = dispatchSpy.secondCall.args
expect(action).to.equal('dialog/displayError', 'notify error')
expect(message).to.equal(
i18n.t('file.upload.error', {filename: file.name}),
'specific error message')
expect(error).to.equal(axiosError, 'display error content')
})
it('should return 0 on failure', async () => {
authStub.post.returns(Promise.reject())
let actionResponse
await store.dispatch('item/rtiUpload', file)
.then(response => {
actionResponse = response
})
expect(actionResponse).to.equal(0)
})
})
describe('setItemsLoad', () => {
it('should call retrieve list api', () => {
store.dispatch('item/setItemsLoad', {size: 11})
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment