jquery.flot.colorhelpers.Test.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* eslint-disable */
  2. /* global $, describe, it, xit, xdescribe, after, afterEach, expect*/
  3. describe("colorhelpers plugin", function() {
  4. it('can make a new color', function () {
  5. var color = $.color.make(10, 20, 30, 0.5);
  6. expect(color.r).toBe(10);
  7. expect(color.g).toBe(20);
  8. expect(color.b).toBe(30);
  9. expect(color.a).toBe(0.5);
  10. });
  11. it('scales the specified components with given factor', function () {
  12. var color = $.color.make(10, 20, 30, 0.5);
  13. var factor = 0.2;
  14. color.scale('ga', factor);
  15. expect(color.r).toBe(10);
  16. expect(color.g).toBe(20 * factor);
  17. expect(color.b).toBe(30);
  18. expect(color.a).toBe(0.5 * factor);
  19. });
  20. it('adds to the specified component a given delta', function () {
  21. var color = $.color.make(10, 20, 30, 0.5);
  22. var delta = 3;
  23. color.add('rb', delta);
  24. expect(color.r).toBe(10 + delta);
  25. expect(color.g).toBe(20);
  26. expect(color.b).toBe(30 + delta);
  27. expect(color.a).toBe(0.5);
  28. });
  29. it('normalizes the invalid values', function () {
  30. var color = $.color.make(-1, 256, 200.1, -0.1);
  31. expect(color.r).toBe(0);
  32. expect(color.g).toBe(255);
  33. expect(color.b).toBe(200);
  34. expect(color.a).toBe(0);
  35. });
  36. it('normalizes the invalid values', function () {
  37. var color = $.color.make(-1, 256, 200.1, -0.1);
  38. expect(color.r).toBe(0);
  39. expect(color.g).toBe(255);
  40. expect(color.b).toBe(200);
  41. expect(color.a).toBe(0);
  42. });
  43. it('can make a new color object based on different color format string', function() {
  44. [
  45. 'rgb(17, 170, 187)',
  46. 'rgba(17, 170, 187, 1)',
  47. '#1ab',
  48. '#11aabb'
  49. ].forEach(function(str) {
  50. color = $.color.parse(str);
  51. expect(color.r).toBe(17);
  52. expect(color.g).toBe(170);
  53. expect(color.b).toBe(187);
  54. expect(color.a).toBe(1);
  55. });
  56. });
  57. it('can make a new color object based on a named color string', function() {
  58. [
  59. { str: 'darkolivegreen', rgba: [85, 107, 47, 1] },
  60. { str: 'transparent', rgba: [255, 255, 255, 0] }
  61. ].forEach(function(tc) {
  62. color = $.color.parse(tc.str);
  63. expect(color.r).toBe(tc.rgba[0]);
  64. expect(color.g).toBe(tc.rgba[1]);
  65. expect(color.b).toBe(tc.rgba[2]);
  66. expect(color.a).toBe(tc.rgba[3]);
  67. });
  68. });
  69. describe('by looking in DOM', function() {
  70. var testElement;
  71. beforeEach(function() {
  72. testElement = setFixtures('<div style="color: red"><div id="test-element" style="background-color: yellow" /></div>')
  73. .find('#test-element');
  74. });
  75. it('extracts a specified CSS color from a given element', function() {
  76. var color = $.color.extract(testElement, 'background-color');
  77. expect($.color.parse('yellow').toString()).toBe(color.toString());
  78. });
  79. it('extracts a specified CSS color from the parent of a given element', function() {
  80. var color = $.color.extract(testElement, 'color');
  81. expect($.color.parse('red').toString()).toBe(color.toString());
  82. });
  83. });
  84. });