S.subcomputations.spec.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. describe("S() with subcomputations", function () {
  2. it("does not register a dependency on the subcomputation", function () {
  3. S.root(function () {
  4. var d = S.data(1),
  5. spy = jasmine.createSpy("spy"),
  6. gspy = jasmine.createSpy("gspy"),
  7. f = S(function () { spy(); var g = S(function () { gspy(); return d(); }); })
  8. spy.calls.reset();
  9. gspy.calls.reset();
  10. d(2);
  11. expect(gspy.calls.count()).toBe(1);
  12. expect(spy.calls.count()).toBe(0);
  13. });
  14. });
  15. describe("with child", function () {
  16. var d, e, fspy, f, gspy, g, h;
  17. function init() {
  18. d = S.data(1);
  19. e = S.data(2);
  20. fspy = jasmine.createSpy("fspy");
  21. gspy = jasmine.createSpy("gspy");
  22. f = S(function () {
  23. fspy();
  24. d();
  25. g = S(function () {
  26. gspy();
  27. return e();
  28. });
  29. });
  30. h = g;
  31. h();
  32. }
  33. it("creates child on initialization", function () {
  34. S.root(function () {
  35. init();
  36. expect(h).toEqual(jasmine.any(Function));
  37. expect(h()).toBe(2);
  38. });
  39. });
  40. it("does not depend on child's dependencies", function () {
  41. S.root(function () {
  42. init();
  43. e(3);
  44. expect(fspy.calls.count()).toBe(1);
  45. expect(gspy.calls.count()).toBe(2);
  46. });
  47. });
  48. it("disposes old child when updated", function () {
  49. S.root(function () {
  50. init();
  51. // re-evalue parent, thereby disposing stale g, which we've stored at h
  52. d(2);
  53. e(3);
  54. // h is now disposed
  55. expect(h()).toBe(2);
  56. });
  57. });
  58. it("disposes child when it is disposed", function () {
  59. S.root(function (dispose) {
  60. init();
  61. dispose();
  62. e(3);
  63. expect(g()).toBe(2);
  64. });
  65. });
  66. });
  67. describe("which disposes sub that's being updated", function () {
  68. it("propagates successfully", function () {
  69. S.root(function () {
  70. var a = S.data(1),
  71. b = S(function () {
  72. var c = S(function () { return a(); });
  73. a();
  74. return { c: c };
  75. }),
  76. d = S(function () {
  77. return b().c();
  78. });
  79. expect(d()).toBe(1);
  80. a(2);
  81. expect(d()).toBe(2);
  82. a(3);
  83. expect(d()).toBe(3);
  84. });
  85. });
  86. });
  87. describe("which disposes a sub with a dependee with a sub", function () {
  88. it("propagates successfully", function () {
  89. S.root(function () {
  90. var a = S.data(1),
  91. c,
  92. b = S(function () {
  93. c = S(function () {
  94. return a();
  95. });
  96. a();
  97. return { c : c };
  98. }),
  99. d = S(function () {
  100. c();
  101. var e = S(function () {
  102. return a();
  103. });
  104. return { e : e };
  105. });
  106. expect(d().e()).toBe(1);
  107. a(2);
  108. expect(d().e()).toBe(2);
  109. a(3);
  110. expect(d().e()).toBe(3);
  111. });
  112. });
  113. });
  114. });