get-value-by-path.spec.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import {expect} from 'chai';
  2. import {getValueByPath} from './get-value-by-path.js';
  3. const VAL = 'value';
  4. describe('getValueByPath', function () {
  5. it('returns undefined if no value', function () {
  6. expect(getValueByPath({}, 'foo')).to.be.undefined;
  7. expect(getValueByPath({}, 'foo.bar')).to.be.undefined;
  8. expect(getValueByPath({foo: {}}, 'foo.bar.baz')).to.be.undefined;
  9. });
  10. it('returns a value by given path', function () {
  11. expect(getValueByPath({foo: VAL}, 'foo')).to.be.eq(VAL);
  12. expect(getValueByPath({foo: {bar: VAL}}, 'foo.bar')).to.be.eq(VAL);
  13. expect(getValueByPath({foo: {bar: {baz: VAL}}}, 'foo.bar.baz')).to.be.eq(
  14. VAL,
  15. );
  16. });
  17. it('returns a given fallback if no value', function () {
  18. expect(getValueByPath({}, 'foo', VAL)).to.be.eq(VAL);
  19. expect(getValueByPath({}, 'foo.bar', VAL)).to.be.eq(VAL);
  20. expect(getValueByPath({foo: {}}, 'foo.bar.baz', VAL)).to.be.eq(VAL);
  21. });
  22. it('returns a given fallback for null or undefined object', function () {
  23. expect(getValueByPath(null, 'foo', VAL)).to.be.eq(VAL);
  24. expect(getValueByPath(undefined, 'foo', VAL)).to.be.eq(VAL);
  25. });
  26. it('returns a given fallback for null or undefined key', function () {
  27. expect(getValueByPath({}, null, VAL)).to.be.eq(VAL);
  28. expect(getValueByPath({}, undefined, VAL)).to.be.eq(VAL);
  29. });
  30. });