apphouse
Version:
Component library for React that uses observable state management and theme-able components.
46 lines (38 loc) • 1.1 kB
text/typescript
import { get } from './get';
describe('get', () => {
test('should return the nested property if it exists', () => {
const obj = {
foo: {
bar: {
baz: 'Hello, World!'
}
}
};
expect(get(obj, 'foo.bar.baz')).toBe('Hello, World!');
});
test('should return the default value if the nested property does not exist', () => {
const obj = {
foo: {
bar: {
baz: 'Hello, World!'
}
}
};
expect(get(obj, 'foo.bar.qux', 'Default')).toBe('Default');
});
test('should return the default value if the object is null or undefined', () => {
expect(get(null, 'foo.bar.baz', 'Default')).toBe('Default');
expect(get(undefined, 'foo.bar.baz', 'Default')).toBe('Default');
});
test('should return the default value if the nested property is null or undefined', () => {
const obj = {
foo: {
bar: {
baz: null
}
}
};
expect(get(obj, 'foo.bar.baz', 'Default')).toBe('Default');
expect(get(obj, 'foo.bar.qux', 'Default')).toBe('Default');
});
});