UNPKG

falcor

Version:

A JavaScript library for efficient data fetching.

107 lines (91 loc) 4.2 kB
const complement = require("../../../lib/request/complement"); const findPartialIntersections = require("../../../lib/request/complement").__test.findPartialIntersections; describe("complement", () => { it("handles empty path sets", () => { expect(complement([], [], {})).toEqual({ intersection: [], optimizedComplement: [], requestedComplement: [] }); }); it("returns all paths if no deduping possible", () => { const paths = [["videos", 0, "title"]]; expect(complement(paths, paths, {})).toEqual({ intersection: [], optimizedComplement: paths, requestedComplement: paths }); }); it("returns the complement and intersection consisting of paths that can be partially deduped", () => { const partialMatchingPath = ["videos", [0, 1], "title"]; const paths = [partialMatchingPath]; const pathTree = { 3: { videos: { 0: { title: null } } } }; expect(complement(paths, paths, pathTree)).toEqual({ intersection: [["videos", 0, "title"]], optimizedComplement: [["videos", 1, "title"]], requestedComplement: [["videos", 1, "title"]] }); }); }); describe("findPartialIntersections", () => { let matchingPath; let matchingPathTree; beforeEach(() => { matchingPath = ["lolomo", 123, "summary"]; matchingPathTree = { lolomo: { 123: { summary: null } } }; }); it("returns paths if no deduping was possible", () => { const requestedPath = ["videos", 0, "title"]; const optimizedPath = ["videosById", 1232, "title"]; expect(findPartialIntersections(requestedPath, optimizedPath, {})).toEqual([ [], [optimizedPath], [requestedPath] ]); }); it("returns the intersection consisting of paths that can be fully deduped", () => { expect(findPartialIntersections(matchingPath, matchingPath, matchingPathTree)).toEqual([ [matchingPath], [], [] ]); }); describe("with optimized paths shorter than requested paths", () => { it("returns the complement and intersection consisting of paths than can be partially deduped", () => { const partialMatchingRequestedPath = ["lolomo", 123, 0, 0, ["title", "boxart"]]; const partialMatchingOptimizedPath = ["videosById", 456, ["title", "boxart"]]; const pathTree = { videosById: { 456: { title: null } } }; expect( findPartialIntersections(partialMatchingRequestedPath, partialMatchingOptimizedPath, pathTree) ).toEqual([ [["lolomo", 123, 0, 0, "title"]], [["videosById", 456, "boxart"]], [["lolomo", 123, 0, 0, "boxart"]] ]); }); }); describe("with optimized paths longer than requested paths", () => { it("returns the complement and intersection consisting of paths than can be partially deduped", () => { const partialMatchingRequestedPath = ["videos", 123, ["title", "boxart"]]; const partialMatchingOptimizedPath = ["some", "weird", "long", "ref", 456, ["title", "boxart"]]; const pathTree = { some: { weird: { long: { ref: { 456: { title: null } } } } } }; expect( findPartialIntersections(partialMatchingRequestedPath, partialMatchingOptimizedPath, pathTree) ).toEqual([ [["videos", 123, "title"]], [["some", "weird", "long", "ref", 456, "boxart"]], [["videos", 123, "boxart"]] ]); }); it("halts descent into the subtree", () => { const requestedPath = ["videos", 123, "title"]; const optimizedPath = ["some", "weird", "long", "ref", 456, "title"]; const pathTree = { some: { differentPath: null } }; expect(findPartialIntersections(requestedPath, optimizedPath, pathTree)).toEqual([ [], [optimizedPath], [requestedPath] ]); }); }); });