Mixed.spec.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import {
  2. stringify,
  3. isDefined,
  4. isUndefined,
  5. isEmpty,
  6. isRegExp,
  7. } from 'handsontable/helpers/mixed';
  8. describe('Mixed helper', () => {
  9. describe('stringify', () => {
  10. it('should convert properly `null` to `string`', () => {
  11. var toConvert = null;
  12. expect(stringify(toConvert)).toBe('');
  13. });
  14. it('should convert properly `boolean` to `string`', () => {
  15. var toConvert = true;
  16. expect(stringify(toConvert)).toBe('true');
  17. });
  18. it('should convert properly `number` to `string`', () => {
  19. var toConvert = 1;
  20. expect(stringify(toConvert)).toBe('1');
  21. });
  22. it('should convert properly `string` to `string`', () => {
  23. var toConvert = '2';
  24. expect(stringify(toConvert)).toBe('2');
  25. });
  26. it('should convert properly `object` to `string`', () => {
  27. var toConvert = {id: null};
  28. expect(stringify(toConvert)).toBe('[object Object]');
  29. });
  30. it('should convert properly `array` to `string`', () => {
  31. var toConvert = ['One', 'Two', 3];
  32. expect(stringify(toConvert)).toBe('One,Two,3');
  33. });
  34. it('should convert properly `RegExp` to `string`', () => {
  35. var toConvert = /^\d$/;
  36. expect(stringify(toConvert)).toBe('/^\\d$/');
  37. });
  38. it('should convert properly `function` to `string`', () => {
  39. var toConvert = function() {};
  40. expect(stringify(toConvert)).toMatch(/function/i);
  41. });
  42. it('should convert properly `undefined` to `string`', () => {
  43. var toConvert;
  44. expect(stringify(toConvert)).toBe('');
  45. });
  46. });
  47. describe('isDefined', () => {
  48. it('should return true when a variable is defined', () => {
  49. var toCheck = [];
  50. expect(isDefined(toCheck)).toBeTruthy();
  51. });
  52. it('should return false when a variable is not defined', () => {
  53. var toCheck;
  54. expect(isDefined(toCheck)).toBeFalsy();
  55. });
  56. });
  57. describe('isUndefined', () => {
  58. it('should check if a variable is defined', () => {
  59. var toCheck;
  60. expect(isUndefined(toCheck)).toBeTruthy();
  61. });
  62. it('should return false when a variable is not defined', () => {
  63. var toCheck = [];
  64. expect(isUndefined(toCheck)).toBeFalsy();
  65. });
  66. });
  67. describe('isEmpty', () => {
  68. it('should check if a variable is null, empty string or undefined', () => {
  69. expect(isEmpty(undefined)).toBeTruthy();
  70. expect(isEmpty('')).toBeTruthy();
  71. expect(isEmpty(null)).toBeTruthy();
  72. });
  73. it('should return false when a variable isn\'t null, empty string or undefined', () => {
  74. expect(isEmpty(NaN)).toBeFalsy();
  75. expect(isEmpty(0)).toBeFalsy();
  76. expect(isEmpty('a')).toBeFalsy();
  77. expect(isEmpty([])).toBeFalsy();
  78. expect(isEmpty({})).toBeFalsy();
  79. });
  80. });
  81. describe('isRegExp', () => {
  82. it('should check if a variable is a valid regular expression', () => {
  83. expect(isRegExp(undefined)).toBeFalsy();
  84. expect(isRegExp('')).toBeFalsy();
  85. expect(isRegExp(null)).toBeFalsy();
  86. expect(isRegExp(0)).toBeFalsy();
  87. expect(isRegExp(1)).toBeFalsy();
  88. expect(isRegExp('foo')).toBeFalsy();
  89. expect(isRegExp({a: /\d+/})).toBeFalsy();
  90. expect(isRegExp(/\d+/)).toBeTruthy();
  91. expect(isRegExp(new RegExp('d+'))).toBeTruthy();
  92. });
  93. });
  94. });