annotation_storage_spec.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Copyright 2020 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import { AnnotationStorage } from "../../src/display/annotation_storage.js";
  16. describe("AnnotationStorage", function () {
  17. describe("GetOrDefaultValue", function () {
  18. it("should get and set a new value in the annotation storage", function () {
  19. const annotationStorage = new AnnotationStorage();
  20. let value = annotationStorage.getValue("123A", {
  21. value: "hello world",
  22. }).value;
  23. expect(value).toEqual("hello world");
  24. annotationStorage.setValue("123A", {
  25. value: "hello world",
  26. });
  27. // the second argument is the default value to use
  28. // if the key isn't in the storage
  29. value = annotationStorage.getValue("123A", {
  30. value: "an other string",
  31. }).value;
  32. expect(value).toEqual("hello world");
  33. });
  34. it("should get set values and default ones in the annotation storage", function () {
  35. const annotationStorage = new AnnotationStorage();
  36. annotationStorage.setValue("123A", {
  37. value: "hello world",
  38. hello: "world",
  39. });
  40. const result = annotationStorage.getValue("123A", {
  41. value: "an other string",
  42. world: "hello",
  43. });
  44. expect(result).toEqual({
  45. value: "hello world",
  46. hello: "world",
  47. world: "hello",
  48. });
  49. });
  50. });
  51. describe("SetValue", function () {
  52. it("should set a new value in the annotation storage", function () {
  53. const annotationStorage = new AnnotationStorage();
  54. annotationStorage.setValue("123A", { value: "an other string" });
  55. const value = annotationStorage.getAll()["123A"].value;
  56. expect(value).toEqual("an other string");
  57. });
  58. it("should call onSetModified() if value is changed", function () {
  59. const annotationStorage = new AnnotationStorage();
  60. let called = false;
  61. const callback = function () {
  62. called = true;
  63. };
  64. annotationStorage.onSetModified = callback;
  65. annotationStorage.setValue("asdf", { value: "original" });
  66. expect(called).toBe(true);
  67. // changing value
  68. annotationStorage.setValue("asdf", { value: "modified" });
  69. expect(called).toBe(true);
  70. // not changing value
  71. called = false;
  72. annotationStorage.setValue("asdf", { value: "modified" });
  73. expect(called).toBe(false);
  74. });
  75. });
  76. describe("ResetModified", function () {
  77. it("should call onResetModified() if set", function () {
  78. const annotationStorage = new AnnotationStorage();
  79. let called = false;
  80. const callback = function () {
  81. called = true;
  82. };
  83. annotationStorage.onResetModified = callback;
  84. annotationStorage.setValue("asdf", { value: "original" });
  85. annotationStorage.resetModified();
  86. expect(called).toBe(true);
  87. called = false;
  88. // not changing value
  89. annotationStorage.setValue("asdf", { value: "original" });
  90. annotationStorage.resetModified();
  91. expect(called).toBe(false);
  92. // changing value
  93. annotationStorage.setValue("asdf", { value: "modified" });
  94. annotationStorage.resetModified();
  95. expect(called).toBe(true);
  96. });
  97. });
  98. });