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

Code before:
      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
        });

        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);
    });
  });

  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):
This test does not seem to work.
If you don't change `helpers.canvas.ts` and only add this test and expect it to throw it does not throw an error. It seems that passing `null` to the canvas object does not make the chart without a canvas

as you can see here in the test utils package it fills the config with default values when null is passed: https://github.com/chartjs/chartjs-test-utils/blob/master/src/utils.js#L36-L44

![image](https://github.com/chartjs/Chart.js/assets/39033624/ccca2df1-74e2-4efe-90d6-8a1aaadfeafb)
![image](https://github.com/chartjs/Chart.js/assets/39033624/30013a9b-9203-4fbd-add0-7fb468298ca6)


Code after:
      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);
    });
  });

  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";