Advertisement
simonradev

RGB

Mar 5th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. describe('rgbToHexColor works correct', () => {
  2.  
  3.     const colors = ['red', 'green', 'blue'];
  4.     for (let indexRGB = 0; indexRGB < 3; indexRGB++) {
  5.         it(`Invalid ${colors[indexRGB]} values should return undefined`, () => {
  6.             const expected = undefined;
  7.  
  8.             const InvalidValues = ['1', 1.1, -1, -100, -1000, 256, 300, 1000];
  9.             const Dummy = [1, 1, 1];
  10.  
  11.             for (let indexInvalidValue = 0; indexInvalidValue < InvalidValues.length; indexInvalidValue++) {
  12.                 const currInvalidValue = InvalidValues[indexInvalidValue];
  13.                
  14.                 let newArr = Dummy.slice();
  15.                 newArr[indexRGB] = currInvalidValue;
  16.  
  17.                 const actual = rgbToHexColor(...newArr);
  18.                
  19.                 assert.equal(expected, actual);
  20.             }
  21.         });
  22.     }
  23.  
  24.     it('Should start with # and be 7 symbols long', () => {
  25.         const actual = rgbToHexColor(100, 100, 100);
  26.         assert.equal(actual.length, 7);
  27.         assert.equal(actual[0], '#');
  28.     });
  29.  
  30.    
  31.     for (let indexRGB = 0; indexRGB < 3; indexRGB++) {
  32.         it(`Hex value for ${colors[indexRGB]} with length of one should be prepended wih 0`, () => {
  33.             const SmallValue = '123456789';
  34.             const Dummy = [200, 200, 200];
  35.  
  36.             for (let indexSmallValue = 0; indexSmallValue < SmallValue.length; indexSmallValue++) {
  37.                 const currSmallValue = SmallValue[indexSmallValue];
  38.  
  39.                 const newArr = Dummy.slice();
  40.                 newArr[indexRGB] = +currSmallValue;
  41.  
  42.                 const result = rgbToHexColor(...newArr);
  43.                 const trimmed = result.slice(1);
  44.                 const actual = trimmed.substr(indexRGB * 2, 2);
  45.  
  46.                 assert(actual[0], '0');
  47.             }
  48.  
  49.         });
  50.     }
  51.  
  52.     it('Should return correct RGB value', () => {
  53.         const actual1 = rgbToHexColor(0, 0, 0);
  54.         const actual2 = rgbToHexColor(100, 100, 100);
  55.         const actual3 = rgbToHexColor(255, 255, 255);
  56.  
  57.         const expexted1 = '#000000';
  58.         const expexted2 = '#646464';
  59.         const expexted3 = '#FFFFFF';
  60.  
  61.         assert.equal(actual1, expexted1);
  62.         assert.equal(actual2, expexted2);
  63.         assert.equal(actual3, expexted3);
  64.     });
  65. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement