Repository: chartjs/Chart.js
File: test/specs/helpers.canvas.tests.js (JavaScript)

Code before:

  describe('clearCanvas', function() {
    it('should clear the chart canvas', function() {
      var chart = acquireChart({}, {
        canvas: {
          style: 'width: 150px; height: 245px'
        }
      });

      spyOn(chart.ctx, 'clearRect');

      helpers.clearCanvas(chart.canvas, chart.ctx);

      expect(chart.ctx.clearRect.calls.count()).toBe(1);
      expect(chart.ctx.clearRect.calls.first().object).toBe(chart.ctx);
      expect(chart.ctx.clearRect.calls.first().args).toEqual([0, 0, 150, 245]);
    });

    it('should not throw error when chart is null', function() {
      function createChart() {
        return acquireChart({}, {
          canvas: null
        });
      }

      expect(createChart).not.toThrow();
    });
  });

  describe('isPointInArea', function() {
    it('should return true when no area is provided', function() {
      expect(helpers._isPointInArea({x: 1, y: 1})).toBe(true);
    });
    it('should determine if a point is in the area', function() {
      var isPointInArea = helpers._isPointInArea;
      var area = {left: 0, top: 0, right: 512, bottom: 256};

      expect(isPointInArea({x: 0, y: 0}, area)).toBe(true);
      expect(isPointInArea({x: -1e-12, y: -1e-12}, area)).toBe(true);
      expect(isPointInArea({x: 512, y: 256}, area)).toBe(true);
      expect(isPointInArea({x: 512 + 1e-12, y: 256 + 1e-12}, area)).toBe(true);
      expect(isPointInArea({x: -0.5, y: 0}, area)).toBe(false);
      expect(isPointInArea({x: 0, y: 256.5}, area)).toBe(false);
    });
  });

  it('should return the width of the longest text in an Array and 2D Array', function() {
    var context = window.createMockContext();
    var font = "normal 12px 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif";
    var arrayOfThings1D = ['FooBar', 'Bar'];
    var arrayOfThings2D = [['FooBar_1', 'Bar_2'], 'Foo_1'];

Review comment (refactor):
Shouldn't we check on `helpers.clearCanvas` to not throw since that is the method that broke instead of the whole create canvas.

If for some reason the method does not get called in acquireChart this test will lose its value.

Code after:

  describe('clearCanvas', function() {
    it('should clear the chart canvas', function() {
      var chart = acquireChart({}, {
        canvas: {
          style: 'width: 150px; height: 245px'
        }
      });

      spyOn(chart.ctx, 'clearRect');

      helpers.clearCanvas(chart.canvas, chart.ctx);

      expect(chart.ctx.clearRect.calls.count()).toBe(1);
      expect(chart.ctx.clearRect.calls.first().object).toBe(chart.ctx);
      expect(chart.ctx.clearRect.calls.first().args).toEqual([0, 0, 150, 245]);
    });

    it('should not throw error when chart is null', function() {
      function createAndClearChart() {
        var chart = acquireChart({}, {
          canvas: null
        });
        // explicitly set canvas and ctx to null since setting it in acquireChart doesn't do anything
        chart.canvas = null;
        chart.ctx = null;

        helpers.clearCanvas(chart.canvas, chart.ctx);
      }

      expect(createAndClearChart).not.toThrow();
    });
  });

  describe('isPointInArea', function() {
    it('should return true when no area is provided', function() {
      expect(helpers._isPointInArea({x: 1, y: 1})).toBe(true);
    });
    it('should determine if a point is in the area', function() {
      var isPointInArea = helpers._isPointInArea;
      var area = {left: 0, top: 0, right: 512, bottom: 256};

      expect(isPointInArea({x: 0, y: 0}, area)).toBe(true);
      expect(isPointInArea({x: -1e-12, y: -1e-12}, area)).toBe(true);
      expect(isPointInArea({x: 512, y: 256}, area)).toBe(true);
      expect(isPointInArea({x: 512 + 1e-12, y: 256 + 1e-12}, area)).toBe(true);
      expect(isPointInArea({x: -0.5, y: 0}, area)).toBe(false);
      expect(isPointInArea({x: 0, y: 256.5}, area)).toBe(false);
    });
  });