import expect from 'expect'
import model from '../lib/model'
const FORM_SCHEMA = [
  'name',
  'color'
]

describe('reducer', () => {

  it('will process a schema', () => {
    
    var schema = model.process_schema(FORM_SCHEMA)

    expect(schema).toEqual([
      {
        "name": "name",
        "type": "text",
        "title": "Name"
      },
      {
        "name": "color",
        "type": "text",
        "title": "Color"
      }
    ])

  })

  it('will genetate some meta data', () => {

    var schema = [{
      name:'color',
      validate:function(v){
        return v.indexOf('r')==0 ? null : 'color must start with r'
      }
    },{
      name:'height'
    }]

    var validate = function(data, meta){
      var ret = {}
      if(data.color=='red' && data.height<10){
        ret.height = 'red things must be more than 10 tall'
      }
      return ret
    }

    var data1 = {
      color:'red',
      height:5
    }

    var meta1 = model.generate_meta(null, data1, schema, validate)

    expect(meta1).toEqual({
      "valid": false,
      "dirty": false,
      "fields": {
        "color": {
          "valid": true,
          "dirty": false
        },
        "height": {
          "valid": false,
          "dirty": false,
          "error": "red things must be more than 10 tall"
        }
      }
    })

    var data2 = {
      color:'blue'
    }

    var meta2 = model.generate_meta(null, data2, schema, validate)

    expect(meta2).toEqual({
      "valid": false,
      "dirty": false,
      "fields": {
        "color": {
          "error": "color must start with r",
          "valid": false,
          "dirty": false
        },
        "height": {
          "valid": true,
          "dirty": false
        }
      }
    })

    var data3 = {
      color:'red',
      height:20
    }

    var meta3 = model.generate_meta({
      dirty:true,
      valid:true,
      fields:{
        color:{
          valid:true,
          dirty:true
        },
        height:{
          valid:false,
          dirty:true
        }
      }
    }, data3, schema, validate)

    expect(meta3).toEqual({
      "dirty": true,
      "valid": true,
      "fields": {
        "color": {
          "valid": true,
          "dirty": true
        },
        "height": {
          "valid": true,
          "dirty": true
        }
      }
    })

  })

})

@@ -1061,32 +1061,104 @@
 %22dirty%22: false,%0A
+      %22olddata%22: %7B%0A        %22color%22: %22red%22,%0A        %22height%22: 5%0A      %7D,%0A
       %22fields%22: 
@@ -1170,32 +1170,60 @@
      %22color%22: %7B%0A
+          %22changed%22: false,%0A
           %22valid
@@ -1279,32 +1279,60 @@
     %22height%22: %7B%0A
+          %22changed%22: false,%0A
           %22valid
@@ -1631,24 +1631,76 @@
 ty%22: false,%0A
+      %22olddata%22: %7B%0A        %22color%22: %22blue%22%0A      %7D,%0A
       %22field
@@ -1716,32 +1716,60 @@
      %22color%22: %7B%0A
+          %22changed%22: false,%0A
           %22error
@@ -1872,32 +1872,60 @@
     %22height%22: %7B%0A
+          %22changed%22: false,%0A
           %22valid
@@ -2114,24 +2114,91 @@
 valid:true,%0A
+      %22olddata%22: %7B%0A        color:'red',%0A        height:20%0A      %7D,%0A
       fields
@@ -2216,16 +2216,44 @@
 color:%7B%0A
+          %22changed%22: false,%0A
         
@@ -2315,16 +2315,44 @@
 eight:%7B%0A
+          %22changed%22: false,%0A
         
@@ -2504,24 +2504,91 @@
 lid%22: true,%0A
+      %22olddata%22: %7B%0A        color:'red',%0A        height:20%0A      %7D,%0A
       %22field
@@ -2604,32 +2604,60 @@
      %22color%22: %7B%0A
+          %22changed%22: false,%0A
           %22valid
@@ -2712,32 +2712,60 @@
     %22height%22: %7B%0A
+          %22changed%22: false,%0A
           %22valid

test passing