transform-values-deep.spec.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import {expect} from 'chai';
  2. import {transformValuesDeep} from './transform-values-deep.js';
  3. describe('transformValuesDeep', function () {
  4. it('transforms property values of an object', function () {
  5. const object = {
  6. foo: 1,
  7. bar: {
  8. baz: 2,
  9. qux: {
  10. qwe: 3,
  11. },
  12. },
  13. };
  14. const result = transformValuesDeep(object, String);
  15. expect(result).to.be.eql({
  16. foo: '1',
  17. bar: {
  18. baz: '2',
  19. qux: {
  20. qwe: '3',
  21. },
  22. },
  23. });
  24. });
  25. it('transforms elements of an array', function () {
  26. const object = [1, 2, 3, [4, 5, 6, [7, 8, 9]]];
  27. const result = transformValuesDeep(object, String);
  28. expect(result).to.be.eql(['1', '2', '3', ['4', '5', '6', ['7', '8', '9']]]);
  29. });
  30. it('transforms non-pure objects', function () {
  31. const date = new Date();
  32. const str = String(date);
  33. const result1 = transformValuesDeep(date, String);
  34. const result2 = transformValuesDeep([date], String);
  35. const result3 = transformValuesDeep([[date]], String);
  36. const result4 = transformValuesDeep({date}, String);
  37. const result5 = transformValuesDeep({foo: {date}}, String);
  38. expect(result1).to.be.eql(str);
  39. expect(result2).to.be.eql([str]);
  40. expect(result3).to.be.eql([[str]]);
  41. expect(result4).to.be.eql({date: str});
  42. expect(result5).to.be.eql({foo: {date: str}});
  43. });
  44. });