index.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. 'use strict';
  2. var fixture = require('./fixture');
  3. var Context = require('./context');
  4. var matchers = require('./matchers');
  5. var utils = require('./utils');
  6. (function() {
  7. // Keep track of all acquired charts to automatically release them after each specs
  8. var charts = {};
  9. function acquireChart() {
  10. var chart = utils.acquireChart.apply(utils, arguments);
  11. charts[chart.id] = chart;
  12. return chart;
  13. }
  14. function releaseChart(chart) {
  15. utils.releaseChart.apply(utils, arguments);
  16. delete charts[chart.id];
  17. }
  18. function createMockContext() {
  19. return new Context();
  20. }
  21. // force ratio=1 for tests on high-res/retina devices
  22. // fixes https://github.com/chartjs/Chart.js/issues/4515
  23. window.devicePixelRatio = 1;
  24. window.acquireChart = acquireChart;
  25. window.releaseChart = releaseChart;
  26. window.waitForResize = utils.waitForResize;
  27. window.createMockContext = createMockContext;
  28. // some style initialization to limit differences between browsers across different platforms.
  29. utils.injectCSS(
  30. '.chartjs-wrapper, .chartjs-wrapper canvas {' +
  31. 'border: 0;' +
  32. 'margin: 0;' +
  33. 'padding: 0;' +
  34. '}' +
  35. '.chartjs-wrapper {' +
  36. 'position: absolute' +
  37. '}');
  38. jasmine.fixture = fixture;
  39. jasmine.triggerMouseEvent = utils.triggerMouseEvent;
  40. beforeEach(function() {
  41. jasmine.addMatchers(matchers);
  42. });
  43. afterEach(function() {
  44. // Auto releasing acquired charts
  45. Object.keys(charts).forEach(function(id) {
  46. var chart = charts[id];
  47. if (!(chart.$test || {}).persistent) {
  48. releaseChart(chart);
  49. }
  50. });
  51. });
  52. }());