Commit 666e3ec9 authored by Mycha de Vrees's avatar Mycha de Vrees
Browse files

fixed unittests

parent 5f45d6dc
Pipeline #351 passed with stage
in 1 minute and 25 seconds
......@@ -48,6 +48,16 @@ const getDefaultState = () => ({
error: null
})
function notifyError ({vm, error}) {
vm.$notify({
message: error.message,
icon: 'add_alert',
horizontalAlign: 'center',
verticalAlign: 'top',
type: 'danger'
})
}
/**
* Defines all module actions
* @type {object}
......@@ -78,13 +88,7 @@ const actions = {
commit(AUTHORS_FETCH_SUCCESS, { authors, count })
commit(AUTHORS_FETCHING, false, null)
} catch (error) {
vm.$notify({
message: error,
icon: 'add_alert',
horizontalAlign: 'center',
verticalAlign: 'top',
type: 'danger'
})
notifyError({vm, error})
commit(AUTHORS_FETCHING, false, error)
}
},
......@@ -97,13 +101,7 @@ const actions = {
commit(AUTHOR_FETCH_SUCCESS, { current: author })
commit(AUTHORS_FETCHING, false, null)
} catch (error) {
vm.$notify({
message: error,
icon: 'add_alert',
horizontalAlign: 'center',
verticalAlign: 'top',
type: 'danger'
})
notifyError({vm, error})
commit(AUTHORS_FETCHING, false, error)
}
},
......@@ -117,13 +115,7 @@ const actions = {
commit(AUTHOR_FETCH_BOOKS_SUCCESS, { books })
commit(AUTHORS_FETCHING, false, null)
} catch (error) {
vm.$notify({
message: error,
icon: 'add_alert',
horizontalAlign: 'center',
verticalAlign: 'top',
type: 'danger'
})
notifyError({vm, error})
commit(AUTHORS_FETCHING, false, error)
}
}
......
......@@ -56,7 +56,7 @@ const getDefaultState = () => ({
function notifyError ({vm, error}) {
vm.$notify({
message: error,
message: error.message,
icon: 'add_alert',
horizontalAlign: 'center',
verticalAlign: 'top',
......
import Transformer from './Transformer'
function fetchMultipleAuthors (response) {
const authors = []
response.data.data.map((author) => {
const obj = {
id: author.id,
...author.attributes
}
authors.push(obj)
})
return authors
}
function fetchSingleAuthor (response) {
return {
id: response.data.data.id,
...response.data.data.attributes
}
}
/**
* AuthorTransformer class
*/
......@@ -10,24 +29,13 @@ class AuthorTransformer extends Transformer {
* @returns {*}
*/
static fetch (response) {
const authors = []
// check if we fetch all authors
// .map doesnt work on objects, only arrays
if (Array.isArray(response.data.data)) {
response.data.data.map((author) => {
const obj = {
id: author.id,
...author.attributes
}
authors.push(obj)
})
return authors
return fetchMultipleAuthors(response)
}
// or just fetch a single author.
return {
id: response.data.data.id,
...response.data.data.attributes
}
return fetchSingleAuthor(response)
}
static fetchAuthorBooks (response) {
const books = []
......
......@@ -9,15 +9,19 @@ function fetchMultipleBooks (response) {
id: book.id,
...book.attributes
}
book.relationships.authors.data.map((author) => {
const foundAuthor = _.find(response.data.included, (included) => included.type === 'author' && included.id === author.id)
const authorObj = {
id: foundAuthor.id,
name: foundAuthor.attributes.name
if (Object.hasOwnProperty.call(book, 'relationships')) {
if (Object.hasOwnProperty.call(book.relationships, 'authors')) {
book.relationships.authors.data.map((author) => {
const foundAuthor = _.find(response.data.included, (included) => included.type === 'author' && included.id === author.id)
const authorObj = {
id: foundAuthor.id,
name: foundAuthor.attributes.name
}
authors.push(authorObj)
obj.authors = authors
})
}
authors.push(authorObj)
obj.authors = authors
})
}
books.push(obj)
})
return books
......
......@@ -11,9 +11,28 @@ describe('BookTransformer fetch', () => {
language: null,
serie_id: null,
sortable_title: null,
title: "Het kraaienmeisje"
title: 'Het kraaienmeisje'
},
relationships: {
authors: {
data: [
{
id: 1,
name: 'Test author'
}
]
}
}
}
],
included: [
{
id: 1,
attributes: {
title: 'Test author'
},
type: 'author'
}
]
}
}
......@@ -21,5 +40,4 @@ describe('BookTransformer fetch', () => {
it('should contain the correct data format', () => {
expect(transform[0].id).toEqual(1)
})
})
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