File size: 2,101 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import get from '../../utils/get';

describe('get', () => {
  it('should get the right data', () => {
    const test = {
      bill: [1, 2, 3],
      luo: [1, 3, { betty: 'test' }],
      betty: { test: { test1: [{ test2: 'bill' }] } },
      'betty.test.test1[0].test1': 'test',
      'dotted.filled': 'content',
      'dotted.empty': '',
    };
    expect(get(test, 'bill')).toEqual([1, 2, 3]);
    expect(get(test, 'bill[0]')).toEqual(1);
    expect(get(test, 'luo[2].betty')).toEqual('test');
    expect(get(test, 'betty.test.test1[0].test2')).toEqual('bill');
    expect(get(test, 'betty.test.test1[0].test1')).toEqual('test');
    expect(get(test, 'betty.test.test1[0].test3')).toEqual(undefined);
    expect(get(test, 'dotted.filled')).toEqual(test['dotted.filled']);
    expect(get(test, 'dotted.empty')).toEqual(test['dotted.empty']);
    expect(get(test, 'dotted.nonexistent', 'default')).toEqual('default');
  });

  it('should get from the flat data', () => {
    const test = {
      bill: 'test',
    };
    expect(get(test, 'bill')).toEqual('test');
  });

  it('should return undefined when provided with empty path', () => {
    const test = {
      bill: 'test',
    };
    expect(get(test, '')).toEqual(undefined);
    expect(get(test, undefined)).toEqual(undefined);
    expect(get(test, null)).toEqual(undefined);
  });

  it('should retrieve values from path containing quotes', () => {
    const object = {
      'Im with single quote!': {
        _f: {
          name: "I'm with single quote!",
          mount: true,
          required: true,
        },
      },
      'With  dobule quote': {
        _f: {
          name: 'With " dobule quote',
          mount: true,
          required: true,
        },
      },
    };

    expect(get(object, "I'm with single quote!")).toEqual({
      _f: {
        name: "I'm with single quote!",
        mount: true,
        required: true,
      },
    });

    expect(get(object, 'With " dobule quote')).toEqual({
      _f: {
        name: 'With " dobule quote',
        mount: true,
        required: true,
      },
    });
  });
});