object.test.js 846 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { each, merge, values } from '../../src/js/utils/object';
  2. describe( 'Object function ', () => {
  3. test( '"merge" should deeply merge 2 objects.', () => {
  4. const merged = merge(
  5. {
  6. a: 1,
  7. b: 2,
  8. c: { x: 1, y: 2 },
  9. },
  10. {
  11. b: 3,
  12. c: { x: 0 },
  13. }
  14. );
  15. expect( merged ).toEqual( {
  16. a: 1,
  17. b: 3,
  18. c: { x: 0, y: 2 },
  19. } );
  20. } );
  21. test( '"each" should iterate an object.', () => {
  22. const obj = { a: 1, b: 2 };
  23. const values = [];
  24. const keys = [];
  25. each( obj, ( value, key ) => {
  26. values.push( value );
  27. keys.push( key );
  28. } );
  29. expect( values ).toEqual( [ 1, 2 ] );
  30. expect( keys ).toEqual( [ 'a', 'b' ] );
  31. } );
  32. test( '"values" should pick values from an object and return them as an array.', () => {
  33. const obj = { a: 1, b: 2 };
  34. expect( values( obj ) ).toEqual( [ 1, 2 ] );
  35. } );
  36. } );