import * as forms from './forms'
import fetchMock from 'fetch-mock'

describe('entity-browser', () => {
  describe('util', () => {
    describe('api', () => {
      describe('forms', () => {
        beforeEach(() => {
          fetchMock.reset()
          fetchMock.restore()
        })

        describe('getFieldsOfDetailForm', () => {
          it('get array of fields', () => {
            const formDefinition = {
              name: 'fromX',
              children: [
                {
                  type: 'ch.tocco.nice2.model.form.components.layout.HorizontalBox',
                  name: 'box 1',
                  children: [
                    {
                      type: 'ch.tocco.nice2.model.form.components.layout.HorizontalBox1',
                      name: 'box 2',
                      children: [
                        {
                          name: 'firstname',
                          type: 'ch.tocco.nice2.model.form.components.simple.TextArea'
                        },
                        {
                          name: 'lastname',
                          type: 'ch.tocco.nice2.model.form.components.simple.TextArea'
                        }
                      ]
                    }
                  ]
                },
                {
                  name: 'xyz',
                  type: 'ch.tocco.nice2.model.form.components.simple.SomeType'
                }
              ]
            }

            const fields = forms.getFieldsOfDetailForm(formDefinition)
            expect(fields).to.eql(['firstname', 'lastname', 'xyz'])
          })
        })

        describe('searchFormTransformer', () => {
          it('should return an array of fields', () => {
            const fetchResult = require('../../dev/test-data/user_search.json')

            const result = forms.searchFormTransformer(fetchResult)
            const expectedResult = [
              {
                name: 'txtFulltext',
                type: 'ch.tocco.nice2.model.form.components.simple.TextField',
                displayType: 'EDITABLE',
                label: 'Person',
                useLabel: 'YES'
              }
            ]
            expect(result).to.eql(expectedResult)
          })
        })

        describe('columnDefinitionTransformer', () => {
          it('should return an array of label and list of fields ', () => {
            const fetchResult = {
              form: {
                children: [{
                  name: 'table',
                  children: [
                    {
                      displayType: 'EDITABLE',
                      label: 'label1',
                      children: [{name: 'name1', type: 'type', displayType: 'EDITABLE', label: 'label'}]
                    }, {
                      displayType: 'HIDDEN',
                      label: 'label2',
                      children: [{name: 'name2', type: 'type', displayType: 'HIDDEN', label: 'label'}]
                    }, {
                      displayType: 'EDITABLE',
                      label: 'label3',
                      children: [{name: 'custom:name3', type: 'type', displayType: 'EDITABLE', label: 'label'}]
                    }, {
                      displayType: 'EDITABLE',
                      label: 'label4',
                      children: [{
                        name: 'name4',
                        type: 'ch.tocco.nice2.model.form.components.action.Action',
                        displayType: 'EDITABLE',
                        label: 'label'
                      }]
                    }, {
                      displayType: 'EDITABLE',
                      label: 'label5',
                      children: [{name: 'name5', type: 'type', displayType: 'EDITABLE', label: 'label'}]
                    }
                  ]
                }]
              }
            }
            const result = forms.columnDefinitionTransformer(fetchResult)

            const expectedColumnDefinition = [
              {label: 'label1', value: ['name1']},
              {label: 'label3', value: []},
              {label: 'label4', value: []},
              {label: 'label5', value: ['name5']}
            ]
            expect(result).to.eql(expectedColumnDefinition)
          })
        })

        describe('fetchForm', () => {
          it('should call fetch ', () => {
            fetchMock.get('*', {})
            return forms.fetchForm('User_search').then(res => {
              expect(fetchMock.calls().matched).to.have.length(1)
              const lastCallUrl = fetchMock.lastCall()[0]
              expect(lastCallUrl).to.eql('/nice2/rest/forms/User_search')
            })
          })
        })
      })
    })
  })
})

@@ -356,32 +356,74 @@
  array of fields
+ and ignore not simple types and iterators
 ', () =%3E %7B%0A     
@@ -1192,32 +1192,218 @@
 imple.TextArea'%0A
+                        %7D,%0A                        %7B%0A                          name: 'relAffiliation',%0A                          type: 'ch.tocco.nice2.model.form.components.table.Table'%0A
                 
@@ -1625,16 +1625,389 @@
 meType'%0A
+                %7D,%0A                %7B%0A                  name: 'relTask_note',%0A                  type: 'ch.tocco.nice2.model.form.components.navigation.IteratorComponent',%0A                  children: %5B%0A                    %7B%0A                      name: 'iteratorField',%0A                      type: 'ch.tocco.nice2.model.form.components.simple.TextArea'%0A                    %7D%5D%0A
         

write better tests for _paths getter