|
| 1 | +import { PropertyMap } from '../dist/models'; |
1 | 2 | import stringify from '../src/stringify'; |
2 | 3 |
|
3 | 4 | describe('stringify ', () => { |
4 | | - it('returns nothing for a blank JSON string', () => { |
5 | | - const result = stringify({}, {}); |
| 5 | + const expectString = (obj: object, map: PropertyMap, str: string) => |
| 6 | + expect(stringify(obj, map).replace(/\s/g, '')).toEqual(str); |
6 | 7 |
|
7 | | - expect(result).toEqual('{}'); |
8 | | - }); |
| 8 | + it('returns nothing for a blank JSON string', |
| 9 | + () => expectString( |
| 10 | + {}, |
| 11 | + {}, |
| 12 | + '{}')); |
| 13 | + |
| 14 | + it('ignores properties not found in source', |
| 15 | + () => expectString( |
| 16 | + {}, |
| 17 | + { $: ['a'] }, |
| 18 | + '{}')); |
| 19 | + |
| 20 | + it('ignores properties not found in map', |
| 21 | + () => expectString( |
| 22 | + { a: '1', b: '2' }, |
| 23 | + { $: ['b'] }, |
| 24 | + '{"b":"2"}')); |
| 25 | + |
| 26 | + it('returns first level object properties in order', |
| 27 | + () => expectString( |
| 28 | + { a: 2, b: 1 }, |
| 29 | + { $: ['b', 'a'] }, |
| 30 | + '{"b":1,"a":2}')); |
| 31 | + |
| 32 | + it('returns first level array value in order', |
| 33 | + () => expectString( |
| 34 | + { a: ['2', 1, true] }, |
| 35 | + { $: ['a'] }, |
| 36 | + '{"a":["2",1,true]}')); |
| 37 | + |
| 38 | + it('returns nested [array] > [object] properties in expected order', |
| 39 | + () => expectString( |
| 40 | + { a: [1, { c: '3', d: '2' }] }, |
| 41 | + { '$': ['a'], '$.a.1': ['d', 'c'] }, |
| 42 | + '{"a":[1,{"d":"2","c":"3"}]}')); |
| 43 | + |
| 44 | + it('ignores nested [array] > [object] properties not found in map', |
| 45 | + () => expectString( |
| 46 | + { a: [1, { b: 2, c: 3 }, 4] }, |
| 47 | + { '$': ['a'], '$.a.1': ['c'] }, |
| 48 | + '{"a":[1,{"c":3},4]}')); |
| 49 | + |
| 50 | + it('ignores nested [array] > [object] properties not found in map', |
| 51 | + () => expectString( |
| 52 | + { a: [1, { b: 2, c: 3 }, 4] }, |
| 53 | + { $: ['a'] }, |
| 54 | + '{"a":[1,{},4]}')); |
| 55 | + |
| 56 | + it('returns nested [object] > [object] properties in expected order', |
| 57 | + () => expectString( |
| 58 | + { |
| 59 | + a: { |
| 60 | + b: { |
| 61 | + c: 3, d: 4 |
| 62 | + }, |
| 63 | + e: { |
| 64 | + f: 4, g: 5 |
| 65 | + }, |
| 66 | + h: 6 |
| 67 | + }, |
| 68 | + i: 7 |
| 69 | + }, |
| 70 | + { |
| 71 | + '$': ['i', 'a'], |
| 72 | + '$.a': ['e', 'h', 'b'], |
| 73 | + '$.a.e': ['g', 'f'], |
| 74 | + '$.a.b': ['d', 'c'] |
| 75 | + }, |
| 76 | + '{"i":7,"a":{"e":{"g":5,"f":4},"h":6,"b":{"d":4,"c":3}}}')); |
| 77 | + |
| 78 | + it('returns nested [object] > [array] > [object] > [array] > [object] properties in expected order', |
| 79 | + () => expectString( |
| 80 | + { |
| 81 | + a: { |
| 82 | + b: [8, { |
| 83 | + c: 9, |
| 84 | + d: [{ |
| 85 | + e: 12, |
| 86 | + f: { |
| 87 | + g: true, h: 'h' |
| 88 | + } |
| 89 | + }, 10] |
| 90 | + }, 11] |
| 91 | + }, |
| 92 | + i: 7 |
| 93 | + }, |
| 94 | + { |
| 95 | + '$': ['i', 'a'], |
| 96 | + '$.a': ['b'], |
| 97 | + '$.a.b.1': ['d', 'c'], |
| 98 | + '$.a.b.1.d.0': ['f', 'e'], |
| 99 | + '$.a.b.1.d.0.f': ['h', 'g'] |
| 100 | + }, |
| 101 | + '{"i":7,"a":{"b":[8,{"d":[{"f":{"h":"h","g":true},"e":12},10],"c":9},11]}}')); |
9 | 102 | }); |
0 commit comments