not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.1 KiB

  1. define(["sugar-web/bus"], function (bus) {
  2. 'use strict';
  3. describe("bus requests", function () {
  4. var client;
  5. function MockClient() {
  6. this.result = [];
  7. this.error = null;
  8. }
  9. MockClient.prototype.send = function (data) {
  10. var that = this;
  11. setTimeout(function () {
  12. var parsed = JSON.parse(data);
  13. var message = {
  14. data: JSON.stringify({
  15. result: that.result,
  16. error: that.error,
  17. id: parsed.id
  18. })
  19. };
  20. that.onMessage(message);
  21. }, 0);
  22. };
  23. MockClient.prototype.close = function () {};
  24. beforeEach(function () {
  25. client = new MockClient();
  26. bus.listen(client);
  27. });
  28. afterEach(function () {
  29. bus.close();
  30. client = null;
  31. });
  32. it("should receive a response", function () {
  33. var responseReceived;
  34. runs(function () {
  35. responseReceived = false;
  36. function onResponseReceived(error, result) {
  37. expect(error).toBeNull();
  38. expect(result).toEqual(["hello"]);
  39. responseReceived = true;
  40. }
  41. client.result = ["hello"];
  42. bus.sendMessage("hello", [], onResponseReceived);
  43. });
  44. waitsFor(function () {
  45. return responseReceived;
  46. }, "a response should be received");
  47. });
  48. it("should receive an error", function () {
  49. var errorReceived;
  50. runs(function () {
  51. errorReceived = false;
  52. function onResponseReceived(error, result) {
  53. expect(error).toEqual(jasmine.any(Error));
  54. expect(result).toBeNull();
  55. errorReceived = true;
  56. }
  57. client.result = null;
  58. client.error = new Error("error");
  59. bus.sendMessage("hello", [], onResponseReceived);
  60. });
  61. waitsFor(function () {
  62. return errorReceived;
  63. }, "an error should be received");
  64. });
  65. });
  66. describe("bus notifications", function () {
  67. var client;
  68. function MockClient() {
  69. this.params = null;
  70. }
  71. MockClient.prototype.send_notification = function (method, params) {
  72. var that = this;
  73. setTimeout(function () {
  74. var message = {
  75. data: JSON.stringify({
  76. method: method,
  77. params: that.params
  78. })
  79. };
  80. that.onMessage(message);
  81. }, 0);
  82. };
  83. MockClient.prototype.close = function () {};
  84. beforeEach(function () {
  85. client = new MockClient();
  86. bus.listen(client);
  87. });
  88. afterEach(function () {
  89. bus.close();
  90. client = null;
  91. });
  92. it("should receive a notification", function () {
  93. var notificationReceived;
  94. var notificationParams;
  95. var originalParams = {
  96. param1: true,
  97. param2: "foo"
  98. };
  99. runs(function () {
  100. notificationReceived = false;
  101. notificationParams = null;
  102. function onNotificationReceived(params) {
  103. notificationReceived = true;
  104. notificationParams = params;
  105. }
  106. bus.onNotification("hey.there", onNotificationReceived);
  107. client.params = originalParams;
  108. client.send_notification("hey.there");
  109. });
  110. waitsFor(function () {
  111. return notificationReceived;
  112. }, "a notification should be received");
  113. runs(function () {
  114. expect(notificationParams).toEqual(originalParams);
  115. });
  116. });
  117. });
  118. });