summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/src/utils/resource/tests
diff options
context:
space:
mode:
Diffstat (limited to 'devtools/client/debugger/src/utils/resource/tests')
-rw-r--r--devtools/client/debugger/src/utils/resource/tests/crud.spec.js266
-rw-r--r--devtools/client/debugger/src/utils/resource/tests/query.spec.js1079
2 files changed, 1345 insertions, 0 deletions
diff --git a/devtools/client/debugger/src/utils/resource/tests/crud.spec.js b/devtools/client/debugger/src/utils/resource/tests/crud.spec.js
new file mode 100644
index 0000000000..474323e6e5
--- /dev/null
+++ b/devtools/client/debugger/src/utils/resource/tests/crud.spec.js
@@ -0,0 +1,266 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
+
+// @flow
+declare var describe: (name: string, func: () => void) => void;
+declare var it: (desc: string, func: () => void) => void;
+declare var expect: (value: any) => any;
+
+import {
+ createInitial,
+ insertResources,
+ removeResources,
+ updateResources,
+ hasResource,
+ getResourceIds,
+ getResource,
+ getMappedResource,
+ type Resource,
+ type ResourceIdentity,
+} from "..";
+
+type TestResource = Resource<{
+ id: string,
+ name: string,
+ data: number,
+ obj: {},
+}>;
+
+const makeResource = (id: string): TestResource => ({
+ id,
+ name: `name-${id}`,
+ data: 42,
+ obj: {},
+});
+
+const mapName = (resource: TestResource) => resource.name;
+const mapWithIdent = (resource: TestResource, identity: ResourceIdentity) => ({
+ resource,
+ identity,
+ obj: {},
+});
+
+const clone = <T>(v: T): T => (JSON.parse((JSON.stringify(v): any)): any);
+
+describe("resource CRUD operations", () => {
+ let r1, r2, r3;
+ let originalInitial;
+ let initialState;
+ beforeEach(() => {
+ r1 = makeResource("id-1");
+ r2 = makeResource("id-2");
+ r3 = makeResource("id-3");
+
+ initialState = createInitial();
+ originalInitial = clone(initialState);
+ });
+
+ describe("insert", () => {
+ it("should work", () => {
+ const state = insertResources(initialState, [r1, r2, r3]);
+
+ expect(initialState).toEqual(originalInitial);
+ expect(getResource(state, r1.id)).toBe(r1);
+ expect(getResource(state, r2.id)).toBe(r2);
+ expect(getResource(state, r3.id)).toBe(r3);
+ });
+
+ it("should throw on duplicate", () => {
+ const state = insertResources(initialState, [r1]);
+ expect(() => {
+ insertResources(state, [r1]);
+ }).toThrow(/already exists/);
+
+ expect(() => {
+ insertResources(state, [r2, r2]);
+ }).toThrow(/already exists/);
+ });
+
+ it("should be a no-op when given no resources", () => {
+ const state = insertResources(initialState, []);
+
+ expect(state).toBe(initialState);
+ });
+ });
+
+ describe("read", () => {
+ beforeEach(() => {
+ initialState = insertResources(initialState, [r1, r2, r3]);
+ });
+
+ it("should allow reading all IDs", () => {
+ expect(getResourceIds(initialState)).toEqual([r1.id, r2.id, r3.id]);
+ });
+
+ it("should allow checking for existing of an ID", () => {
+ expect(hasResource(initialState, r1.id)).toBe(true);
+ expect(hasResource(initialState, r2.id)).toBe(true);
+ expect(hasResource(initialState, r3.id)).toBe(true);
+ expect(hasResource(initialState, "unknownId")).toBe(false);
+ });
+
+ it("should allow reading an item", () => {
+ expect(getResource(initialState, r1.id)).toBe(r1);
+ expect(getResource(initialState, r2.id)).toBe(r2);
+ expect(getResource(initialState, r3.id)).toBe(r3);
+
+ expect(() => {
+ getResource(initialState, "unknownId");
+ }).toThrow(/does not exist/);
+ });
+
+ it("should allow reading and mapping an item", () => {
+ expect(getMappedResource(initialState, r1.id, mapName)).toBe(r1.name);
+ expect(getMappedResource(initialState, r2.id, mapName)).toBe(r2.name);
+ expect(getMappedResource(initialState, r3.id, mapName)).toBe(r3.name);
+
+ expect(() => {
+ getMappedResource(initialState, "unknownId", mapName);
+ }).toThrow(/does not exist/);
+ });
+
+ it("should allow reading and mapping an item with identity", () => {
+ const r1Ident = getMappedResource(initialState, r1.id, mapWithIdent);
+ const r2Ident = getMappedResource(initialState, r2.id, mapWithIdent);
+
+ const state = updateResources(initialState, [{ ...r1, obj: {} }]);
+
+ const r1NewIdent = getMappedResource(state, r1.id, mapWithIdent);
+ const r2NewIdent = getMappedResource(state, r2.id, mapWithIdent);
+
+ // The update changed the resource object, but not the identity.
+ expect(r1NewIdent.resource).not.toBe(r1Ident.resource);
+ expect(r1NewIdent.resource).toEqual(r1Ident.resource);
+ expect(r1NewIdent.identity).toBe(r1Ident.identity);
+
+ // The update did not change the r2 resource.
+ expect(r2NewIdent.resource).toBe(r2Ident.resource);
+ expect(r2NewIdent.identity).toBe(r2Ident.identity);
+ });
+ });
+
+ describe("update", () => {
+ beforeEach(() => {
+ initialState = insertResources(initialState, [r1, r2, r3]);
+ originalInitial = clone(initialState);
+ });
+
+ it("should work", () => {
+ const r1Ident = getMappedResource(initialState, r1.id, mapWithIdent);
+ const r2Ident = getMappedResource(initialState, r2.id, mapWithIdent);
+ const r3Ident = getMappedResource(initialState, r3.id, mapWithIdent);
+
+ const state = updateResources(initialState, [
+ {
+ id: r1.id,
+ data: 21,
+ },
+ {
+ id: r2.id,
+ name: "newName",
+ },
+ ]);
+
+ expect(initialState).toEqual(originalInitial);
+ expect(getResource(state, r1.id)).toEqual({ ...r1, data: 21 });
+ expect(getResource(state, r2.id)).toEqual({ ...r2, name: "newName" });
+ expect(getResource(state, r3.id)).toBe(r3);
+
+ const r1NewIdent = getMappedResource(state, r1.id, mapWithIdent);
+ const r2NewIdent = getMappedResource(state, r2.id, mapWithIdent);
+ const r3NewIdent = getMappedResource(state, r3.id, mapWithIdent);
+
+ // The update changed the resource object, but not the identity.
+ expect(r1NewIdent.resource).not.toBe(r1Ident.resource);
+ expect(r1NewIdent.resource).toEqual({
+ ...r1Ident.resource,
+ data: 21,
+ });
+ expect(r1NewIdent.identity).toBe(r1Ident.identity);
+
+ // The update changed the resource object, but not the identity.
+ expect(r2NewIdent.resource).toEqual({
+ ...r2Ident.resource,
+ name: "newName",
+ });
+ expect(r2NewIdent.identity).toBe(r2Ident.identity);
+
+ // The update did not change the r3 resource.
+ expect(r3NewIdent.resource).toBe(r3Ident.resource);
+ expect(r3NewIdent.identity).toBe(r3Ident.identity);
+ });
+
+ it("should throw if not found", () => {
+ expect(() => {
+ updateResources(initialState, [
+ {
+ ...r1,
+ id: "unknownId",
+ },
+ ]);
+ }).toThrow(/does not exists/);
+ });
+
+ it("should be a no-op when new fields are strict-equal", () => {
+ const state = updateResources(initialState, [r1]);
+ expect(state).toBe(initialState);
+ });
+
+ it("should be a no-op when given no resources", () => {
+ const state = updateResources(initialState, []);
+ expect(state).toBe(initialState);
+ });
+ });
+
+ describe("delete", () => {
+ beforeEach(() => {
+ initialState = insertResources(initialState, [r1, r2, r3]);
+ originalInitial = clone(initialState);
+ });
+
+ it("should work with objects", () => {
+ const state = removeResources(initialState, [r1]);
+
+ expect(initialState).toEqual(originalInitial);
+ expect(hasResource(state, r1.id)).toBe(false);
+ expect(hasResource(state, r2.id)).toBe(true);
+ expect(hasResource(state, r3.id)).toBe(true);
+ });
+
+ it("should work with object subsets", () => {
+ const state = removeResources(initialState, [{ id: r1.id }]);
+
+ expect(initialState).toEqual(originalInitial);
+ expect(hasResource(state, r1.id)).toBe(false);
+ expect(hasResource(state, r2.id)).toBe(true);
+ expect(hasResource(state, r3.id)).toBe(true);
+ });
+
+ it("should work with ids", () => {
+ const state = removeResources(initialState, [r1.id]);
+
+ expect(initialState).toEqual(originalInitial);
+ expect(hasResource(state, r1.id)).toBe(false);
+ expect(hasResource(state, r2.id)).toBe(true);
+ expect(hasResource(state, r3.id)).toBe(true);
+ });
+
+ it("should throw if not found", () => {
+ expect(() => {
+ removeResources(initialState, [makeResource("unknownId")]);
+ }).toThrow(/does not exist/);
+ expect(() => {
+ removeResources(initialState, [{ id: "unknownId" }]);
+ }).toThrow(/does not exist/);
+ expect(() => {
+ removeResources(initialState, ["unknownId"]);
+ }).toThrow(/does not exist/);
+ });
+
+ it("should be a no-op when given no resources", () => {
+ const state = removeResources(initialState, []);
+ expect(state).toBe(initialState);
+ });
+ });
+});
diff --git a/devtools/client/debugger/src/utils/resource/tests/query.spec.js b/devtools/client/debugger/src/utils/resource/tests/query.spec.js
new file mode 100644
index 0000000000..adca73f750
--- /dev/null
+++ b/devtools/client/debugger/src/utils/resource/tests/query.spec.js
@@ -0,0 +1,1079 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
+
+// @flow
+
+import {
+ createInitial,
+ insertResources,
+ updateResources,
+ makeMapWithArgs,
+ makeWeakQuery,
+ makeShallowQuery,
+ makeStrictQuery,
+ type Id,
+ type Resource,
+ type ResourceValues,
+ type ResourceIdentity,
+ type QueryMapNoArgs,
+ type QueryMapWithArgs,
+ type QueryFilter,
+ type QueryReduce,
+} from "..";
+
+type TestResource = Resource<{
+ id: string,
+ name: string,
+ data: number,
+ obj: {},
+}>;
+
+const makeResource = (id: string): TestResource => ({
+ id,
+ name: `name-${id}`,
+ data: 42,
+ obj: {},
+});
+
+// Jest's mock type just wouldn't cooperate below, so this is a custom version
+// that does what I need.
+type MockedFn<InputFn, OutputFn> = OutputFn & {
+ mock: {
+ calls: Array<any>,
+ },
+ mockImplementation(fn: InputFn): void,
+};
+
+// We need to pass the 'needsArgs' prop through to the query fn so we use
+// this utility to do that and at the same time preserve typechecking.
+const mockFn = (f: any) => Object.assign((jest.fn(f): any), f);
+
+const mockFilter = <Args, F: QueryFilter<TestResource, Args>>(
+ callback: F
+): MockedFn<F, F> => mockFn(callback);
+const mockMapNoArgs = <Mapped, F: (TestResource, ResourceIdentity) => Mapped>(
+ callback: F
+): MockedFn<F, QueryMapNoArgs<TestResource, Mapped>> => mockFn(callback);
+const mockMapWithArgs = <
+ Args,
+ Mapped,
+ F: (TestResource, ResourceIdentity, Args) => Mapped
+>(
+ callback: F
+): MockedFn<F, QueryMapWithArgs<TestResource, Args, Mapped>> =>
+ mockFn(makeMapWithArgs(callback));
+const mockReduce = <
+ Args,
+ Mapped,
+ Reduced,
+ F: QueryReduce<TestResource, Args, Mapped, Reduced>
+>(
+ callback: F
+): MockedFn<F, F> => mockFn(callback);
+
+type TestArgs = Array<Id<TestResource>>;
+type TestReduced = { [Id<TestResource>]: TestResource };
+
+describe("resource query operations", () => {
+ let r1, r2, r3;
+ let initialState;
+ let mapNoArgs, mapWithArgs, reduce;
+
+ beforeEach(() => {
+ r1 = makeResource("id-1");
+ r2 = makeResource("id-2");
+ r3 = makeResource("id-3");
+
+ initialState = createInitial();
+
+ initialState = insertResources(initialState, [r1, r2, r3]);
+
+ mapNoArgs = mockMapNoArgs(
+ (resource: TestResource, ident: ResourceIdentity): TestResource =>
+ resource
+ );
+ mapWithArgs = mockMapWithArgs(
+ (
+ resource: TestResource,
+ ident: ResourceIdentity,
+ args: mixed
+ ): TestResource => resource
+ );
+ reduce = mockReduce(
+ (
+ mapped: $ReadOnlyArray<TestResource>,
+ ids: $ReadOnlyArray<string>,
+ args: mixed
+ ): TestReduced => {
+ return mapped.reduce((acc, item, i) => {
+ acc[ids[i]] = item;
+ return acc;
+ }, {});
+ }
+ );
+ });
+
+ describe("weak cache", () => {
+ let filter;
+
+ beforeEach(() => {
+ filter = mockFilter(
+ // eslint-disable-next-line max-nested-callbacks
+ (values: ResourceValues<TestResource>, args: TestArgs): TestArgs => args
+ );
+ });
+
+ describe("no args", () => {
+ let query;
+
+ beforeEach(() => {
+ query = makeWeakQuery({ filter, map: mapNoArgs, reduce });
+ });
+
+ it("should return same with same state and same args", () => {
+ const args = [r1.id, r2.id];
+ const result1 = query(initialState, args);
+ expect(result1).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ const result2 = query(initialState, args);
+ expect(result2).toBe(result1);
+ expect(result2).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+ });
+
+ it("should return same with updated other state and same args 1", () => {
+ const args = [r1.id, r2.id];
+ const result1 = query(initialState, args);
+ expect(result1).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ // Updating r2 does not affect cached result that only cares about r2.
+ const state = updateResources(initialState, [
+ {
+ id: r3.id,
+ obj: {},
+ },
+ ]);
+
+ const result2 = query(state, args);
+ expect(result2).toBe(result1);
+ expect(result2).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+ });
+
+ it("should return same with updated other state and same args 2", () => {
+ // eslint-disable-next-line max-nested-callbacks
+ mapNoArgs.mockImplementation(resource => ({ ...resource, name: "" }));
+
+ const args = [r1.id, r2.id];
+ const result1 = query(initialState, args);
+ expect(result1).toEqual({
+ [r1.id]: { ...r1, name: "" },
+ [r2.id]: { ...r2, name: "" },
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ // Since the map function ignores the name value, updating it should
+ // not reset the cached for this query.
+ const state = updateResources(initialState, [
+ {
+ id: r3.id,
+ name: "newName",
+ },
+ ]);
+
+ const result2 = query(state, args);
+ expect(result2).toBe(result1);
+ expect(result2).toEqual({
+ [r1.id]: { ...r1, name: "" },
+ [r2.id]: { ...r2, name: "" },
+ });
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+ });
+
+ it("should return diff with updated id state and same args", () => {
+ const args = [r1.id, r2.id];
+ const result1 = query(initialState, args);
+ expect(result1).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ // Since the mapper returns a value with name, changing a name will
+ // invalidate the cache.
+ const state = updateResources(initialState, [
+ {
+ id: r1.id,
+ name: "newName",
+ },
+ ]);
+
+ const result2 = query(state, args);
+ expect(result2).not.toBe(result1);
+ expect(result2).toEqual({
+ [r1.id]: { ...r1, name: "newName" },
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapNoArgs.mock.calls).toHaveLength(3);
+ expect(reduce.mock.calls).toHaveLength(2);
+ });
+
+ it("should return diff with same state and diff args", () => {
+ const firstArgs = [r1.id, r2.id];
+ const secondArgs = [r1.id, r2.id];
+ const result1 = query(initialState, firstArgs);
+ expect(result1).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ const result2 = query(initialState, secondArgs);
+ expect(result2).not.toBe(result1);
+ expect(result2).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(2);
+
+ // Same result from first query still available.
+ const result3 = query(initialState, firstArgs);
+ expect(result3).not.toBe(result2);
+ expect(result3).toBe(result1);
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(2);
+
+ // Same result from second query still available.
+ const result4 = query(initialState, secondArgs);
+ expect(result4).toBe(result2);
+ expect(result4).not.toBe(result3);
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(2);
+ });
+ });
+
+ describe("with args", () => {
+ let query;
+
+ beforeEach(() => {
+ query = makeWeakQuery({ filter, map: mapWithArgs, reduce });
+ });
+
+ it("should return same with same state and same args", () => {
+ const args = [r1.id, r2.id];
+ const result1 = query(initialState, args);
+ expect(result1).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapWithArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ const result2 = query(initialState, args);
+ expect(result2).toBe(result1);
+ expect(result2).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapWithArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+ });
+
+ it("should return same with updated other state and same args 1", () => {
+ const args = [r1.id, r2.id];
+ const result1 = query(initialState, args);
+ expect(result1).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapWithArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ // Updating r2 does not affect cached result that only cares about r2.
+ const state = updateResources(initialState, [
+ {
+ id: r3.id,
+ obj: {},
+ },
+ ]);
+
+ const result2 = query(state, args);
+ expect(result2).toBe(result1);
+ expect(result2).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapWithArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+ });
+
+ it("should return same with updated other state and same args 2", () => {
+ // eslint-disable-next-line max-nested-callbacks
+ mapWithArgs.mockImplementation(resource => ({ ...resource, name: "" }));
+
+ const args = [r1.id, r2.id];
+ const result1 = query(initialState, args);
+ expect(result1).toEqual({
+ [r1.id]: { ...r1, name: "" },
+ [r2.id]: { ...r2, name: "" },
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapWithArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ // Since the map function ignores the name value, updating it should
+ // not reset the cached for this query.
+ const state = updateResources(initialState, [
+ {
+ id: r3.id,
+ name: "newName",
+ },
+ ]);
+
+ const result2 = query(state, args);
+ expect(result2).toBe(result1);
+ expect(result2).toEqual({
+ [r1.id]: { ...r1, name: "" },
+ [r2.id]: { ...r2, name: "" },
+ });
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapWithArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+ });
+
+ it("should return diff with updated id state and same args", () => {
+ const args = [r1.id, r2.id];
+ const result1 = query(initialState, args);
+ expect(result1).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapWithArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ // Since the mapper returns a value with name, changing a name will
+ // invalidate the cache.
+ const state = updateResources(initialState, [
+ {
+ id: r1.id,
+ name: "newName",
+ },
+ ]);
+
+ const result2 = query(state, args);
+ expect(result2).not.toBe(result1);
+ expect(result2).toEqual({
+ [r1.id]: { ...r1, name: "newName" },
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapWithArgs.mock.calls).toHaveLength(3);
+ expect(reduce.mock.calls).toHaveLength(2);
+ });
+
+ it("should return diff with same state and diff args", () => {
+ const firstArgs = [r1.id, r2.id];
+ const secondArgs = [r1.id, r2.id];
+
+ const result1 = query(initialState, firstArgs);
+ expect(result1).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapWithArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ const result2 = query(initialState, secondArgs);
+ expect(result2).not.toBe(result1);
+ expect(result2).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapWithArgs.mock.calls).toHaveLength(4);
+ expect(reduce.mock.calls).toHaveLength(2);
+
+ // Same result from first query still available.
+ const result3 = query(initialState, firstArgs);
+ expect(result3).not.toBe(result2);
+ expect(result3).toBe(result1);
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapWithArgs.mock.calls).toHaveLength(4);
+ expect(reduce.mock.calls).toHaveLength(2);
+
+ // Same result from second query still available.
+ const result4 = query(initialState, secondArgs);
+ expect(result4).toBe(result2);
+ expect(result4).not.toBe(result3);
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapWithArgs.mock.calls).toHaveLength(4);
+ expect(reduce.mock.calls).toHaveLength(2);
+ });
+ });
+ });
+
+ describe("shallow cache", () => {
+ let filter;
+
+ beforeEach(() => {
+ filter = mockFilter(
+ // eslint-disable-next-line max-nested-callbacks
+ (
+ values: ResourceValues<TestResource>,
+ { ids }: { ids: TestArgs }
+ ): TestArgs => ids
+ );
+ });
+
+ describe("no args", () => {
+ let query;
+
+ beforeEach(() => {
+ query = makeShallowQuery({ filter, map: mapNoArgs, reduce });
+ });
+
+ it("should return last with same state and same args", () => {
+ const ids = [r1.id, r2.id];
+ const result1 = query(initialState, { ids });
+ expect(result1).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ const result2 = query(initialState, { ids });
+ expect(result2).toBe(result1);
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+ });
+
+ it("should return last with updated other state and same args 1", () => {
+ const ids = [r1.id, r2.id];
+ const result1 = query(initialState, { ids });
+ expect(result1).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ // Updating r2 does not affect cached result that only cares about r2.
+ const state = updateResources(initialState, [
+ {
+ id: r3.id,
+ obj: {},
+ },
+ ]);
+
+ const result2 = query(state, { ids });
+ expect(result2).toBe(result1);
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+ });
+
+ it("should return last with updated other state and same args 2", () => {
+ // eslint-disable-next-line max-nested-callbacks
+ mapNoArgs.mockImplementation(resource => ({ ...resource, name: "" }));
+
+ const ids = [r1.id, r2.id];
+ const result1 = query(initialState, { ids });
+ expect(result1).toEqual({
+ [r1.id]: { ...r1, name: "" },
+ [r2.id]: { ...r2, name: "" },
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ // Since the map function ignores the name value, updating it should
+ // not reset the cached for this query.
+ const state = updateResources(initialState, [
+ {
+ id: r3.id,
+ name: "newName",
+ },
+ ]);
+
+ const result2 = query(state, { ids });
+ expect(result2).toBe(result1);
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+ });
+
+ it("should return new with updated id state and same args", () => {
+ const ids = [r1.id, r2.id];
+ const result1 = query(initialState, { ids });
+ expect(result1).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ // Updating r2 does not affect cached result that only cares about r2.
+ const state = updateResources(initialState, [
+ {
+ id: r2.id,
+ name: "newName",
+ },
+ ]);
+
+ const result2 = query(state, { ids });
+ expect(result2).not.toBe(result1);
+ expect(result2).toEqual({
+ [r1.id]: r1,
+ [r2.id]: { ...r2, name: "newName" },
+ });
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapNoArgs.mock.calls).toHaveLength(3);
+ expect(reduce.mock.calls).toHaveLength(2);
+ });
+
+ it("should return diff with same state and diff args", () => {
+ const ids = [r1.id, r2.id];
+ const result1 = query(initialState, { ids, flag: true });
+ expect(result1).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ const result2 = query(initialState, { ids, flag: false });
+ expect(result2).toBe(result1);
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ const result3 = query(initialState, { ids, flag: true });
+ expect(result3).toBe(result1);
+ expect(filter.mock.calls).toHaveLength(3);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ const result4 = query(initialState, { ids, flag: false });
+ expect(result4).toBe(result1);
+ expect(filter.mock.calls).toHaveLength(4);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+ });
+ });
+
+ describe("with args", () => {
+ let query;
+
+ beforeEach(() => {
+ query = makeShallowQuery({ filter, map: mapWithArgs, reduce });
+ });
+
+ it("should return last with same state and same args", () => {
+ const ids = [r1.id, r2.id];
+ const result1 = query(initialState, { ids });
+ expect(result1).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapWithArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ const result2 = query(initialState, { ids });
+ expect(result2).toBe(result1);
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapWithArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+ });
+
+ it("should return last with updated other state and same args 1", () => {
+ const ids = [r1.id, r2.id];
+ const result1 = query(initialState, { ids });
+ expect(result1).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapWithArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ // Updating r2 does not affect cached result that only cares about r2.
+ const state = updateResources(initialState, [
+ {
+ id: r3.id,
+ obj: {},
+ },
+ ]);
+
+ const result2 = query(state, { ids });
+ expect(result2).toBe(result1);
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapWithArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+ });
+
+ it("should return last with updated other state and same args 2", () => {
+ // eslint-disable-next-line max-nested-callbacks
+ mapWithArgs.mockImplementation(resource => ({ ...resource, name: "" }));
+
+ const ids = [r1.id, r2.id];
+ const result1 = query(initialState, { ids });
+ expect(result1).toEqual({
+ [r1.id]: { ...r1, name: "" },
+ [r2.id]: { ...r2, name: "" },
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapWithArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ // Since the map function ignores the name value, updating it should
+ // not reset the cached for this query.
+ const state = updateResources(initialState, [
+ {
+ id: r3.id,
+ name: "newName",
+ },
+ ]);
+
+ const result2 = query(state, { ids });
+ expect(result2).toBe(result1);
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapWithArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+ });
+
+ it("should return new with updated id state and same args", () => {
+ const ids = [r1.id, r2.id];
+ const result1 = query(initialState, { ids });
+ expect(result1).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapWithArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ // Updating r2 does not affect cached result that only cares about r2.
+ const state = updateResources(initialState, [
+ {
+ id: r2.id,
+ name: "newName",
+ },
+ ]);
+
+ const result2 = query(state, { ids });
+ expect(result2).not.toBe(result1);
+ expect(result2).toEqual({
+ [r1.id]: r1,
+ [r2.id]: { ...r2, name: "newName" },
+ });
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapWithArgs.mock.calls).toHaveLength(3);
+ expect(reduce.mock.calls).toHaveLength(2);
+ });
+
+ it("should return diff with same state and diff args", () => {
+ const ids = [r1.id, r2.id];
+ const result1 = query(initialState, { ids, flag: true });
+ expect(result1).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapWithArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ const result2 = query(initialState, { ids, flag: false });
+ expect(result2).toBe(result1);
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapWithArgs.mock.calls).toHaveLength(4);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ const result3 = query(initialState, { ids, flag: true });
+ expect(result3).toBe(result1);
+ expect(filter.mock.calls).toHaveLength(3);
+ expect(mapWithArgs.mock.calls).toHaveLength(6);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ const result4 = query(initialState, { ids, flag: false });
+ expect(result4).toBe(result1);
+ expect(filter.mock.calls).toHaveLength(4);
+ expect(mapWithArgs.mock.calls).toHaveLength(8);
+ expect(reduce.mock.calls).toHaveLength(1);
+ });
+ });
+ });
+
+ describe("strict cache", () => {
+ let filter;
+
+ beforeEach(() => {
+ filter = mockFilter(
+ // eslint-disable-next-line max-nested-callbacks
+ (values: ResourceValues<TestResource>, ids: Array<string>): TestArgs =>
+ ids
+ );
+ });
+
+ describe("no args", () => {
+ let query;
+
+ beforeEach(() => {
+ query = makeStrictQuery({ filter, map: mapNoArgs, reduce });
+ });
+
+ it("should return same with same state and same args", () => {
+ const args = [r1.id, r2.id];
+ const result1 = query(initialState, args);
+ expect(result1).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ const result2 = query(initialState, args);
+ expect(result2).toBe(result1);
+ expect(result2).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+ });
+
+ it("should return same with updated other state and same args 1", () => {
+ const args = [r1.id, r2.id];
+ const result1 = query(initialState, args);
+ expect(result1).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ // Updating r2 does not affect cached result that only cares about r2.
+ const state = updateResources(initialState, [
+ {
+ id: r3.id,
+ obj: {},
+ },
+ ]);
+
+ const result2 = query(state, args);
+ expect(result2).toBe(result1);
+ expect(result2).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+ });
+
+ it("should return same with updated other state and same args 2", () => {
+ // eslint-disable-next-line max-nested-callbacks
+ mapNoArgs.mockImplementation(resource => ({ ...resource, name: "" }));
+
+ const args = [r1.id, r2.id];
+ const result1 = query(initialState, args);
+ expect(result1).toEqual({
+ [r1.id]: { ...r1, name: "" },
+ [r2.id]: { ...r2, name: "" },
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ // Since the map function ignores the name value, updating it should
+ // not reset the cached for this query.
+ const state = updateResources(initialState, [
+ {
+ id: r3.id,
+ name: "newName",
+ },
+ ]);
+
+ const result2 = query(state, args);
+ expect(result2).toBe(result1);
+ expect(result2).toEqual({
+ [r1.id]: { ...r1, name: "" },
+ [r2.id]: { ...r2, name: "" },
+ });
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+ });
+
+ it("should return diff with updated id state and same args", () => {
+ const args = [r1.id, r2.id];
+ const result1 = query(initialState, args);
+ expect(result1).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ // Since the mapper returns a value with name, changing a name will
+ // invalidate the cache.
+ const state = updateResources(initialState, [
+ {
+ id: r1.id,
+ name: "newName",
+ },
+ ]);
+
+ const result2 = query(state, args);
+ expect(result2).not.toBe(result1);
+ expect(result2).toEqual({
+ [r1.id]: { ...r1, name: "newName" },
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapNoArgs.mock.calls).toHaveLength(3);
+ expect(reduce.mock.calls).toHaveLength(2);
+ });
+
+ it("should return diff with same state and diff args", () => {
+ const firstArgs = [r1.id, r2.id];
+ const secondArgs = [r1.id, r2.id];
+ const result1 = query(initialState, firstArgs);
+ expect(result1).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ const result2 = query(initialState, secondArgs);
+ expect(result2).toBe(result1);
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ const result3 = query(initialState, firstArgs);
+ expect(result3).toBe(result2);
+ expect(filter.mock.calls).toHaveLength(3);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ const result4 = query(initialState, secondArgs);
+ expect(result4).toBe(result3);
+ expect(filter.mock.calls).toHaveLength(4);
+ expect(mapNoArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+ });
+ });
+
+ describe("with args", () => {
+ let query;
+
+ beforeEach(() => {
+ query = makeStrictQuery({ filter, map: mapWithArgs, reduce });
+ });
+
+ it("should return same with same state and same args", () => {
+ const args = [r1.id, r2.id];
+ const result1 = query(initialState, args);
+ expect(result1).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapWithArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ const result2 = query(initialState, args);
+ expect(result2).toBe(result1);
+ expect(result2).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapWithArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+ });
+
+ it("should return same with updated other state and same args 1", () => {
+ const args = [r1.id, r2.id];
+ const result1 = query(initialState, args);
+ expect(result1).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapWithArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ // Updating r2 does not affect cached result that only cares about r2.
+ const state = updateResources(initialState, [
+ {
+ id: r3.id,
+ obj: {},
+ },
+ ]);
+
+ const result2 = query(state, args);
+ expect(result2).toBe(result1);
+ expect(result2).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapWithArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+ });
+
+ it("should return same with updated other state and same args 2", () => {
+ // eslint-disable-next-line max-nested-callbacks
+ mapWithArgs.mockImplementation(resource => ({ ...resource, name: "" }));
+
+ const args = [r1.id, r2.id];
+ const result1 = query(initialState, args);
+ expect(result1).toEqual({
+ [r1.id]: { ...r1, name: "" },
+ [r2.id]: { ...r2, name: "" },
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapWithArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ // Since the map function ignores the name value, updating it should
+ // not reset the cached for this query.
+ const state = updateResources(initialState, [
+ {
+ id: r3.id,
+ name: "newName",
+ },
+ ]);
+
+ const result2 = query(state, args);
+ expect(result2).toBe(result1);
+ expect(result2).toEqual({
+ [r1.id]: { ...r1, name: "" },
+ [r2.id]: { ...r2, name: "" },
+ });
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapWithArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+ });
+
+ it("should return diff with updated id state and same args", () => {
+ const args = [r1.id, r2.id];
+ const result1 = query(initialState, args);
+ expect(result1).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapWithArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ // Since the mapper returns a value with name, changing a name will
+ // invalidate the cache.
+ const state = updateResources(initialState, [
+ {
+ id: r1.id,
+ name: "newName",
+ },
+ ]);
+
+ const result2 = query(state, args);
+ expect(result2).not.toBe(result1);
+ expect(result2).toEqual({
+ [r1.id]: { ...r1, name: "newName" },
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapWithArgs.mock.calls).toHaveLength(3);
+ expect(reduce.mock.calls).toHaveLength(2);
+ });
+
+ it("should return diff with same state and diff args", () => {
+ const firstArgs = [r1.id, r2.id];
+ const secondArgs = [r1.id, r2.id];
+
+ const result1 = query(initialState, firstArgs);
+ expect(result1).toEqual({
+ [r1.id]: r1,
+ [r2.id]: r2,
+ });
+ expect(filter.mock.calls).toHaveLength(1);
+ expect(mapWithArgs.mock.calls).toHaveLength(2);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ const result2 = query(initialState, secondArgs);
+ expect(result2).toBe(result1);
+ expect(filter.mock.calls).toHaveLength(2);
+ expect(mapWithArgs.mock.calls).toHaveLength(4);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ const result3 = query(initialState, firstArgs);
+ expect(result3).toBe(result2);
+ expect(filter.mock.calls).toHaveLength(3);
+ expect(mapWithArgs.mock.calls).toHaveLength(6);
+ expect(reduce.mock.calls).toHaveLength(1);
+
+ const result4 = query(initialState, secondArgs);
+ expect(result4).toBe(result3);
+ expect(filter.mock.calls).toHaveLength(4);
+ expect(mapWithArgs.mock.calls).toHaveLength(8);
+ expect(reduce.mock.calls).toHaveLength(1);
+ });
+ });
+ });
+});