Source

depresso / test / resolve.coffee

Full commit
{testing,resolve} = require '../depresso'
{data} = require './sample/data'
spahql = require 'spahql'
_ = require 'underscore'

@resolve = (assert) ->
  deps = [
    target: '/products//price'
    depends: 
      netPrice: '../netPrice'
      tax: '../tax'
    calculate: ->
      @netPrice * (1 + @tax)
  ,
    target: '/general/price'
    depends:
      prices: '/products//price'
    context:
      discount: 1
    calculate: ->
      sum = _.reduce(
        @prices
        (x,y) -> x+y
        0)
      sum - @discount
  ]

  result = resolve data, deps, '/general/price', '/products/*/price'
  expected =
    general:
      discount: 0.1
      price: 33
    products: [
      name: 'Bag'
      netPrice: 20
      tax: 0.1
      price: 22
    ,
      name: 'Beer'
      netPrice: 10
      tax: 0.2
      price: 12
    ] 
  assert.deepEqual expected, result
  assert.done()

@fibonacci = (assert) ->
  data = nums: [{},{}]
  data.nums[0].index = 1
  data.nums[1].index = 1
  deps = [
    target: '/nums/*/index'
    depends:
      prev: '../</index'
      prev2: '../</</index'
    calculate: ->
      @prev + @prev2 
  ]
  result = resolve data, deps, '/nums/6/index'
  assert.deepEqual [1,1,2,3,5,8,13], _(result.nums).map (x) -> x.index
  assert.done()