简体   繁体   中英

Knockout ViewModel instance returning empty array Jasmine Rails Coffeescript

I'm trying to write a jasmine spec for my Knockout ViewModel, but it returns an empty array, even though when I actually apply the databindings it works. I'm not sure if this is because of how I structured my Coffeescript, since I namespaced everything.

jasmine spec:

describe 'knockout', ->
  it 'computes available timeslots based on date', ->
    target = new HEALTHFUND.AppointmentViewModel()
    target.date('2014/02/15')
    expect(target.available_timeslots()).toBe([])
    target.date('2014/02/16')
    expect(target.available_timeslots()).toBe(target.sunday_timeslots)

ViewModel:

ready = ->
  # HEALTHFUND.AppointmentViewModel = {}
  HEALTHFUND.AppointmentViewModel = (->

    @date = ko.observable()

    @weekday_timeslots = ['10 am - 12 pm', '12 pm - 2 pm', '2 pm - 4 pm', '4 pm - 6 pm', '6 pm - 9 pm', '9 pm - 12 am']
    @sunday_timeslots = ['12 pm - 2 pm', '2 pm - 4 pm', '4 pm - 6 pm', '6 pm - 8 pm']
    @available_timeslots = ko.computed =>
      input_date = new Date(@date()).getDay()

      if input_date == 0
        return @sunday_timeslots
      else if input_date < 6
        return @weekday_timeslots
      else
        return []

  )()

$(document).ready ready
$(document).on "page:load", ready

It was actually just because I forgot to add a "return this" at the end of the ViewModel. Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM