utils.spec.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { Icon } from "../icon";
  2. import { addIcons, getIconMap, getName, getSrc, getUrl } from "../utils";
  3. describe('getUrl', () => {
  4. let i;
  5. beforeEach(() => {
  6. i = new Icon();
  7. });
  8. it('use icon prop, as name', () => {
  9. i.icon = 'some-name';
  10. expect(getUrl(i)).toBe('/svg/some-name.svg');
  11. });
  12. it('use icon prop, as url', () => {
  13. i.icon = './some.svg';
  14. expect(getUrl(i)).toBe('./some.svg');
  15. });
  16. it('use name prop', () => {
  17. i.name = 'some-name';
  18. expect(getUrl(i)).toBe('/svg/some-name.svg');
  19. });
  20. it('use src prop', () => {
  21. i.src = './some.svg';
  22. i.name = 'some-name';
  23. i.icon = 'some-icon';
  24. expect(getUrl(i)).toBe('./some.svg');
  25. });
  26. });
  27. describe('getSrc', () => {
  28. it('both . and /', () => {
  29. expect(getSrc('./somefile.svg')).toBe('./somefile.svg');
  30. });
  31. it('url', () => {
  32. expect(getSrc('https://ionicons/somefile.svg')).toBe('https://ionicons/somefile.svg');
  33. });
  34. it('just a .', () => {
  35. expect(getSrc('somefile.svg')).toBe('somefile.svg');
  36. });
  37. it('just a /', () => {
  38. expect(getSrc('/somesvg')).toBe('/somesvg');
  39. });
  40. it('no . or /', () => {
  41. expect(getSrc('some-name')).toBe(null);
  42. });
  43. });
  44. describe('getName', () => {
  45. it('not allow special chars', () => {
  46. expect(getName('some\\name', '', 'io', '', '')).toBe(null);
  47. expect(getName('some$name', '', 'io', '', '')).toBe(null);
  48. expect(getName('some:name', '', 'io', '', '')).toBe(null);
  49. expect(getName('some.name', '', 'io', '', '')).toBe(null);
  50. expect(getName('some/name', '', 'io', '', '')).toBe(null);
  51. });
  52. it('use ios mode prefixed', () => {
  53. expect(getName('ios-some-name', '', '', '', '')).toBe('ios-some-name');
  54. });
  55. it('use md mode prefixed', () => {
  56. expect(getName('md-some-name', '', '', '', '')).toBe('md-some-name');
  57. });
  58. it('should not use name if no name, ios or md', () => {
  59. expect(getName(undefined, undefined, '', '', '')).toBe(null);
  60. });
  61. });
  62. describe('addIcons', () => {
  63. it('should add an svg to the icon cache', () => {
  64. const testData = 'stubbed data';
  65. expect(getIconMap().get('logo-ionic')).toEqual(undefined);
  66. addIcons({ 'logo-ionic': 'stubbed data' });
  67. expect(getIconMap().get('logo-ionic')).toEqual(testData);
  68. });
  69. it('should add kebab and camel case names to the icon cache', () => {
  70. const logoIonitron = 'stubbed data';
  71. expect(getIconMap().get('logo-ionitron')).toEqual(undefined);
  72. expect(getIconMap().get('logoIonitron')).toEqual(undefined);
  73. addIcons({ logoIonitron });
  74. expect(getIconMap().get('logo-ionitron')).toEqual(logoIonitron);
  75. expect(getIconMap().get('logoIonitron')).toEqual(logoIonitron);
  76. const logoIonitron0 = 'stubbed data 0';
  77. expect(getIconMap().get('logo-ionitron-0')).toEqual(undefined);
  78. expect(getIconMap().get('logoIonitron0')).toEqual(undefined);
  79. addIcons({ logoIonitron0 });
  80. expect(getIconMap().get('logo-ionitron-0')).toEqual(logoIonitron0);
  81. expect(getIconMap().get('logoIonitron0')).toEqual(logoIonitron0);
  82. });
  83. it('should map to a name that does not match the svg', () => {
  84. const logoIonitron = 'stubbed data';
  85. expect(getIconMap().get('my-fun-icon')).toEqual(undefined);
  86. addIcons({ 'my-fun-icon': logoIonitron });
  87. expect(getIconMap().get('my-fun-icon')).toEqual(logoIonitron);
  88. });
  89. it('should map to an explicit camel case name', () => {
  90. const logoIonitron = 'stubbed data';
  91. expect(getIconMap().get('myCoolIcon')).toEqual(undefined);
  92. addIcons({ 'myCoolIcon': logoIonitron });
  93. expect(getIconMap().get('myCoolIcon')).toEqual(logoIonitron);
  94. });
  95. it('should not warn when mapping the same icon twice', () => {
  96. const spy = jest.spyOn(console, 'warn');
  97. const myIcon = 'my-icon';
  98. expect(spy).not.toHaveBeenCalled();
  99. addIcons({ myIcon });
  100. expect(spy).not.toHaveBeenCalled();
  101. addIcons({ myIcon });
  102. expect(spy).not.toHaveBeenCalled();
  103. });
  104. it('should not overwrite icons', () => {
  105. const spy = jest.spyOn(console, 'warn');
  106. const logoA = 'logo a';
  107. const logoB = 'logo b';
  108. expect(spy).not.toHaveBeenCalled();
  109. expect(getIconMap().get('logo-a')).toEqual(undefined);
  110. addIcons({ 'logo-a': logoB, logoA });
  111. expect(getIconMap().get('logo-a')).toEqual(logoB);
  112. expect(getIconMap().get('logoA')).toEqual(logoA);
  113. expect(spy).toHaveBeenCalled();
  114. });
  115. });