|
| 1 | +/* globals test expect */ |
| 2 | +import { Map } from 'immutable' |
| 3 | +import { getValuesForKey } from '../util' |
| 4 | + |
| 5 | +test('should get the values to search on in an object', () => { |
| 6 | + const value = getValuesForKey('foo', { |
| 7 | + foo: 'bar' |
| 8 | + }) |
| 9 | + expect(value).toEqual(['bar']) |
| 10 | +}) |
| 11 | + |
| 12 | +test('should get the values to search on in an array', () => { |
| 13 | + const value = getValuesForKey('foo', [{ |
| 14 | + foo: 'bar' |
| 15 | + }]) |
| 16 | + expect(value).toEqual(['bar']) |
| 17 | +}) |
| 18 | + |
| 19 | +test('should get the values to search on in a nested object', () => { |
| 20 | + const value = getValuesForKey('foo.bar', { |
| 21 | + foo: { |
| 22 | + bar: 'baz' |
| 23 | + } |
| 24 | + }) |
| 25 | + expect(value).toEqual(['baz']) |
| 26 | +}) |
| 27 | + |
| 28 | +test('should get the values to search on in a nested array', () => { |
| 29 | + const value = getValuesForKey('foo', { |
| 30 | + foo: ['bar', 'baz'] |
| 31 | + }) |
| 32 | + expect(value).toEqual(['bar', 'baz']) |
| 33 | +}) |
| 34 | + |
| 35 | +test('should get the values to search on in a nested array', () => { |
| 36 | + const value = getValuesForKey('foo.bar', { |
| 37 | + foo: [{ |
| 38 | + bar: 'baz' |
| 39 | + }, { |
| 40 | + bar: 'baz2' |
| 41 | + }] |
| 42 | + }) |
| 43 | + expect(value).toEqual(['baz', 'baz2']) |
| 44 | +}) |
| 45 | + |
| 46 | +test('should get the values to search on in a nested array with an index', () => { |
| 47 | + const value = getValuesForKey('foo.1.bar', { |
| 48 | + foo: [{ |
| 49 | + bar: 'baz' |
| 50 | + }, { |
| 51 | + bar: 'baz2' |
| 52 | + }] |
| 53 | + }) |
| 54 | + expect(value).toEqual(['baz2']) |
| 55 | +}) |
| 56 | + |
| 57 | +test('should ignore undefined values', () => { |
| 58 | + const value = getValuesForKey('fooz', { |
| 59 | + foo: [{ |
| 60 | + bar: 'baz' |
| 61 | + }, { |
| 62 | + bar: 'baz2' |
| 63 | + }] |
| 64 | + }) |
| 65 | + expect(value).toEqual([]) |
| 66 | +}) |
| 67 | + |
| 68 | +test('should get the values to search on in an immutable map', () => { |
| 69 | + const value = getValuesForKey('foo.bar', Map({ |
| 70 | + foo: { |
| 71 | + bar: 'baz' |
| 72 | + } |
| 73 | + })) |
| 74 | + expect(value).toEqual(['baz']) |
| 75 | +}) |
| 76 | + |
| 77 | +test('should ignore non-string and non-number values', () => { |
| 78 | + const value = getValuesForKey('foo.bar', { |
| 79 | + foo: [{ |
| 80 | + bar: [] |
| 81 | + }, { |
| 82 | + bar: [] |
| 83 | + }] |
| 84 | + }) |
| 85 | + expect(value).toEqual([]) |
| 86 | +}) |
0 commit comments