S.on.spec.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* globals jasmine */
  2. describe("S.on(...)", function () {
  3. it("registers a dependency", function () {
  4. S.root(function () {
  5. var d = S.data(1),
  6. spy = jasmine.createSpy(),
  7. f = S.on(d, function () { spy(); });
  8. expect(spy.calls.count()).toBe(1);
  9. d(2);
  10. expect(spy.calls.count()).toBe(2);
  11. });
  12. });
  13. it("prohibits dynamic dependencies", function () {
  14. S.root(function () {
  15. var d = S.data(1),
  16. spy = jasmine.createSpy("spy"),
  17. s = S.on(function () {}, function () { spy(); return d(); });
  18. expect(spy.calls.count()).toBe(1);
  19. d(2);
  20. expect(spy.calls.count()).toBe(1);
  21. });
  22. });
  23. it("allows multiple dependencies", function () {
  24. S.root(function () {
  25. var a = S.data(1),
  26. b = S.data(2),
  27. c = S.data(3),
  28. spy = jasmine.createSpy(),
  29. f = S.on(function () { a(); b(); c(); }, function () { spy(); });
  30. expect(spy.calls.count()).toBe(1);
  31. a(4);
  32. b(5);
  33. c(6);
  34. expect(spy.calls.count()).toBe(4);
  35. });
  36. });
  37. it("allows an array of dependencies", function () {
  38. S.root(function () {
  39. var a = S.data(1),
  40. b = S.data(2),
  41. c = S.data(3),
  42. spy = jasmine.createSpy(),
  43. f = S.on([a, b, c], function () { spy(); });
  44. expect(spy.calls.count()).toBe(1);
  45. a(4);
  46. b(5);
  47. c(6);
  48. expect(spy.calls.count()).toBe(4);
  49. });
  50. });
  51. it("modifies its accumulator when reducing", function () {
  52. S.root(function () {
  53. var a = S.data(1),
  54. c = S.on(a, function (sum) { return sum + a(); }, 0);
  55. expect(c()).toBe(1);
  56. a(2);
  57. expect(c()).toBe(3);
  58. a(3);
  59. a(4);
  60. expect(c()).toBe(10);
  61. });
  62. });
  63. it("suppresses initial run when onchanges is true", function () {
  64. S.root(function () {
  65. var a = S.data(1),
  66. c = S.on(a, function () { return a() * 2; }, 0, true);
  67. expect(c()).toBe(0);
  68. a(2);
  69. expect(c()).toBe(4);
  70. });
  71. })
  72. });