import test from 'ava'
import jsonTransform from '../../tests/helpers/resources/transformers/jsonTransform'

import createRequestMapper from './createRequestMapper'

test('should return null when no mapping', (t) => {
  const endpoint = {}

  const ret = createRequestMapper(endpoint)

  t.is(ret, null)
})

test('should create mapper with requestMapping path', (t) => {
  const endpoint = { requestMapping: 'content.items' }
  const request = {
    data: [{ id: 'ent1', type: 'entry' }]
  }
  const expected = { content: { items: [{ id: 'ent1', type: 'entry' }] } }

  const ret = createRequestMapper(endpoint)(request)

  t.deepEqual(ret, expected)
})

test('should create mapper with requestMapping object', (t) => {
  const endpoint = { requestMapping: { 'meta.section': 'params.section' } }
  const request = {
    data: [{ id: 'ent1', type: 'entry' }],
    params: { section: 'news' }
  }
  const expected = { meta: { section: 'news' } }

  const ret = createRequestMapper(endpoint)(request)

  t.deepEqual(ret, expected)
})

test('should create mapper with transform function', (t) => {
  const transformers = { jsonTransform }
  const endpoint = {
    requestMapping: {
      'content': {
        path: 'data',
        transform: 'jsonTransform'
      }
    }
  }
  const request = {
    data: [{ id: 'ent1', type: 'entry' }],
    params: { section: 'news' }
  }
  const expected = { content: JSON.stringify([{ id: 'ent1', type: 'entry' }]) }

  const ret = createRequestMapper(endpoint, { transformers })(request)

  t.deepEqual(ret, expected)
})

@@ -725,32 +725,36 @@
 nst endpoint = %7B
+%0A   
  requestMapping:
@@ -755,16 +755,22 @@
 pping: %7B
+%0A     
  'meta.s
@@ -794,18 +794,66 @@
 section'
+,%0A      'meta.@prefix:attr': 'params.attr'%0A    %7D%0A
  
-%7D
  %7D%0A  con
@@ -931,32 +931,43 @@
  section: 'news'
+, attr: '1'
  %7D%0A  %7D%0A  const e
@@ -1001,16 +1001,37 @@
 : 'news'
+, '@prefix:attr': '1'
  %7D %7D%0A%0A  

Test mapping with @ and : in key