index.spec.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { deepClone } from '../deep-clone';
  2. import { isAndroid, isDef, camelize, get } from '..';
  3. import { raf, cancel } from '../raf';
  4. import { later } from '../../../test/utils';
  5. import { isSrc } from '../validate/src';
  6. import { isEmail } from '../validate/email';
  7. import { isMobile } from '../validate/mobile';
  8. import { isNumber } from '../validate/number';
  9. test('deepClone', () => {
  10. const a = { foo: 0 };
  11. const b = { foo: 0, bar: 1 };
  12. const fn = () => { };
  13. const arr = [a, b];
  14. expect(deepClone(a)).toEqual(a);
  15. expect(deepClone(b)).toEqual(b);
  16. expect(deepClone(fn)).toEqual(fn);
  17. expect(deepClone(arr)).toEqual(arr);
  18. expect(deepClone(undefined)).toEqual(undefined);
  19. expect(deepClone(1)).toEqual(1);
  20. });
  21. test('isDef', () => {
  22. expect(isDef(null)).toBeFalsy();
  23. expect(isDef(undefined)).toBeFalsy();
  24. expect(isDef(1)).toBeTruthy();
  25. expect(isDef('1')).toBeTruthy();
  26. expect(isDef({})).toBeTruthy();
  27. expect(isDef(() => { })).toBeTruthy();
  28. });
  29. test('camelize', () => {
  30. expect(camelize('ab')).toEqual('ab');
  31. expect(camelize('a-b')).toEqual('aB');
  32. expect(camelize('a-b-c-d')).toEqual('aBCD');
  33. expect(camelize('a-b-')).toEqual('aB-');
  34. expect(camelize('-a-b')).toEqual('AB');
  35. expect(camelize('-')).toEqual('-');
  36. });
  37. test('get', () => {
  38. expect(get({ a: 1 }, 'a')).toEqual(1);
  39. expect(get({ a: { b: 2 } }, 'a.b')).toEqual(2);
  40. expect(get({ a: { b: 2 } }, 'a.b.c')).toEqual('');
  41. });
  42. test('isAndroid', () => {
  43. expect(isAndroid()).toBeFalsy();
  44. });
  45. test('raf', async () => {
  46. const spy = jest.fn();
  47. raf(spy);
  48. await later(50);
  49. expect(spy).toHaveBeenCalledTimes(1);
  50. cancel(1);
  51. });
  52. test('is-email', () => {
  53. expect(isEmail('abc@gmail.com')).toBeTruthy();
  54. expect(isEmail('abc@@gmail.com')).toBeFalsy();
  55. expect(isEmail('@gmail.com')).toBeFalsy();
  56. expect(isEmail('abc@')).toBeFalsy();
  57. });
  58. test('is-mobile', () => {
  59. expect(isMobile('13000000000')).toBeTruthy();
  60. expect(isMobile('+8613000000000')).toBeTruthy();
  61. expect(isMobile('8613000000000')).toBeTruthy();
  62. expect(isMobile('1300000000')).toBeFalsy();
  63. expect(isMobile('abc')).toBeFalsy();
  64. });
  65. test('is-number', () => {
  66. expect(isNumber('1')).toBeTruthy();
  67. expect(isNumber('abc')).toBeFalsy();
  68. expect(isNumber('1b2')).toBeFalsy();
  69. });
  70. test('is-src', () => {
  71. expect(isSrc('http://img.cdn.com')).toBeTruthy();
  72. expect(isSrc('https://img.cdn.com')).toBeTruthy();
  73. expect(isSrc('//img.cdn.com')).toBeTruthy();
  74. expect(isSrc('data:image/jpeg;base64,/9j/4AAQSkZ')).toBeTruthy();
  75. expect(isSrc('img.cdn.com')).toBeFalsy();
  76. expect(isSrc('name')).toBeFalsy();
  77. expect(isSrc('')).toBeFalsy();
  78. expect(isSrc('blob:http://img.cdn.com')).toBeTruthy();
  79. expect(isSrc('blob:https://img.cdn.com')).toBeTruthy();
  80. expect(isSrc('xdata:image/jpeg;base64,/9j/4AAQSkZ')).toBeFalsy();
  81. });