http.publish.tests.client.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. function equals(a, b) {
  2. return !!(EJSON.stringify(a) === EJSON.stringify(b));
  3. }
  4. list = new Meteor.Collection('list');
  5. console.log('Client url: ' + Meteor.absoluteUrl('api'));
  6. Tinytest.add('http-publish - client - test environment', function(test) {
  7. test.isTrue(typeof _publishHTTP === 'undefined', 'test environment not initialized _publishHTTP');
  8. test.isTrue(typeof HTTP !== 'undefined', 'test environment not initialized HTTP');
  9. test.isTrue(typeof HTTP.publish !== 'undefined', 'test environment not initialized HTTP.publish');
  10. test.isTrue(typeof HTTP.unpublish !== 'undefined', 'test environment not initialized HTTP.unpublish');
  11. test.isTrue(typeof HTTP.publishFormats !== 'undefined', 'test environment not initialized HTTP.publishFormats');
  12. });
  13. Tinytest.addAsync('http-publish - client - clearTest', function (test, onComplete) {
  14. test.isTrue(true);
  15. Meteor.call('clearTest', function(err, result) {
  16. test.isTrue(result);
  17. onComplete();
  18. });
  19. test.isTrue(true);
  20. });
  21. id = '';
  22. removedId = '';
  23. Tinytest.addAsync('http-publish - client - get list', function (test, onComplete) {
  24. HTTP.get(Meteor.absoluteUrl('api/list'), function(err, result) {
  25. // Test the length of array result
  26. var len = result.data && result.data.length;
  27. test.isTrue(!!len, 'Result was empty');
  28. // Get the object
  29. var obj = result.data && result.data[0] || {};
  30. test.equal(obj.text, 'OK', 'Didnt get the expected result');
  31. // Set the id for the next test
  32. id = obj._id;
  33. onComplete();
  34. });
  35. });
  36. Tinytest.addAsync('http-publish - client - get list from custom prefix', function (test, onComplete) {
  37. // Now test the one we added with a custom prefix
  38. HTTP.get(Meteor.absoluteUrl('api2/list'), function(err, result) {
  39. // Test the length of array result
  40. var len = result.data && result.data.length;
  41. test.isTrue(!!len, 'Result was empty');
  42. // Get the object
  43. var obj = result.data && result.data[0] || {};
  44. test.equal(obj.text, 'OK', 'Didnt get the expected result');
  45. onComplete();
  46. });
  47. });
  48. Tinytest.addAsync('http-publish - client - unmountCustom', function (test, onComplete) {
  49. // Now unmount the methods with custom prefix
  50. test.isTrue(true);
  51. Meteor.call('unmountCustom', function(err, result) {
  52. test.isTrue(result);
  53. onComplete();
  54. });
  55. test.isTrue(true);
  56. });
  57. Tinytest.addAsync('http-publish - client - custom unmounted', function (test, onComplete) {
  58. // Now test the one we added with a custom prefix
  59. HTTP.get(Meteor.absoluteUrl('api2/list'), function(err, result) {
  60. test.isTrue(!!err, "Should have received an error since we unmounted the custom rest points");
  61. onComplete();
  62. });
  63. });
  64. Tinytest.addAsync('http-publish - client - put list', function (test, onComplete) {
  65. test.isTrue(id !== '', 'No id is set?');
  66. // Update the data
  67. HTTP.put(Meteor.absoluteUrl('api/list/' + id), {
  68. data: {
  69. $set: { text: 'UPDATED' }
  70. }
  71. }, function(err, result) {
  72. var resultId = result.data && result.data._id;
  73. test.isTrue(resultId !== undefined, 'Didnt get the expected id in result');
  74. // Check if data is updated
  75. HTTP.get(Meteor.absoluteUrl('api/list'), function(err, result) {
  76. var len = result.data && result.data.length;
  77. test.isTrue(!!len, 'Result was empty');
  78. var obj = result.data && result.data[0] || {};
  79. test.equal(obj.text, 'UPDATED', 'Didnt get the expected result');
  80. onComplete();
  81. });
  82. });
  83. });
  84. Tinytest.addAsync('http-publish - client - insert/remove list', function (test, onComplete) {
  85. // Insert a doc
  86. HTTP.post(Meteor.absoluteUrl('api/list'), {
  87. data: {
  88. text: 'INSERTED'
  89. }
  90. }, function(err, result) {
  91. var resultId = result.data && result.data._id;
  92. test.isTrue(resultId !== undefined, 'Didnt get the expected id in result');
  93. // Delete the doc
  94. HTTP.del(Meteor.absoluteUrl('api/list/' + resultId), function(err, result) {
  95. removedId = result.data && result.data._id;
  96. test.isTrue(removedId !== undefined, 'Didnt get the expected id in result');
  97. onComplete();
  98. });
  99. });
  100. });
  101. Tinytest.addAsync('http-publish - client - check removed', function (test, onComplete) {
  102. test.isTrue(removedId !== '', 'No removedId is set?');
  103. HTTP.get(Meteor.absoluteUrl('api/list/' + removedId), function(err, result) {
  104. var obj = result.data || {};
  105. test.isTrue(obj._id === undefined, 'Item was not removed');
  106. test.isTrue(err.response.statusCode === 404, 'Item was not removed');
  107. onComplete();
  108. });
  109. });
  110. Tinytest.addAsync('http-publish - client - check findOne', function (test, onComplete) {
  111. test.isTrue(id !== '', 'No id is set?');
  112. HTTP.get(Meteor.absoluteUrl('api/list/' + id), function(err, result) {
  113. var obj = result.data || {};
  114. test.isTrue(obj._id !== undefined, 'expected a document');
  115. test.isTrue(obj.text === 'UPDATED', 'expected text === UPDATED');
  116. onComplete();
  117. });
  118. });
  119. // Check if removedId found
  120. // Check if id still found
  121. //Test API:
  122. //test.isFalse(v, msg)
  123. //test.isTrue(v, msg)
  124. //test.equalactual, expected, message, not
  125. //test.length(obj, len)
  126. //test.include(s, v)
  127. //test.isNaN(v, msg)
  128. //test.isUndefined(v, msg)
  129. //test.isNotNull
  130. //test.isNull
  131. //test.throws(func)
  132. //test.instanceOf(obj, klass)
  133. //test.notEqual(actual, expected, message)
  134. //test.runId()
  135. //test.exception(exception)
  136. //test.expect_fail()
  137. //test.ok(doc)
  138. //test.fail(doc)
  139. //test.equal(a, b, msg)