index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. 'use strict';
  2. var test = require('tape');
  3. var getSideChannel = require('../');
  4. test('export', function (t) {
  5. t.equal(typeof getSideChannel, 'function', 'is a function');
  6. t.equal(getSideChannel.length, 0, 'takes no arguments');
  7. var channel = getSideChannel();
  8. t.ok(channel, 'is truthy');
  9. t.equal(typeof channel, 'object', 'is an object');
  10. t.end();
  11. });
  12. test('assert', function (t) {
  13. var channel = getSideChannel();
  14. t['throws'](
  15. function () { channel.assert({}); },
  16. TypeError,
  17. 'nonexistent value throws'
  18. );
  19. var o = {};
  20. channel.set(o, 'data');
  21. t.doesNotThrow(function () { channel.assert(o); }, 'existent value noops');
  22. t.end();
  23. });
  24. test('has', function (t) {
  25. var channel = getSideChannel();
  26. var o = [];
  27. t.equal(channel.has(o), false, 'nonexistent value yields false');
  28. channel.set(o, 'foo');
  29. t.equal(channel.has(o), true, 'existent value yields true');
  30. t.end();
  31. });
  32. test('get', function (t) {
  33. var channel = getSideChannel();
  34. var o = {};
  35. t.equal(channel.get(o), undefined, 'nonexistent value yields undefined');
  36. var data = {};
  37. channel.set(o, data);
  38. t.equal(channel.get(o), data, '"get" yields data set by "set"');
  39. t.end();
  40. });
  41. test('set', function (t) {
  42. var channel = getSideChannel();
  43. var o = function () {};
  44. t.equal(channel.get(o), undefined, 'value not set');
  45. channel.set(o, 42);
  46. t.equal(channel.get(o), 42, 'value was set');
  47. channel.set(o, Infinity);
  48. t.equal(channel.get(o), Infinity, 'value was set again');
  49. var o2 = {};
  50. channel.set(o2, 17);
  51. t.equal(channel.get(o), Infinity, 'o is not modified');
  52. t.equal(channel.get(o2), 17, 'o2 is set');
  53. channel.set(o, 14);
  54. t.equal(channel.get(o), 14, 'o is modified');
  55. t.equal(channel.get(o2), 17, 'o2 is not modified');
  56. t.end();
  57. });