S.root.spec.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* globals S, describe, it, expect */
  2. describe("S.root()", function () {
  3. it("allows subcomputations to escape their parents", function () {
  4. S.root(function () {
  5. var outerTrigger = S.data(null),
  6. innerTrigger = S.data(null),
  7. outer,
  8. innerRuns = 0;
  9. outer = S(function () {
  10. // register dependency to outer trigger
  11. outerTrigger();
  12. // inner computation
  13. S.root(function () {
  14. S(function () {
  15. // register dependency on inner trigger
  16. innerTrigger();
  17. // count total runs
  18. innerRuns++;
  19. });
  20. });
  21. });
  22. // at start, we have one inner computation, that's run once
  23. expect(innerRuns).toBe(1);
  24. // trigger the outer computation, making more inners
  25. outerTrigger(null);
  26. outerTrigger(null);
  27. expect(innerRuns).toBe(3);
  28. // now trigger inner signal: three orphaned computations should equal three runs
  29. innerRuns = 0;
  30. innerTrigger(null);
  31. expect(innerRuns).toBe(3);
  32. });
  33. });
  34. //it("is necessary to create a toplevel computation", function () {
  35. // expect(() => {
  36. // S(() => 1)
  37. // }).toThrowError(/root/);
  38. //});
  39. it("does not freeze updates when used at top level", function () {
  40. S.root(() => {
  41. var s = S.data(1),
  42. c = S(() => s());
  43. expect(c()).toBe(1);
  44. s(2);
  45. expect(c()).toBe(2);
  46. s(3);
  47. expect(c()).toBe(3);
  48. });
  49. });
  50. it("persists through entire scope when used at top level", () => {
  51. S.root(() => {
  52. var s = S.data(1),
  53. c1 = S(() => s());
  54. s(2);
  55. var c2 = S(() => s());
  56. s(3);
  57. expect(c2()).toBe(3);
  58. });
  59. });
  60. });