Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected behavior when an intermediate key is null #21

Open
sturench opened this issue Dec 7, 2017 · 2 comments
Open

Unexpected behavior when an intermediate key is null #21

sturench opened this issue Dec 7, 2017 · 2 comments
Labels

Comments

@sturench
Copy link

sturench commented Dec 7, 2017

When attempting to access a a nested attribute of a null value, a TypeError is encountered.

The below test suite illustrates the behavior.

Is this intended, or something that can/should be handled in the library?

describe('dot-prop-immutable get behavior', () => {
  const testObject = {
    a: {
      b: {
        c: 1
      },
      nullKey: null
    }
  };
  it('returns correct value when all keys present', () => {
    expect(dotProp.get(testObject, 'a.b.c')).toBe(1);
  });

  it('returns undefined when an intermediate key is missing', () => {
    expect(dotProp.get(testObject, 'a.missingKey.c')).toBeUndefined();
  });

  it('returns null when a leaf key is null', () => {
    expect(dotProp.get(testObject, 'a.nullKey')).toBeNull();
  });

  it('returns undefined when an intermediate key is missing', () => {
    expect(dotProp.get(testObject, 'a.missingKey.c')).toBeUndefined();
  });

  // This test currently fails.  My expectation is that it would return
  // undefined.
  it('returns undefined when an intermediate key is null', () => {
    expect(dotProp.get(testObject, 'a.nullKey.anotherKey')).toBeUndefined();
  });
});
 FAIL  src/__test__/utils.test.js
  ● dot-prop-immutable get behavior › returns undefined when an intermediate key is null

    TypeError: Cannot read property 'anotherKey' of null

      at Object.get (node_modules/dot-prop-immutable/index.js:48:13)
      at Object.it (src/__test__/utils.test.js:206:39)
          at Promise (<anonymous>)
      at Promise.resolve.then.el (node_modules/p-map/index.js:46:16)
          at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)
  dot-prop-immutable get behavior
    ✓ returns correct value when all keys present
    ✓ returns undefined when an intermediate key is missing (1ms)
    ✓ returns null when a leaf key is null
    ✓ returns undefined when an intermediate key is missing
    ✕ returns undefined when an intermediate key is null

Test Suites: 1 failed, 1 total
@hilleer hilleer added the bug label Aug 23, 2019
bifrost pushed a commit that referenced this issue Jan 3, 2020
#21 - add null checks and tests relative to the solved issues
@hilleer
Copy link
Member

hilleer commented Jan 8, 2020

Hi,

sorry for the late response to this.

This issue should be resolved with these changes published in latest version: #23

@hilleer hilleer closed this as completed Jan 8, 2020
@ghost
Copy link

ghost commented Oct 9, 2020

This still happens when there is a call to set with an intermediate key that is null, e.g. dotProp.set(testObject, 'a.nullKey.anotherKey', 'a value')

@hilleer hilleer reopened this Oct 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants