123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- import { makeStrongCache } from "../lib/config/caching";
- describe("caching API", () => {
- it("should allow permacaching with .forever()", () => {
- let count = 0;
- const fn = makeStrongCache((arg, cache) => {
- cache.forever();
- return { arg, count: count++ };
- });
- expect(fn("one")).toEqual({ arg: "one", count: 0 });
- expect(fn("one")).toBe(fn("one"));
- expect(fn("two")).toEqual({ arg: "two", count: 1 });
- expect(fn("two")).toBe(fn("two"));
- expect(fn("one")).not.toEqual(fn("two"));
- });
- it("should allow disabling caching with .never()", () => {
- let count = 0;
- const fn = makeStrongCache((arg, cache) => {
- cache.never();
- return { arg, count: count++ };
- });
- expect(fn("one")).toEqual({ arg: "one", count: 0 });
- expect(fn("one")).toEqual({ arg: "one", count: 1 });
- expect(fn("one")).not.toEqual(fn("one"));
- expect(fn("two")).toEqual({ arg: "two", count: 4 });
- expect(fn("two")).toEqual({ arg: "two", count: 5 });
- expect(fn("two")).not.toEqual(fn("two"));
- expect(fn("one")).not.toEqual(fn("two"));
- });
- it("should allow caching based on a value with .using(fn)", () => {
- let count = 0;
- let other = "default";
- const fn = makeStrongCache((arg, cache) => {
- const val = cache.using(() => other);
- return { arg, val, count: count++ };
- });
- expect(fn("one")).toEqual({ arg: "one", val: "default", count: 0 });
- expect(fn("one")).toBe(fn("one"));
- expect(fn("two")).toEqual({ arg: "two", val: "default", count: 1 });
- expect(fn("two")).toBe(fn("two"));
- other = "new";
- expect(fn("one")).toEqual({ arg: "one", val: "new", count: 2 });
- expect(fn("one")).toBe(fn("one"));
- expect(fn("two")).toEqual({ arg: "two", val: "new", count: 3 });
- expect(fn("two")).toBe(fn("two"));
- other = "default";
- expect(fn("one")).toEqual({ arg: "one", val: "default", count: 0 });
- expect(fn("one")).toBe(fn("one"));
- expect(fn("two")).toEqual({ arg: "two", val: "default", count: 1 });
- expect(fn("two")).toBe(fn("two"));
- other = "new";
- expect(fn("one")).toEqual({ arg: "one", val: "new", count: 2 });
- expect(fn("one")).toBe(fn("one"));
- expect(fn("two")).toEqual({ arg: "two", val: "new", count: 3 });
- expect(fn("two")).toBe(fn("two"));
- });
- it("should allow invalidation based on a value with .invalidate(fn)", () => {
- let count = 0;
- let other = "default";
- const fn = makeStrongCache((arg, cache) => {
- const val = cache.invalidate(() => other);
- return { arg, val, count: count++ };
- });
- expect(fn("one")).toEqual({ arg: "one", val: "default", count: 0 });
- expect(fn("one")).toBe(fn("one"));
- expect(fn("two")).toEqual({ arg: "two", val: "default", count: 1 });
- expect(fn("two")).toBe(fn("two"));
- other = "new";
- expect(fn("one")).toEqual({ arg: "one", val: "new", count: 2 });
- expect(fn("one")).toBe(fn("one"));
- expect(fn("two")).toEqual({ arg: "two", val: "new", count: 3 });
- expect(fn("two")).toBe(fn("two"));
- other = "default";
- expect(fn("one")).toEqual({ arg: "one", val: "default", count: 4 });
- expect(fn("one")).toBe(fn("one"));
- expect(fn("two")).toEqual({ arg: "two", val: "default", count: 5 });
- expect(fn("two")).toBe(fn("two"));
- other = "new";
- expect(fn("one")).toEqual({ arg: "one", val: "new", count: 6 });
- expect(fn("one")).toBe(fn("one"));
- expect(fn("two")).toEqual({ arg: "two", val: "new", count: 7 });
- expect(fn("two")).toBe(fn("two"));
- });
- it("should allow invalidation with .using and .invalidate", () => {
- let count = 0;
- let other = "default";
- let another = "another";
- const fn = makeStrongCache((arg, cache) => {
- const val = cache.using(() => other);
- const val2 = cache.invalidate(() => another);
- return { arg, val, val2, count: count++ };
- });
- expect(fn("one")).toEqual({
- arg: "one",
- val: "default",
- val2: "another",
- count: 0,
- });
- expect(fn("one")).toBe(fn("one"));
- expect(fn("two")).toEqual({
- arg: "two",
- val: "default",
- val2: "another",
- count: 1,
- });
- expect(fn("two")).toBe(fn("two"));
- other = "new";
- expect(fn("one")).toEqual({
- arg: "one",
- val: "new",
- val2: "another",
- count: 2,
- });
- expect(fn("one")).toBe(fn("one"));
- expect(fn("two")).toEqual({
- arg: "two",
- val: "new",
- val2: "another",
- count: 3,
- });
- expect(fn("two")).toBe(fn("two"));
- other = "default";
- expect(fn("one")).toEqual({
- arg: "one",
- val: "default",
- val2: "another",
- count: 4,
- });
- expect(fn("one")).toBe(fn("one"));
- expect(fn("two")).toEqual({
- arg: "two",
- val: "default",
- val2: "another",
- count: 5,
- });
- expect(fn("two")).toBe(fn("two"));
- other = "new";
- expect(fn("one")).toEqual({
- arg: "one",
- val: "new",
- val2: "another",
- count: 6,
- });
- expect(fn("one")).toBe(fn("one"));
- expect(fn("two")).toEqual({
- arg: "two",
- val: "new",
- val2: "another",
- count: 7,
- });
- expect(fn("two")).toBe(fn("two"));
- another = "second";
- expect(fn("one")).toEqual({
- arg: "one",
- val: "new",
- val2: "second",
- count: 8,
- });
- expect(fn("one")).toBe(fn("one"));
- expect(fn("two")).toEqual({
- arg: "two",
- val: "new",
- val2: "second",
- count: 9,
- });
- expect(fn("two")).toBe(fn("two"));
- });
- it("should auto-permacache by default", () => {
- let count = 0;
- const fn = makeStrongCache(arg => ({ arg, count: count++ }));
- expect(fn("one")).toEqual({ arg: "one", count: 0 });
- expect(fn("one")).toBe(fn("one"));
- expect(fn("two")).toEqual({ arg: "two", count: 1 });
- expect(fn("two")).toBe(fn("two"));
- expect(fn("one")).not.toEqual(fn("two"));
- });
- it("should throw if you set permacaching and use .using", () => {
- const fn = makeStrongCache((arg, cache) => {
- cache.forever();
- cache.using(() => null);
- });
- expect(() => fn()).toThrow(/Caching has already been configured/);
- });
- it("should throw if you set permacaching and use .invalidate", () => {
- const fn = makeStrongCache((arg, cache) => {
- cache.forever();
- cache.invalidate(() => null);
- });
- expect(() => fn()).toThrow(/Caching has already been configured/);
- });
- it("should throw if you set permacaching and use .never", () => {
- const fn = makeStrongCache((arg, cache) => {
- cache.forever();
- cache.never();
- });
- expect(() => fn()).toThrow(/Caching has already been configured/);
- });
- it("should throw if you set no caching and use .using", () => {
- const fn = makeStrongCache((arg, cache) => {
- cache.never();
- cache.using(() => null);
- });
- expect(() => fn()).toThrow(/Caching has already been configured/);
- });
- it("should throw if you set no caching and use .invalidate", () => {
- const fn = makeStrongCache((arg, cache) => {
- cache.never();
- cache.invalidate(() => null);
- });
- expect(() => fn()).toThrow(/Caching has already been configured/);
- });
- it("should throw if you set no caching and use .never", () => {
- const fn = makeStrongCache((arg, cache) => {
- cache.never();
- cache.using(() => null);
- });
- expect(() => fn()).toThrow(/Caching has already been configured/);
- });
- it("should throw if you configure .forever after exiting", () => {
- const fn = makeStrongCache((arg, cache) => cache);
- expect(() => fn().forever()).toThrow(
- /Cannot change caching after evaluation/,
- );
- });
- it("should throw if you configure .never after exiting", () => {
- const fn = makeStrongCache((arg, cache) => cache);
- expect(() => fn().never()).toThrow(
- /Cannot change caching after evaluation/,
- );
- });
- it("should throw if you configure .using after exiting", () => {
- const fn = makeStrongCache((arg, cache) => cache);
- expect(() => fn().using(() => null)).toThrow(
- /Cannot change caching after evaluation/,
- );
- });
- it("should throw if you configure .invalidate after exiting", () => {
- const fn = makeStrongCache((arg, cache) => cache);
- expect(() => fn().invalidate(() => null)).toThrow(
- /Cannot change caching after evaluation/,
- );
- });
- describe("simple", () => {
- it("should allow permacaching with cache(true)", () => {
- let count = 0;
- const fn = makeStrongCache((arg, cache) => {
- cache = cache.simple();
- cache(true);
- return { arg, count: count++ };
- });
- expect(fn("one")).toEqual({ arg: "one", count: 0 });
- expect(fn("one")).toBe(fn("one"));
- expect(fn("two")).toEqual({ arg: "two", count: 1 });
- expect(fn("two")).toBe(fn("two"));
- expect(fn("one")).not.toEqual(fn("two"));
- });
- it("should allow disabling caching with cache(false)", () => {
- let count = 0;
- const fn = makeStrongCache((arg, cache) => {
- cache = cache.simple();
- cache(false);
- return { arg, count: count++ };
- });
- expect(fn("one")).toEqual({ arg: "one", count: 0 });
- expect(fn("one")).toEqual({ arg: "one", count: 1 });
- expect(fn("one")).not.toEqual(fn("one"));
- expect(fn("two")).toEqual({ arg: "two", count: 4 });
- expect(fn("two")).toEqual({ arg: "two", count: 5 });
- expect(fn("two")).not.toEqual(fn("two"));
- expect(fn("one")).not.toEqual(fn("two"));
- });
- it("should allow caching based on a value with cache(fn)", () => {
- let count = 0;
- let other = "default";
- const fn = makeStrongCache((arg, cache) => {
- cache = cache.simple();
- const val = cache(() => other);
- return { arg, val, count: count++ };
- });
- expect(fn("one")).toEqual({ arg: "one", val: "default", count: 0 });
- expect(fn("one")).toBe(fn("one"));
- expect(fn("two")).toEqual({ arg: "two", val: "default", count: 1 });
- expect(fn("two")).toBe(fn("two"));
- other = "new";
- expect(fn("one")).toEqual({ arg: "one", val: "new", count: 2 });
- expect(fn("one")).toBe(fn("one"));
- expect(fn("two")).toEqual({ arg: "two", val: "new", count: 3 });
- expect(fn("two")).toBe(fn("two"));
- other = "default";
- expect(fn("one")).toEqual({ arg: "one", val: "default", count: 0 });
- expect(fn("one")).toBe(fn("one"));
- expect(fn("two")).toEqual({ arg: "two", val: "default", count: 1 });
- expect(fn("two")).toBe(fn("two"));
- other = "new";
- expect(fn("one")).toEqual({ arg: "one", val: "new", count: 2 });
- expect(fn("one")).toBe(fn("one"));
- expect(fn("two")).toEqual({ arg: "two", val: "new", count: 3 });
- expect(fn("two")).toBe(fn("two"));
- });
- });
- });
|