|
| 1 | +import {test, expect} from 'vitest'; |
| 2 | +import {Path, Reference} from '..'; |
| 3 | +import {isArrayReference, isArrayEnd} from '../find'; |
| 4 | +import {parseJsonPointer} from '../util'; |
| 5 | + |
| 6 | +export const testFindRef = (find: (val: unknown, path: Path) => Reference) => { |
| 7 | + test('can find number root', () => { |
| 8 | + const res = find(123, []); |
| 9 | + expect(res.val).toBe(123); |
| 10 | + }); |
| 11 | + |
| 12 | + test('can find string root', () => { |
| 13 | + const res = find('foo', []); |
| 14 | + expect(res.val).toBe('foo'); |
| 15 | + }); |
| 16 | + |
| 17 | + test('can find key in object', () => { |
| 18 | + const res = find({foo: 'bar'}, ['foo']); |
| 19 | + expect(res.val).toBe('bar'); |
| 20 | + }); |
| 21 | + |
| 22 | + test('returns container object and key', () => { |
| 23 | + const res = find({foo: {bar: {baz: 'qux', a: 1}}}, ['foo', 'bar', 'baz']); |
| 24 | + expect(res).toEqual({ |
| 25 | + val: 'qux', |
| 26 | + obj: {baz: 'qux', a: 1}, |
| 27 | + key: 'baz', |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + test('can reference simple object key', () => { |
| 32 | + const doc = {a: 123}; |
| 33 | + const path = parseJsonPointer('/a'); |
| 34 | + const res = find(doc, path); |
| 35 | + expect(res).toEqual({ |
| 36 | + val: 123, |
| 37 | + obj: {a: 123}, |
| 38 | + key: 'a', |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + test('throws when referencing missing key with multiple steps', () => { |
| 43 | + const doc = {a: 123}; |
| 44 | + const path = parseJsonPointer('/b/c'); |
| 45 | + expect(() => find(doc, path)).toThrow(new Error('NOT_FOUND')); |
| 46 | + }); |
| 47 | + |
| 48 | + test('can reference array element', () => { |
| 49 | + const doc = {a: {b: [1, 2, 3]}}; |
| 50 | + const path = parseJsonPointer('/a/b/1'); |
| 51 | + const res = find(doc, path); |
| 52 | + expect(res).toEqual({ |
| 53 | + val: 2, |
| 54 | + obj: [1, 2, 3], |
| 55 | + key: 1, |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + test('can reference end of array', () => { |
| 60 | + const doc = {a: {b: [1, 2, 3]}}; |
| 61 | + const path = parseJsonPointer('/a/b/-'); |
| 62 | + const ref = find(doc, path); |
| 63 | + expect(ref).toEqual({ |
| 64 | + val: undefined, |
| 65 | + obj: [1, 2, 3], |
| 66 | + key: 3, |
| 67 | + }); |
| 68 | + expect(isArrayReference(ref)).toBe(true); |
| 69 | + if (isArrayReference(ref)) expect(isArrayEnd(ref)).toBe(true); |
| 70 | + }); |
| 71 | + |
| 72 | + test('throws when pointing past array boundary', () => { |
| 73 | + const doc = {a: {b: [1, 2, 3]}}; |
| 74 | + const path = parseJsonPointer('/a/b/-1'); |
| 75 | + expect(() => find(doc, path)).toThrow(new Error('INVALID_INDEX')); |
| 76 | + }); |
| 77 | + |
| 78 | + test('can point one element past array boundary', () => { |
| 79 | + const doc = {a: {b: [1, 2, 3]}}; |
| 80 | + const path = parseJsonPointer('/a/b/3'); |
| 81 | + const ref = find(doc, path); |
| 82 | + expect(ref).toEqual({ |
| 83 | + val: undefined, |
| 84 | + obj: [1, 2, 3], |
| 85 | + key: 3, |
| 86 | + }); |
| 87 | + expect(isArrayReference(ref)).toBe(true); |
| 88 | + if (isArrayReference(ref)) expect(isArrayEnd(ref)).toBe(true); |
| 89 | + }); |
| 90 | + |
| 91 | + test('can reference missing object key', () => { |
| 92 | + const doc = {foo: 123}; |
| 93 | + const path = parseJsonPointer('/bar'); |
| 94 | + const ref = find(doc, path); |
| 95 | + expect(ref).toEqual({ |
| 96 | + val: undefined, |
| 97 | + obj: {foo: 123}, |
| 98 | + key: 'bar', |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + test('can reference missing array key withing bounds', () => { |
| 103 | + const doc = {foo: 123, bar: [1, 2, 3]}; |
| 104 | + const path = parseJsonPointer('/bar/3'); |
| 105 | + const ref = find(doc, path); |
| 106 | + expect(ref).toEqual({ |
| 107 | + val: undefined, |
| 108 | + obj: [1, 2, 3], |
| 109 | + key: 3, |
| 110 | + }); |
| 111 | + }); |
| 112 | +}; |
0 commit comments