DOMination

The function selectLocalTimezone(), only six lines long and shown in Listing 15-2, forced me to bring in some additional techniques. This function sets the selected value of a selection list representing time zone choices to that of the current or specified time zone. This small piece of code uses jQuery to operate on the DOM.

Listing 15-2: A small method using jQuery to operate on the DOM. The timezone_select property is supposed to be a jQuery collection referencing a <select> tag.

var selectLocalTimezone = function(tp_inst, date) {
  if (tp_inst && tp_inst.timezone_select) {
    var now = date || new Date();
    tp_inst.timezone_select.val(-now.getTimezoneOffset());
  }
};

I had originally set up the Jasmine spec runner to include jasmine-jquery,5 confusing it with jasmine-fixture.6 My next action was to correct that [f018316]. The jasmine-fixture module lets me define DOM fixtures based on jQuery selector syntax that get cleaned up automatically. I used it to set up a structure like Timepicker expected for the operation of the method and tested to ensure that the right value was selected [34e2ee2], partially shown in Listing 15-3.

5. https://github.com/velesin/jasmine-jquery

6. https://github.com/searls/jasmine-fixture

Listing 15-3: A test using jasmine-fixture to set up the DOM as expected

describe('selectLocalTimezone', function() {
  var timepicker,
    timezoneOffset,
    defaultTimezoneOffset;

  beforeEach(function() {
    timepicker = {
      timezone_select: affix('select')
    };
    var now = new Date();
      timezoneOffset = String(-now.getTimezoneOffset());
      defaultTimezoneOffset = String(timezoneOffset - 60);
    timepicker.timezone_select.affix('option')
      .text(defaultTimezoneOffset);
    timepicker.timezone_select.affix('option')
      .text(timezoneOffset);
    timepicker.timezone_select.affix('option')
      .text(timezoneOffset + 60);
  });

  it('should select the current timezone with a valid ' +
      'timezone_select and a date', function() {
    util._selectLocalTimezone(timepicker, new Date());

    expect(timepicker.timezone_select.val())
      .toBe(timezoneOffset);
  });
});

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.186.79