object.test.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import { describe, expect, it } from 'vitest';
  2. import { getIn, isEmptyArray, isNaN, isPromise, shallowSetIn } from '../src/utils';
  3. describe('object', () => {
  4. describe('isEmptyArray', () => {
  5. it('returns true when an empty array is passed in', () => {
  6. expect(isEmptyArray([])).toBe(true);
  7. });
  8. it('returns false when anything other than empty array is passed in', () => {
  9. expect(isEmptyArray()).toBe(false);
  10. expect(isEmptyArray(null)).toBe(false);
  11. expect(isEmptyArray(123)).toBe(false);
  12. expect(isEmptyArray('abc')).toBe(false);
  13. expect(isEmptyArray({})).toBe(false);
  14. expect(isEmptyArray({ a: 1 })).toBe(false);
  15. expect(isEmptyArray(['abc'])).toBe(false);
  16. });
  17. });
  18. describe('getIn', () => {
  19. const obj = {
  20. a: {
  21. b: 2,
  22. c: false,
  23. d: null,
  24. },
  25. t: true,
  26. s: 'a random string',
  27. };
  28. it('gets a value by array path', () => {
  29. expect(getIn(obj, ['a', 'b'])).toBe(2);
  30. });
  31. it('gets a value by string path', () => {
  32. expect(getIn(obj, 'a.b')).toBe(2);
  33. });
  34. it('return "undefined" if value was not found using given path', () => {
  35. expect(getIn(obj, 'a.z')).toBeUndefined();
  36. });
  37. it('return "undefined" if value was not found using given path and an intermediate value is "false"', () => {
  38. expect(getIn(obj, 'a.c.z')).toBeUndefined();
  39. });
  40. it('return "undefined" if value was not found using given path and an intermediate value is "null"', () => {
  41. expect(getIn(obj, 'a.d.z')).toBeUndefined();
  42. });
  43. it('return "undefined" if value was not found using given path and an intermediate value is "true"', () => {
  44. expect(getIn(obj, 't.z')).toBeUndefined();
  45. });
  46. it('return "undefined" if value was not found using given path and an intermediate value is a string', () => {
  47. expect(getIn(obj, 's.z')).toBeUndefined();
  48. });
  49. });
  50. describe('shallowSetIn', () => {
  51. it('sets flat value', () => {
  52. const obj = { x: 'y' };
  53. const newObj = shallowSetIn(obj, 'flat', 'value');
  54. expect(obj).toEqual({ x: 'y' });
  55. expect(newObj).toEqual({ x: 'y', flat: 'value' });
  56. });
  57. it('keep the same object if nothing is changed', () => {
  58. const obj = { x: 'y' };
  59. const newObj = shallowSetIn(obj, 'x', 'y');
  60. expect(obj).toBe(newObj);
  61. });
  62. it('removes flat value', () => {
  63. const obj = { x: 'y' };
  64. const newObj = shallowSetIn(obj, 'x', undefined);
  65. expect(obj).toEqual({ x: 'y' });
  66. expect(newObj).toEqual({});
  67. expect(newObj).not.toHaveProperty('x');
  68. });
  69. it('sets nested value', () => {
  70. const obj = { x: 'y' };
  71. const newObj = shallowSetIn(obj, 'nested.value', 'nested value');
  72. expect(obj).toEqual({ x: 'y' });
  73. expect(newObj).toEqual({ x: 'y', nested: { value: 'nested value' } });
  74. });
  75. it('updates nested value', () => {
  76. const obj = { x: 'y', nested: { value: 'a' } };
  77. const newObj = shallowSetIn(obj, 'nested.value', 'b');
  78. expect(obj).toEqual({ x: 'y', nested: { value: 'a' } });
  79. expect(newObj).toEqual({ x: 'y', nested: { value: 'b' } });
  80. });
  81. it('removes nested value', () => {
  82. const obj = { x: 'y', nested: { value: 'a' } };
  83. const newObj = shallowSetIn(obj, 'nested.value', undefined);
  84. expect(obj).toEqual({ x: 'y', nested: { value: 'a' } });
  85. expect(newObj).toEqual({ x: 'y', nested: {} });
  86. expect(newObj.nested).not.toHaveProperty('value');
  87. });
  88. it('updates deep nested value', () => {
  89. const obj = { x: 'y', twofoldly: { nested: { value: 'a' } } };
  90. const newObj = shallowSetIn(obj, 'twofoldly.nested.value', 'b');
  91. expect(obj.twofoldly.nested === newObj.twofoldly.nested).toEqual(false); // fails, same object still
  92. expect(obj).toEqual({ x: 'y', twofoldly: { nested: { value: 'a' } } }); // fails, it's b here, too
  93. expect(newObj).toEqual({ x: 'y', twofoldly: { nested: { value: 'b' } } }); // works ofc
  94. });
  95. it('removes deep nested value', () => {
  96. const obj = { x: 'y', twofoldly: { nested: { value: 'a' } } };
  97. const newObj = shallowSetIn(obj, 'twofoldly.nested.value', undefined);
  98. expect(obj.twofoldly.nested === newObj.twofoldly.nested).toEqual(false);
  99. expect(obj).toEqual({ x: 'y', twofoldly: { nested: { value: 'a' } } });
  100. expect(newObj).toEqual({ x: 'y', twofoldly: { nested: {} } });
  101. expect(newObj.twofoldly.nested).not.toHaveProperty('value');
  102. });
  103. it('shallow clone data along the update path', () => {
  104. const obj = {
  105. x: 'y',
  106. twofoldly: { nested: ['a', { c: 'd' }] },
  107. other: { nestedOther: 'o' },
  108. };
  109. const newObj = shallowSetIn(obj, 'twofoldly.nested.0', 'b');
  110. // All new objects/arrays created along the update path.
  111. expect(obj).not.toBe(newObj);
  112. expect(obj.twofoldly).not.toBe(newObj.twofoldly);
  113. expect(obj.twofoldly.nested).not.toBe(newObj.twofoldly.nested);
  114. // All other objects/arrays copied, not cloned (retain same memory
  115. // location).
  116. expect(obj.other).toBe(newObj.other);
  117. expect(obj.twofoldly.nested[1]).toBe(newObj.twofoldly.nested[1]);
  118. });
  119. it('sets new array', () => {
  120. const obj = { x: 'y' };
  121. const newObj = shallowSetIn(obj, 'nested.0', 'value');
  122. expect(obj).toEqual({ x: 'y' });
  123. expect(newObj).toEqual({ x: 'y', nested: ['value'] });
  124. });
  125. it('sets new array when item is empty string', () => {
  126. const obj = { x: 'y' };
  127. const newObj = shallowSetIn(obj, 'nested.0', '');
  128. expect(obj).toEqual({ x: 'y' });
  129. expect(newObj).toEqual({ x: 'y', nested: [''] });
  130. });
  131. it('sets new array when item is empty string', () => {
  132. const obj = {};
  133. const newObj = shallowSetIn(obj, 'nested.0', '');
  134. expect(obj).toEqual({});
  135. expect(newObj).toEqual({ nested: [''] });
  136. });
  137. it('updates nested array value', () => {
  138. const obj = { x: 'y', nested: ['a'] };
  139. const newObj = shallowSetIn(obj, 'nested[0]', 'b');
  140. expect(obj).toEqual({ x: 'y', nested: ['a'] });
  141. expect(newObj).toEqual({ x: 'y', nested: ['b'] });
  142. });
  143. it('adds new item to nested array', () => {
  144. const obj = { x: 'y', nested: ['a'] };
  145. const newObj = shallowSetIn(obj, 'nested.1', 'b');
  146. expect(obj).toEqual({ x: 'y', nested: ['a'] });
  147. expect(newObj).toEqual({ x: 'y', nested: ['a', 'b'] });
  148. });
  149. it('sticks to object with int key when defined', () => {
  150. const obj = { x: 'y', nested: { 0: 'a' } };
  151. const newObj = shallowSetIn(obj, 'nested.0', 'b');
  152. expect(obj).toEqual({ x: 'y', nested: { 0: 'a' } });
  153. expect(newObj).toEqual({ x: 'y', nested: { 0: 'b' } });
  154. });
  155. it('supports bracket path', () => {
  156. const obj = { x: 'y' };
  157. const newObj = shallowSetIn(obj, 'nested[0]', 'value');
  158. expect(obj).toEqual({ x: 'y' });
  159. expect(newObj).toEqual({ x: 'y', nested: ['value'] });
  160. });
  161. it('supports path containing key of the object', () => {
  162. const obj = { x: 'y' };
  163. const newObj = shallowSetIn(obj, 'a.x.c', 'value');
  164. expect(obj).toEqual({ x: 'y' });
  165. expect(newObj).toEqual({ x: 'y', a: { x: { c: 'value' } } });
  166. });
  167. it('should keep class inheritance for the top level object', () => {
  168. class TestClass {
  169. constructor(public key: string, public setObj?: any) {}
  170. }
  171. const obj = new TestClass('value');
  172. const newObj = shallowSetIn(obj, 'setObj.nested', 'shallowSetInValue');
  173. expect(obj).toEqual(new TestClass('value'));
  174. expect(newObj).toEqual({
  175. key: 'value',
  176. setObj: { nested: 'shallowSetInValue' },
  177. });
  178. expect(obj instanceof TestClass).toEqual(true);
  179. expect(newObj instanceof TestClass).toEqual(true);
  180. });
  181. it('can convert primitives to objects before setting', () => {
  182. const obj = { x: [{ y: true }] };
  183. const newObj = shallowSetIn(obj, 'x.0.y.z', true);
  184. expect(obj).toEqual({ x: [{ y: true }] });
  185. expect(newObj).toEqual({ x: [{ y: { z: true } }] });
  186. });
  187. });
  188. describe('isPromise', () => {
  189. it('verifies that a value is a promise', () => {
  190. const alwaysResolve = (resolve: Function) => resolve();
  191. const promise = new Promise(alwaysResolve);
  192. expect(isPromise(promise)).toEqual(true);
  193. });
  194. it('verifies that a value is not a promise', () => {
  195. const emptyObject = {};
  196. const identity = (i: any) => i;
  197. const foo = 'foo';
  198. const answerToLife = 42;
  199. expect(isPromise(emptyObject)).toEqual(false);
  200. expect(isPromise(identity)).toEqual(false);
  201. expect(isPromise(foo)).toEqual(false);
  202. expect(isPromise(answerToLife)).toEqual(false);
  203. expect(isPromise(undefined)).toEqual(false);
  204. expect(isPromise(null)).toEqual(false);
  205. });
  206. });
  207. describe('isNaN', () => {
  208. it('correctly validate NaN', () => {
  209. expect(isNaN(NaN)).toBe(true);
  210. });
  211. it('correctly validate not NaN', () => {
  212. expect(isNaN(undefined)).toBe(false);
  213. expect(isNaN(1)).toBe(false);
  214. expect(isNaN('')).toBe(false);
  215. expect(isNaN([])).toBe(false);
  216. });
  217. });
  218. });