mercredi 31 août 2016

Karma tests failing on code that fixed a previous bug

I made some bug fixes on my code earlier which caused some UI elements to render before data has been loaded completely into the variable. My fix was to render the UI after callback, meaning, data is completely loaded.

Here's the old code:

    @autofindField = (element) =>
      listener = @listeners[element.id]
      availableFields = @elementFields[element.id]
      filterFieldName = listener.field || @filter.field?.name || @filter.dimension
      if listener.on && _.findWhere(availableFields, {name: filterFieldName})
        listener.field = filterFieldName
      else
        delete listener.field

The fixed code:

@autofindField = (element) =>
  listener = @listeners[element.id]
  # Get baseView after autofindField is triggered in order to begin loading of
  # data from the parent directive
  @getBaseView(element.look?.query.model, element.look?.query.view, (baseView) =>
    # On callback, load the option fields, which gets data from the parent directive of baseView
    availableFields = baseView.allFields
    filterFieldName = listener.field || @filter.field?.name || @filter.dimension
    if listener.on && _.findWhere(availableFields, {name: filterFieldName})
      listener.field = filterFieldName
    else
      delete listener.field
  )

The fixed code indeed fixes the issue, however, it seems my Karma tests are failing, and I have no idea why.

Test #1 tests whether autofindField should set field to filter field name:

  it "should set field to filter field name", ->
    $scope.filter =
      title: "Brand Name"
      field:
        name: "products.brand_name"
      name: "Brand Name"
      model: "thelook"
      type: "field_filter"
    $scope.$digest()
    controller.elementFields = {
      2: [{name: "products.brand_name"}]
    }
    controller.toggleListener(elmWithoutLook)
    console.log 'ELM', controller.listeners[elmWithoutLook.id].field
    console.log 'EXP', controller.filter.field.name
    console.log 'LISTENERS', controller.listeners[elmWithoutLook.id]
    console.log 'FILTER', controller.filter
    expect(controller.listeners[elmWithoutLook.id].field).toBe(controller.filter.field.name)

This is the following test:

  it "should set field to filter field name", ->
    $scope.filter =
      title: "Brand Name"
      field:
        name: "products.brand_name"
      name: "Brand Name"
      model: "thelook"
      type: "field_filter"
    $scope.$digest()
    controller.elementFields = {
      2: [{name: "products.brand_name"}]
    }
    controller.toggleListener(elmWithoutLook)
    console.log 'ELM', controller.listeners[elmWithoutLook.id].field
    console.log 'EXP', controller.filter.field.name
    console.log 'LISTENERS', controller.listeners[elmWithoutLook.id]
    console.log 'FILTER', controller.filter
    expect(controller.listeners[elmWithoutLook.id].field).toBe(controller.filter.field.name)

When I run the test, I get the following error:

LOG: 'ELM', undefined
LOG: 'EXP', 'products.brand_name'
LOG: 'LISTENERS', Object{on: true}
LOG: 'FILTER', Object{title: 'Brand Name', field: Object{name: 'products.brand_name'}, name: 'Brand Name', model: 'thelook', type: 'field_filter'}
Chrome 52.0.2743 (Mac OS X 10.11.6) Dashboard Filter Details dashboardFilterDetailsController autofindField should set field to filter field name FAILED
    Expected undefined to be 'products.brand_name'.

It appears that when EXP attempts to assign to ELM, it fails, as it lacks the field within LISTENERS. However, cross-checking the old buggy code with the new fixed code, I don't really notice any area which would cause such an issue. All I did was load fixed data into baseView and assign it into availableFields.

Another possible explanation to this is that the tests are out of date, and was written for the broken code, and thus should be invalidated.

Would love some additional eyes on this..

Aucun commentaire:

Enregistrer un commentaire