resource-request-test.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. var tap = require("tap");
  2. var ResourceRequest = require("../lib/ResourceRequest");
  3. tap.test("can be created", function(t) {
  4. var create = function() {
  5. var request = new ResourceRequest(undefined, Promise); // eslint-disable-line no-unused-vars
  6. };
  7. t.doesNotThrow(create);
  8. t.end();
  9. });
  10. tap.test("times out when created with a ttl", function(t) {
  11. var reject = function(err) {
  12. t.match(err, /ResourceRequest timed out/);
  13. t.end();
  14. };
  15. var resolve = function(r) {
  16. t.fail("should not resolve");
  17. };
  18. var request = new ResourceRequest(10, Promise); // eslint-disable-line no-unused-vars
  19. request.promise.then(resolve, reject);
  20. });
  21. tap.test("calls resolve when resolved", function(t) {
  22. var resource = {};
  23. var resolve = function(r) {
  24. t.equal(r, resource);
  25. t.end();
  26. };
  27. var reject = function(err) {
  28. t.error(err);
  29. };
  30. var request = new ResourceRequest(undefined, Promise);
  31. request.promise.then(resolve, reject);
  32. request.resolve(resource);
  33. });
  34. tap.test("removeTimeout removes the timeout", function(t) {
  35. var reject = function(err) {
  36. t.error(err);
  37. };
  38. var request = new ResourceRequest(10, Promise);
  39. request.promise.then(undefined, reject);
  40. request.removeTimeout();
  41. setTimeout(function() {
  42. t.end();
  43. }, 20);
  44. });
  45. tap.test("does nothing if resolved more than once", function(t) {
  46. var request = new ResourceRequest(undefined, Promise);
  47. t.doesNotThrow(function() {
  48. request.resolve({});
  49. });
  50. t.doesNotThrow(function() {
  51. request.resolve({});
  52. });
  53. t.end();
  54. });