Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x | import type {Path, Reference} from '..';
import {isArrayReference, isArrayEnd} from '../find';
import {parseJsonPointer} from '../util';
export const testFindRef = (find: (val: unknown, path: Path) => Reference) => {
test('can find number root', () => {
const res = find(123, []);
expect(res.val).toBe(123);
});
test('can find string root', () => {
const res = find('foo', []);
expect(res.val).toBe('foo');
});
test('can find key in object', () => {
const res = find({foo: 'bar'}, ['foo']);
expect(res.val).toBe('bar');
});
test('returns container object and key', () => {
const res = find({foo: {bar: {baz: 'qux', a: 1}}}, ['foo', 'bar', 'baz']);
expect(res).toEqual({
val: 'qux',
obj: {baz: 'qux', a: 1},
key: 'baz',
});
});
test('can reference simple object key', () => {
const doc = {a: 123};
const path = parseJsonPointer('/a');
const res = find(doc, path);
expect(res).toEqual({
val: 123,
obj: {a: 123},
key: 'a',
});
});
test('throws when referencing missing key with multiple steps', () => {
const doc = {a: 123};
const path = parseJsonPointer('/b/c');
expect(() => find(doc, path)).toThrow(new Error('NOT_FOUND'));
});
test('can reference array element', () => {
const doc = {a: {b: [1, 2, 3]}};
const path = parseJsonPointer('/a/b/1');
const res = find(doc, path);
expect(res).toEqual({
val: 2,
obj: [1, 2, 3],
key: 1,
});
});
test('can reference end of array', () => {
const doc = {a: {b: [1, 2, 3]}};
const path = parseJsonPointer('/a/b/-');
const ref = find(doc, path);
expect(ref).toEqual({
val: undefined,
obj: [1, 2, 3],
key: 3,
});
expect(isArrayReference(ref)).toBe(true);
if (isArrayReference(ref)) expect(isArrayEnd(ref)).toBe(true);
});
test('throws when pointing past array boundary', () => {
const doc = {a: {b: [1, 2, 3]}};
const path = parseJsonPointer('/a/b/-1');
expect(() => find(doc, path)).toThrow(new Error('INVALID_INDEX'));
});
test('can point one element past array boundary', () => {
const doc = {a: {b: [1, 2, 3]}};
const path = parseJsonPointer('/a/b/3');
const ref = find(doc, path);
expect(ref).toEqual({
val: undefined,
obj: [1, 2, 3],
key: 3,
});
expect(isArrayReference(ref)).toBe(true);
if (isArrayReference(ref)) expect(isArrayEnd(ref)).toBe(true);
});
test('can reference missing object key', () => {
const doc = {foo: 123};
const path = parseJsonPointer('/bar');
const ref = find(doc, path);
expect(ref).toEqual({
val: undefined,
obj: {foo: 123},
key: 'bar',
});
});
test('can reference missing array key withing bounds', () => {
const doc = {foo: 123, bar: [1, 2, 3]};
const path = parseJsonPointer('/bar/3');
const ref = find(doc, path);
expect(ref).toEqual({
val: undefined,
obj: [1, 2, 3],
key: 3,
});
});
};
|