ids-spec.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. 'use strict';
  2. var Ids = require('..');
  3. var expect = require('chai').expect;
  4. describe('IDs', function() {
  5. describe('creation', function() {
  6. it('should new construct', function() {
  7. expect(new Ids() instanceof Ids).to.be.true;
  8. });
  9. it('should functional construct', function() {
  10. expect(Ids() instanceof Ids).to.be.true;
  11. });
  12. });
  13. describe('#next', function() {
  14. it('should create custom length ids', function() {
  15. // given
  16. var ids = new Ids();
  17. // when
  18. var i1 = ids.next();
  19. // then
  20. expect(i1).to.be.defined;
  21. });
  22. it('should create id', function() {
  23. // given
  24. var ids = new Ids([ 32, 36, 1 ]);
  25. // when
  26. var i1 = ids.next();
  27. // then
  28. expect(i1.length).to.be.equal(7);
  29. });
  30. it('should create id with prefix', function() {
  31. // given
  32. var ids = new Ids([ 32, 36, 1 ]);
  33. // when
  34. var i1 = ids.nextPrefixed('ID_');
  35. // then
  36. expect(ids.assigned(i1)).to.be.true;
  37. expect(i1).to.match(/^ID_.{7}$/);
  38. expect(i1.length).to.be.equal(10);
  39. });
  40. it('should bind id to element', function() {
  41. // given
  42. var ids = new Ids();
  43. var element = {};
  44. // when
  45. var i1 = ids.next(element);
  46. // then
  47. expect(i1).to.be.defined;
  48. expect(ids.assigned(i1)).to.equal(element);
  49. });
  50. });
  51. describe('#claim', function() {
  52. it('should claim', function() {
  53. // given
  54. var ids = new Ids();
  55. // when
  56. ids.claim('foo');
  57. // then
  58. expect(ids.assigned('foo')).to.be.true;
  59. });
  60. it('should claim by element', function() {
  61. // given
  62. var ids = new Ids();
  63. var element = {};
  64. // when
  65. ids.claim('foo', element);
  66. // then
  67. expect(ids.assigned('foo')).to.equal(element);
  68. });
  69. });
  70. describe('#assigned', function() {
  71. it('should answer whether id got already used', function() {
  72. // given
  73. var ids = new Ids();
  74. // assume
  75. expect(ids.assigned('foo')).to.be.false;
  76. // when
  77. ids.claim('foo');
  78. // then
  79. expect(ids.assigned('foo')).to.be.true;
  80. });
  81. });
  82. describe('#clear', function() {
  83. it('should remove all assigned ids', function() {
  84. // given
  85. var ids = new Ids();
  86. var i1 = ids.next();
  87. var i2 = ids.next();
  88. ids.claim('foo');
  89. // when
  90. ids.clear();
  91. // then
  92. expect(ids.assigned(i1)).to.be.false;
  93. expect(ids.assigned(i2)).to.be.false;
  94. expect(ids.assigned('foo')).to.be.false;
  95. });
  96. it('should allow #claim after clear', function() {
  97. // given
  98. var ids = new Ids();
  99. ids.claim('foo');
  100. // clear
  101. ids.clear();
  102. // then
  103. expect(function() {
  104. ids.claim('foo');
  105. }).not.to.throw;
  106. });
  107. it('should allow #next after clear', function() {
  108. // given
  109. var ids = new Ids();
  110. ids.next();
  111. // clear
  112. ids.clear();
  113. // then
  114. expect(function() {
  115. ids.next();
  116. }).not.to.throw;
  117. });
  118. });
  119. describe('performance', function() {
  120. it('should generate 100000 ids', function() {
  121. // given
  122. var ids = new Ids();
  123. var time = new Date().getTime();
  124. // when
  125. for (var i = 0; i < 100000; i++) {
  126. ids.next();
  127. }
  128. var total = new Date().getTime() - time;
  129. expect(total).to.be.below(1000);
  130. });
  131. });
  132. describe('#unclaim', function() {
  133. it('should unclaim an id', function() {
  134. // given
  135. var ids = new Ids();
  136. ids.claim('foo');
  137. // if I unclaim id '1'
  138. ids.unclaim('foo');
  139. // then 1 is removed
  140. expect(ids.assigned('foo')).to.be.false;
  141. });
  142. it('should accept non-existing values', function() {
  143. // given
  144. var ids = new Ids();
  145. ids.claim('foo');
  146. // if I unclaim an unexisting id, it should not throw an error
  147. ids.unclaim('unexisting');
  148. // then 1 is not removed
  149. expect(ids.assigned('foo')).to.be.true;
  150. });
  151. });
  152. });