PersistentCache.test.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import {GlobalOverrider} from "test/unit/utils";
  2. import {PersistentCache} from "lib/PersistentCache.jsm";
  3. describe("PersistentCache", () => {
  4. let fakeOS;
  5. let fakeJsonParse;
  6. let fakeFetch;
  7. let cache;
  8. let filename = "cache.json";
  9. let reportErrorStub;
  10. let globals;
  11. let sandbox;
  12. beforeEach(() => {
  13. globals = new GlobalOverrider();
  14. sandbox = sinon.createSandbox();
  15. fakeOS = {
  16. Constants: {Path: {localProfileDir: "/foo/bar"}},
  17. File: {
  18. writeAtomic: sinon.stub().returns(Promise.resolve()),
  19. },
  20. Path: {join: () => filename},
  21. };
  22. fakeJsonParse = sandbox.stub().resolves({});
  23. fakeFetch = sandbox.stub().resolves({json: fakeJsonParse});
  24. reportErrorStub = sandbox.stub();
  25. globals.set("OS", fakeOS);
  26. globals.set("Cu", {reportError: reportErrorStub});
  27. globals.set("fetch", fakeFetch);
  28. cache = new PersistentCache(filename);
  29. });
  30. afterEach(() => {
  31. globals.restore();
  32. sandbox.restore();
  33. });
  34. describe("#get", () => {
  35. it("tries to fetch the file", async () => {
  36. await cache.get("foo");
  37. assert.calledOnce(fakeFetch);
  38. });
  39. it("doesnt try to parse file if it doesn't exist", async () => {
  40. fakeFetch.throws();
  41. await cache.get("foo");
  42. assert.notCalled(fakeJsonParse);
  43. });
  44. it("doesnt try to fetch the file if it was already loaded", async () => {
  45. await cache._load();
  46. fakeFetch.resetHistory();
  47. await cache.get("foo");
  48. assert.notCalled(fakeFetch);
  49. });
  50. it("should catch and report errors", async () => {
  51. fakeJsonParse.throws();
  52. await cache._load();
  53. assert.calledOnce(reportErrorStub);
  54. });
  55. it("returns data for a given cache key", async () => {
  56. fakeJsonParse.resolves({foo: "bar"});
  57. let value = await cache.get("foo");
  58. assert.equal(value, "bar");
  59. });
  60. it("returns undefined for a cache key that doesn't exist", async () => {
  61. let value = await cache.get("baz");
  62. assert.equal(value, undefined);
  63. });
  64. it("returns all the data if no cache key is specified", async () => {
  65. fakeJsonParse.resolves({foo: "bar"});
  66. let value = await cache.get();
  67. assert.deepEqual(value, {foo: "bar"});
  68. });
  69. });
  70. describe("#set", () => {
  71. it("tries to fetch the file on the first set", async () => {
  72. await cache.set("foo", {x: 42});
  73. assert.calledOnce(fakeFetch);
  74. });
  75. it("doesnt try to fetch the file if it was already loaded", async () => {
  76. cache = new PersistentCache(filename, true);
  77. await cache._load();
  78. fakeFetch.resetHistory();
  79. await cache.set("foo", {x: 42});
  80. assert.notCalled(fakeFetch);
  81. });
  82. it("tries to fetch the file on the first set", async () => {
  83. await cache.set("foo", {x: 42});
  84. assert.calledOnce(fakeFetch);
  85. });
  86. it("sets a string value", async () => {
  87. const key = "testkey";
  88. const value = "testvalue";
  89. await cache.set(key, value);
  90. const cachedValue = await cache.get(key);
  91. assert.equal(cachedValue, value);
  92. });
  93. it("sets an object value", async () => {
  94. const key = "testkey";
  95. const value = {x: 1, y: 2, z: 3};
  96. await cache.set(key, value);
  97. const cachedValue = await cache.get(key);
  98. assert.deepEqual(cachedValue, value);
  99. });
  100. it("writes the data to file", async () => {
  101. const key = "testkey";
  102. const value = {x: 1, y: 2, z: 3};
  103. fakeOS.File.exists = async () => false;
  104. await cache.set(key, value);
  105. assert.calledOnce(fakeOS.File.writeAtomic);
  106. assert.calledWith(fakeOS.File.writeAtomic, filename, `{"testkey":{"x":1,"y":2,"z":3}}`, {tmpPath: `${filename}.tmp`});
  107. });
  108. });
  109. });