ActivityStreamStorage.test.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import {ActivityStreamStorage} from "lib/ActivityStreamStorage.jsm";
  2. import {GlobalOverrider} from "test/unit/utils";
  3. let overrider = new GlobalOverrider();
  4. describe("ActivityStreamStorage", () => {
  5. let sandbox;
  6. let indexedDB;
  7. let storage;
  8. beforeEach(() => {
  9. sandbox = sinon.createSandbox();
  10. indexedDB = {
  11. open: sandbox.stub().resolves({}),
  12. deleteDatabase: sandbox.stub().resolves(),
  13. };
  14. overrider.set({IndexedDB: indexedDB});
  15. storage = new ActivityStreamStorage({
  16. storeNames: ["storage_test"],
  17. telemetry: {handleUndesiredEvent: sandbox.stub()},
  18. });
  19. });
  20. afterEach(() => {
  21. sandbox.restore();
  22. });
  23. it("should throw if required arguments not provided", () => {
  24. assert.throws(() => new ActivityStreamStorage({telemetry: true}));
  25. });
  26. describe(".db", () => {
  27. it("should not throw an error when accessing db", async () => {
  28. assert.ok(storage.db);
  29. });
  30. it("should delete and recreate the db if opening db fails", async () => {
  31. const newDb = {};
  32. indexedDB.open.onFirstCall().rejects(new Error("fake error"));
  33. indexedDB.open.onSecondCall().resolves(newDb);
  34. const db = await storage.db;
  35. assert.calledOnce(indexedDB.deleteDatabase);
  36. assert.calledTwice(indexedDB.open);
  37. assert.equal(db, newDb);
  38. });
  39. });
  40. describe("#getDbTable", () => {
  41. let testStorage;
  42. let storeStub;
  43. beforeEach(() => {
  44. storeStub = {
  45. getAll: sandbox.stub().resolves(),
  46. get: sandbox.stub().resolves(),
  47. put: sandbox.stub().resolves(),
  48. };
  49. sandbox.stub(storage, "_getStore").resolves(storeStub);
  50. testStorage = storage.getDbTable("storage_test");
  51. });
  52. it("should reverse key value parameters for put", async () => {
  53. await testStorage.set("key", "value");
  54. assert.calledOnce(storeStub.put);
  55. assert.calledWith(storeStub.put, "value", "key");
  56. });
  57. it("should return the correct value for get", async () => {
  58. storeStub.get.withArgs("foo").resolves("foo");
  59. const result = await testStorage.get("foo");
  60. assert.calledOnce(storeStub.get);
  61. assert.equal(result, "foo");
  62. });
  63. it("should return the correct value for getAll", async () => {
  64. storeStub.getAll.resolves(["bar"]);
  65. const result = await testStorage.getAll();
  66. assert.calledOnce(storeStub.getAll);
  67. assert.deepEqual(result, ["bar"]);
  68. });
  69. it("should query the correct object store", async () => {
  70. await testStorage.get();
  71. assert.calledOnce(storage._getStore);
  72. assert.calledWithExactly(storage._getStore, "storage_test");
  73. });
  74. it("should throw if table is not found", () => {
  75. assert.throws(() => storage.getDbTable("undefined_store"));
  76. });
  77. });
  78. it("should get the correct objectStore when calling _getStore", async () => {
  79. const objectStoreStub = sandbox.stub();
  80. indexedDB.open.resolves({objectStore: objectStoreStub});
  81. await storage._getStore("foo");
  82. assert.calledOnce(objectStoreStub);
  83. assert.calledWithExactly(objectStoreStub, "foo", "readwrite");
  84. });
  85. it("should create a db with the correct store name", async () => {
  86. const dbStub = {createObjectStore: sandbox.stub(), objectStoreNames: {contains: sandbox.stub().returns(false)}};
  87. await storage.db;
  88. // call the cb with a stub
  89. indexedDB.open.args[0][2](dbStub);
  90. assert.calledOnce(dbStub.createObjectStore);
  91. assert.calledWithExactly(dbStub.createObjectStore, "storage_test");
  92. });
  93. it("should handle an array of object store names", async () => {
  94. storage = new ActivityStreamStorage({
  95. storeNames: ["store1", "store2"],
  96. telemetry: {},
  97. });
  98. const dbStub = {createObjectStore: sandbox.stub(), objectStoreNames: {contains: sandbox.stub().returns(false)}};
  99. await storage.db;
  100. // call the cb with a stub
  101. indexedDB.open.args[0][2](dbStub);
  102. assert.calledTwice(dbStub.createObjectStore);
  103. assert.calledWith(dbStub.createObjectStore, "store1");
  104. assert.calledWith(dbStub.createObjectStore, "store2");
  105. });
  106. it("should skip creating existing stores", async () => {
  107. storage = new ActivityStreamStorage({
  108. storeNames: ["store1", "store2"],
  109. telemetry: {},
  110. });
  111. const dbStub = {createObjectStore: sandbox.stub(), objectStoreNames: {contains: sandbox.stub().returns(true)}};
  112. await storage.db;
  113. // call the cb with a stub
  114. indexedDB.open.args[0][2](dbStub);
  115. assert.notCalled(dbStub.createObjectStore);
  116. });
  117. describe("#_requestWrapper", () => {
  118. it("should return a successful result", async () => {
  119. const result = await storage._requestWrapper(() => Promise.resolve("foo"));
  120. assert.equal(result, "foo");
  121. assert.notCalled(storage.telemetry.handleUndesiredEvent);
  122. });
  123. it("should report failures", async () => {
  124. try {
  125. await storage._requestWrapper(() => Promise.reject(new Error()));
  126. } catch (e) {
  127. assert.calledOnce(storage.telemetry.handleUndesiredEvent);
  128. }
  129. });
  130. });
  131. });