API Docs for: 1.5.1
Show:

File: test/specs/data/SummaryData.spec.js

/**
 * Created by Chantell.Nichols on 10/8/2015.
 */
define(['_', 'promise', 'src/js/data/SummaryData', 'unittest/mocks/CoreMock', 'test/mocks/PluginMock'], function (_, Promise, SummaryData, CoreMock, PluginMock) {
  describe('SummaryData', function () {
    var core, plugin;
    describe('init', function () {
      beforeEach(function () {
        core = new CoreMock();
      });
      it('should add property even if options is empty', function () {
        SummaryData.init(core);
        expect(core.Segment.addPluginProperty).toHaveBeenCalledWith('availability', '0%-0%', true);
        expect(core.Segment.addPluginProperty).toHaveBeenCalledWith('placesAvailable', 0, false);
        expect(core.Segment.addPluginProperty).toHaveBeenCalledWith('minPrice', 'N/A', true);
      });
      it('should validate ranges interval', function () {
        var ranges = [{
          range: '2',
          color: '999999'
        }, {
          range: '1',
          color: '004878'
        }];
        expect(function () {
          SummaryData.init(core, ranges);
        }).toThrow();
      });
      it('should be able to create ranges', function () {
        var ranges = [{
          range: '0',
          color: '999999'
        }, {
          range: '1',
          color: '004878'
        }];
        SummaryData.init(core, ranges);
        expect(SummaryData.ranges).toEqual({
          '0%-0%': {
            name: '0%-0%',
            interiorColor: '999999',
            highRange: 0,
            lowRange: 0
          },
          '0%-100%': {
            name: '0%-100%',
            interiorColor: '004878',
            highRange: 1,
            lowRange: 0
          }
        });
      });
    });

    describe('getData', function () {
      var resaleMockData = {
        facets: [{
          shapes: ['test_id_1'],
          inventoryTypes: ['primary'],
          count: 1,
          faceRange: [{
            min: 16,
            max: 2
          }]
        }, {
          shapes: ['test_id_1'],
          inventoryTypes: ['resale'],
          count: 1,
          faceRange: [{
            min: 3,
            max: 4
          }]
        }, {
          shapes: ['test_id_2'],
          inventoryTypes: ['primary'],
          count: 1,
          faceRange: [{
            min: 2,
            max: 2
          }]
        }, {
          shapes: ['test_id_2'],
          inventoryTypes: ['resale'],
          count: 1,
          faceRange: [{
            min: 2,
            max: 2
          }]
        }]
      };

      var primaryMockData = {
        facets: [{
          shapes: ['test_id_1'],
          inventoryTypes: ['primary'],
          count: 1,
          faceRange: [{
            min: 16,
            max: 21
          }]
        }, {
          shapes: ['test_id_2'],
          inventoryTypes: ['primary'],
          count: 1,
          faceRange: [{
            min: 1,
            max: 1
          }]
        }]
      };

      var composite;

      describe('primary data', function () {
        beforeEach(function () {
          plugin = new PluginMock();
          plugin._placeOfferData = {};
          core = plugin.core;
          plugin.resaleEnabled = false;
          SummaryData.init(core);
          composite = {
            placesAvailable: 0,
            availability: 'NONE',
            minPrice: 'N/A',
            totalPlaces: 100
          };
          plugin.urlBuilder.getSummaryData = jasmine.createSpy('getSummaryData').and.returnValue(Promise.resolve(primaryMockData));
        });
        it('should be able to set availability to high', function (done) {
          var segment1 = core.data.segmentsById['test_id_1'] = _.clone(composite);
          var segment2 = core.data.segmentsById['test_id_2'] = _.clone(composite);
          SummaryData.getData(plugin)
            .then(function () {
              expect(segment1.placesAvailable).toBe(1);
              expect(segment1.minPrice).toBe('$16.00+');
              expect(segment2.minPrice).toBe('$1.00');
              done();
            });
        });

      });

      describe('resale data', function () {
        beforeEach(function () {
          plugin = new PluginMock();
          plugin._placeOfferData = {};
          core = plugin.core;
          plugin.resaleEnabled = true;
          SummaryData.init(core);
          composite = {
            placesAvailable: 0,
            availability: 'NONE',
            minPrice: 'N/A',
            totalPlaces: 100
          };
          plugin.urlBuilder.getSummaryData = jasmine.createSpy('getSummaryData').and.returnValue(Promise.resolve(resaleMockData));
        });
        it('should be able to set availability to high', function (done) {
          var segment1 = core.data.segmentsById['test_id_1'] = _.clone(composite);
          var segment2 = core.data.segmentsById['test_id_2'] = _.clone(composite);
          SummaryData.getData(plugin)
            .then(function () {
              expect(segment1.placesAvailable).toBe(2);
              expect(segment1.minPrice).toBe('$3.00+');
              expect(segment2.minPrice).toBe('$2.00');
              done();
            });
        });
      });




    })

    describe('getData', function () {

      var primaryMockData = {
        facets: [{
          shapes: ['test_id_1'],
          inventoryTypes: ['primary'],
          count: 1,
          faceRange: [{
            min: 16,
            max: 21
          }]
        }, {
          shapes: ['test_id_2'],
          inventoryTypes: ['primary'],
          count: 1,
          faceRange: [{
            min: 1,
            max: 1
          }]
        }]
      };

      var composite;

      beforeEach(function () {
        plugin = new PluginMock();
        plugin._placeOfferData = {};
        core = plugin.core;
        plugin.resaleEnabled = false;
        SummaryData.init(core);
        composite = {
          placesAvailable: 0,
          availability: 'NONE',
          minPrice: '26',
          totalPlaces: 100
        };
        plugin.urlBuilder.getSummaryData = jasmine.createSpy('getSummaryData').and.returnValue(Promise.resolve(primaryMockData));
      });
      it('should be able to unset minPrice', function (done) {
        var segment1 = core.data.segmentsById['test_id_1'] = _.clone(composite);
        var segment2 = core.data.segmentsById['test_id_2'] = _.clone(composite);
        var segment3 = core.data.segmentsById['test_id_7'] = _.clone(composite);
        SummaryData.getData(plugin)
          .then(function () {
            expect(segment3.minPrice).toEqual('N/A');
            done();
          });
      });

    })

  });
});