aspectRatio.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. $(function () {
  2. 'use strict';
  3. var $image = $(window.createCropperImage());
  4. $image.cropper({
  5. built: function () {
  6. var cropper = $image.data('cropper'),
  7. options = cropper.options,
  8. cropBox = cropper.cropBox;
  9. QUnit.test('options.aspectRatio: Number', function (assert) {
  10. var ratios = [NaN, 0, 9, -8 / 7, -6 / 5, -1, -1 / 2, -3 / 4, 1 / 2, 3 / 4, 1, 8 / 7, 6 / 5, 9];
  11. $.each(ratios, function (i, ratio) {
  12. var data;
  13. $image.cropper('setAspectRatio', ratio);
  14. data = $image.cropper('getCropBoxData');
  15. if (i === 0 || i === 1) {
  16. ratio = cropBox.width / cropBox.height;
  17. }
  18. assert.ok(data.width / data.height - Math.abs(ratio) < 1 / 1000000000);
  19. });
  20. });
  21. QUnit.test('options.aspectRatio: String', function (assert) {
  22. var ratios = ['auto', 'a1', '2a', '3', '0'];
  23. $.each(ratios, function (i, ratio) {
  24. $image.cropper('setAspectRatio', ratio);
  25. switch (i) {
  26. case 0:
  27. case 1:
  28. case 4:
  29. assert.ok(isNaN(options.aspectRatio));
  30. break;
  31. case 2:
  32. assert.equal(options.aspectRatio, 2);
  33. break;
  34. case 3:
  35. assert.equal(options.aspectRatio, 3);
  36. break;
  37. }
  38. });
  39. });
  40. QUnit.test('options.aspectRatio: Boolean', function (assert) {
  41. var ratios = [true, false];
  42. $.each(ratios, function (i, ratio) {
  43. $image.cropper('setAspectRatio', ratio);
  44. assert.ok(isNaN(options.aspectRatio));
  45. });
  46. });
  47. QUnit.test('options.aspectRatio: Object', function (assert) {
  48. var ratios = [null, {}, { a: 1, b: 2 }];
  49. $.each(ratios, function (i, ratio) {
  50. $image.cropper('setAspectRatio', ratio);
  51. assert.ok(isNaN(options.aspectRatio));
  52. });
  53. });
  54. QUnit.test('options.aspectRatio: Array', function (assert) {
  55. var ratios = [[], [1], [2, 3], [undefined, 4, 5]];
  56. $.each(ratios, function (i, ratio) {
  57. $image.cropper('setAspectRatio', ratio);
  58. switch (i) {
  59. case 0:
  60. case 3:
  61. assert.ok(isNaN(options.aspectRatio));
  62. break;
  63. case 1:
  64. assert.equal(options.aspectRatio, 1);
  65. break;
  66. case 2:
  67. assert.equal(options.aspectRatio, 2);
  68. break;
  69. }
  70. });
  71. });
  72. QUnit.test('options.aspectRatio: undefined', function (assert) {
  73. var aspectRatio = options.aspectRatio;
  74. $image.cropper('setAspectRatio', undefined);
  75. if (isNaN(aspectRatio)) {
  76. assert.ok(isNaN(options.aspectRatio));
  77. } else {
  78. assert.equal(options.aspectRatio, aspectRatio);
  79. }
  80. });
  81. }
  82. });
  83. });