eventify.js 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946
  1. 'use strict'
  2. const assert = require('chai').assert
  3. const events = require('../../src/events')
  4. const Promise = require('bluebird')
  5. const spooks = require('spooks')
  6. const modulePath = '../../src/eventify'
  7. suite('eventify:', () => {
  8. let log
  9. setup(() => {
  10. log = {}
  11. })
  12. test('require does not throw', () => {
  13. assert.doesNotThrow(() => {
  14. require(modulePath)
  15. })
  16. })
  17. test('require returns function', () => {
  18. assert.isFunction(require(modulePath))
  19. })
  20. suite('require:', () => {
  21. let eventify
  22. setup(() => {
  23. eventify = require(modulePath)
  24. })
  25. test('eventify does not throw', () => {
  26. assert.doesNotThrow(() => {
  27. eventify()
  28. })
  29. })
  30. test('eventify returns EventEmitter', () => {
  31. assert.instanceOf(eventify(), require('events').EventEmitter)
  32. })
  33. test('EventEmitter is decorated with pause method', () => {
  34. assert.isFunction(eventify().pause)
  35. assert.lengthOf(eventify().pause, 0)
  36. })
  37. test('pause method returns continue function', () => {
  38. assert.isFunction(eventify().pause())
  39. assert.lengthOf(eventify().pause(), 0)
  40. })
  41. suite('undefined:', () => {
  42. setup(done => {
  43. const emitter = eventify(undefined)
  44. Object.keys(events).forEach(key => {
  45. emitter.on(events[key], spooks.fn({
  46. name: key,
  47. log: log
  48. }))
  49. })
  50. emitter.on(events.end, done)
  51. })
  52. test('end event occurred once', () => {
  53. assert.strictEqual(log.counts.end, 1)
  54. })
  55. test('end event was dispatched correctly', () => {
  56. assert.lengthOf(log.args.end[0], 1)
  57. assert.isUndefined(log.args.end[0][0])
  58. })
  59. test('array event did not occur', () => {
  60. assert.strictEqual(log.counts.array, 0)
  61. })
  62. test('object event did not occur', () => {
  63. assert.strictEqual(log.counts.object, 0)
  64. })
  65. test('property event did not occur', () => {
  66. assert.strictEqual(log.counts.property, 0)
  67. })
  68. test('string event did not occur', () => {
  69. assert.strictEqual(log.counts.string, 0)
  70. })
  71. test('number event did not occur', () => {
  72. assert.strictEqual(log.counts.number, 0)
  73. })
  74. test('literal event did not occur', () => {
  75. assert.strictEqual(log.counts.literal, 0)
  76. })
  77. test('endArray event did not occur', () => {
  78. assert.strictEqual(log.counts.endArray, 0)
  79. })
  80. test('endObject event did not occur', () => {
  81. assert.strictEqual(log.counts.endObject, 0)
  82. })
  83. test('error event did not occur', () => {
  84. assert.strictEqual(log.counts.error, 0)
  85. })
  86. test('endPrefix event did not occur', () => {
  87. assert.strictEqual(log.counts.endPrefix, 0)
  88. })
  89. })
  90. suite('function:', () => {
  91. setup(done => {
  92. const emitter = eventify(() => {})
  93. Object.keys(events).forEach(key => {
  94. emitter.on(events[key], spooks.fn({
  95. name: key,
  96. log: log
  97. }))
  98. })
  99. emitter.on(events.end, done)
  100. })
  101. test('end event occurred once', () => {
  102. assert.strictEqual(log.counts.end, 1)
  103. })
  104. test('array event did not occur', () => {
  105. assert.strictEqual(log.counts.array, 0)
  106. })
  107. test('object event did not occur', () => {
  108. assert.strictEqual(log.counts.object, 0)
  109. })
  110. test('property event did not occur', () => {
  111. assert.strictEqual(log.counts.property, 0)
  112. })
  113. test('string event did not occur', () => {
  114. assert.strictEqual(log.counts.string, 0)
  115. })
  116. test('number event did not occur', () => {
  117. assert.strictEqual(log.counts.number, 0)
  118. })
  119. test('literal event did not occur', () => {
  120. assert.strictEqual(log.counts.literal, 0)
  121. })
  122. test('endArray event did not occur', () => {
  123. assert.strictEqual(log.counts.endArray, 0)
  124. })
  125. test('endObject event did not occur', () => {
  126. assert.strictEqual(log.counts.endObject, 0)
  127. })
  128. test('error event did not occur', () => {
  129. assert.strictEqual(log.counts.error, 0)
  130. })
  131. test('endPrefix event did not occur', () => {
  132. assert.strictEqual(log.counts.endPrefix, 0)
  133. })
  134. })
  135. suite('symbol:', () => {
  136. setup(done => {
  137. const emitter = eventify(Symbol('foo'))
  138. Object.keys(events).forEach(key => {
  139. emitter.on(events[key], spooks.fn({
  140. name: key,
  141. log: log
  142. }))
  143. })
  144. emitter.on(events.end, done)
  145. })
  146. test('end event occurred once', () => {
  147. assert.strictEqual(log.counts.end, 1)
  148. })
  149. test('array event did not occur', () => {
  150. assert.strictEqual(log.counts.array, 0)
  151. })
  152. test('object event did not occur', () => {
  153. assert.strictEqual(log.counts.object, 0)
  154. })
  155. test('property event did not occur', () => {
  156. assert.strictEqual(log.counts.property, 0)
  157. })
  158. test('string event did not occur', () => {
  159. assert.strictEqual(log.counts.string, 0)
  160. })
  161. test('number event did not occur', () => {
  162. assert.strictEqual(log.counts.number, 0)
  163. })
  164. test('literal event did not occur', () => {
  165. assert.strictEqual(log.counts.literal, 0)
  166. })
  167. test('endArray event did not occur', () => {
  168. assert.strictEqual(log.counts.endArray, 0)
  169. })
  170. test('endObject event did not occur', () => {
  171. assert.strictEqual(log.counts.endObject, 0)
  172. })
  173. test('error event did not occur', () => {
  174. assert.strictEqual(log.counts.error, 0)
  175. })
  176. test('endPrefix event did not occur', () => {
  177. assert.strictEqual(log.counts.endPrefix, 0)
  178. })
  179. })
  180. suite('empty array:', () => {
  181. setup(done => {
  182. const emitter = eventify([])
  183. Object.keys(events).forEach(key => {
  184. emitter.on(events[key], spooks.fn({
  185. name: key,
  186. log: log
  187. }))
  188. })
  189. emitter.on(events.end, done)
  190. })
  191. test('array event occurred once', () => {
  192. assert.strictEqual(log.counts.array, 1)
  193. })
  194. test('array event was dispatched correctly', () => {
  195. assert.lengthOf(log.args.array[0], 1)
  196. assert.isUndefined(log.args.array[0][0])
  197. })
  198. test('endArray event occurred once', () => {
  199. assert.strictEqual(log.counts.endArray, 1)
  200. })
  201. test('endArray event was dispatched correctly', () => {
  202. assert.lengthOf(log.args.endArray[0], 1)
  203. assert.isUndefined(log.args.endArray[0][0])
  204. })
  205. test('end event occurred once', () => {
  206. assert.strictEqual(log.counts.end, 1)
  207. })
  208. test('object event did not occur', () => {
  209. assert.strictEqual(log.counts.object, 0)
  210. })
  211. test('property event did not occur', () => {
  212. assert.strictEqual(log.counts.property, 0)
  213. })
  214. test('string event did not occur', () => {
  215. assert.strictEqual(log.counts.string, 0)
  216. })
  217. test('number event did not occur', () => {
  218. assert.strictEqual(log.counts.number, 0)
  219. })
  220. test('literal event did not occur', () => {
  221. assert.strictEqual(log.counts.literal, 0)
  222. })
  223. test('endObject event did not occur', () => {
  224. assert.strictEqual(log.counts.endObject, 0)
  225. })
  226. test('error event did not occur', () => {
  227. assert.strictEqual(log.counts.error, 0)
  228. })
  229. test('endPrefix event did not occur', () => {
  230. assert.strictEqual(log.counts.endPrefix, 0)
  231. })
  232. })
  233. suite('empty object:', () => {
  234. setup(done => {
  235. const emitter = eventify({})
  236. Object.keys(events).forEach(key => {
  237. emitter.on(events[key], spooks.fn({
  238. name: key,
  239. log: log
  240. }))
  241. })
  242. emitter.on(events.end, done)
  243. })
  244. test('object event occurred once', () => {
  245. assert.strictEqual(log.counts.object, 1)
  246. })
  247. test('object event was dispatched correctly', () => {
  248. assert.lengthOf(log.args.object[0], 1)
  249. assert.isUndefined(log.args.object[0][0])
  250. })
  251. test('endObject event occurred once', () => {
  252. assert.strictEqual(log.counts.endObject, 1)
  253. })
  254. test('endObject event was dispatched correctly', () => {
  255. assert.lengthOf(log.args.endObject[0], 1)
  256. assert.isUndefined(log.args.endObject[0][0])
  257. })
  258. test('end event occurred once', () => {
  259. assert.strictEqual(log.counts.end, 1)
  260. })
  261. test('array event did not occur', () => {
  262. assert.strictEqual(log.counts.array, 0)
  263. })
  264. test('property event did not occur', () => {
  265. assert.strictEqual(log.counts.property, 0)
  266. })
  267. test('string event did not occur', () => {
  268. assert.strictEqual(log.counts.string, 0)
  269. })
  270. test('number event did not occur', () => {
  271. assert.strictEqual(log.counts.number, 0)
  272. })
  273. test('literal event did not occur', () => {
  274. assert.strictEqual(log.counts.literal, 0)
  275. })
  276. test('endArray event did not occur', () => {
  277. assert.strictEqual(log.counts.endArray, 0)
  278. })
  279. test('error event did not occur', () => {
  280. assert.strictEqual(log.counts.error, 0)
  281. })
  282. test('endPrefix event did not occur', () => {
  283. assert.strictEqual(log.counts.endPrefix, 0)
  284. })
  285. })
  286. suite('string:', () => {
  287. setup(done => {
  288. const emitter = eventify('foo')
  289. Object.keys(events).forEach(key => {
  290. emitter.on(events[key], spooks.fn({
  291. name: key,
  292. log: log
  293. }))
  294. })
  295. emitter.on(events.end, done)
  296. })
  297. test('string event occurred once', () => {
  298. assert.strictEqual(log.counts.string, 1)
  299. })
  300. test('string event was dispatched correctly', () => {
  301. assert.lengthOf(log.args.string[0], 1)
  302. assert.strictEqual(log.args.string[0][0], 'foo')
  303. })
  304. test('end event occurred once', () => {
  305. assert.strictEqual(log.counts.end, 1)
  306. })
  307. test('array event did not occur', () => {
  308. assert.strictEqual(log.counts.array, 0)
  309. })
  310. test('object event did not occur', () => {
  311. assert.strictEqual(log.counts.object, 0)
  312. })
  313. test('property event did not occur', () => {
  314. assert.strictEqual(log.counts.property, 0)
  315. })
  316. test('number event did not occur', () => {
  317. assert.strictEqual(log.counts.number, 0)
  318. })
  319. test('literal event did not occur', () => {
  320. assert.strictEqual(log.counts.literal, 0)
  321. })
  322. test('endArray event did not occur', () => {
  323. assert.strictEqual(log.counts.endArray, 0)
  324. })
  325. test('endObject event did not occur', () => {
  326. assert.strictEqual(log.counts.endObject, 0)
  327. })
  328. test('error event did not occur', () => {
  329. assert.strictEqual(log.counts.error, 0)
  330. })
  331. test('endPrefix event did not occur', () => {
  332. assert.strictEqual(log.counts.endPrefix, 0)
  333. })
  334. })
  335. suite('string with special characters:', () => {
  336. setup(done => {
  337. const emitter = eventify('foo\nbar\t"baz"')
  338. Object.keys(events).forEach(key => {
  339. emitter.on(events[key], spooks.fn({
  340. name: key,
  341. log: log
  342. }))
  343. })
  344. emitter.on(events.end, done)
  345. })
  346. test('string event occurred once', () => {
  347. assert.strictEqual(log.counts.string, 1)
  348. })
  349. test('string event was dispatched correctly', () => {
  350. assert.lengthOf(log.args.string[0], 1)
  351. assert.strictEqual(log.args.string[0][0], 'foo\\nbar\\t\\"baz\\"')
  352. })
  353. test('error event did not occur', () => {
  354. assert.strictEqual(log.counts.error, 0)
  355. })
  356. })
  357. suite('number:', () => {
  358. setup(done => {
  359. const emitter = eventify(42)
  360. Object.keys(events).forEach(key => {
  361. emitter.on(events[key], spooks.fn({
  362. name: key,
  363. log: log
  364. }))
  365. })
  366. emitter.on(events.end, done)
  367. })
  368. test('number event occurred once', () => {
  369. assert.strictEqual(log.counts.number, 1)
  370. })
  371. test('number event was dispatched correctly', () => {
  372. assert.lengthOf(log.args.number[0], 1)
  373. assert.strictEqual(log.args.number[0][0], 42)
  374. })
  375. test('end event occurred once', () => {
  376. assert.strictEqual(log.counts.end, 1)
  377. })
  378. test('array event did not occur', () => {
  379. assert.strictEqual(log.counts.array, 0)
  380. })
  381. test('object event did not occur', () => {
  382. assert.strictEqual(log.counts.object, 0)
  383. })
  384. test('property event did not occur', () => {
  385. assert.strictEqual(log.counts.property, 0)
  386. })
  387. test('string event did not occur', () => {
  388. assert.strictEqual(log.counts.string, 0)
  389. })
  390. test('literal event did not occur', () => {
  391. assert.strictEqual(log.counts.literal, 0)
  392. })
  393. test('endArray event did not occur', () => {
  394. assert.strictEqual(log.counts.endArray, 0)
  395. })
  396. test('endObject event did not occur', () => {
  397. assert.strictEqual(log.counts.endObject, 0)
  398. })
  399. test('error event did not occur', () => {
  400. assert.strictEqual(log.counts.error, 0)
  401. })
  402. test('endPrefix event did not occur', () => {
  403. assert.strictEqual(log.counts.endPrefix, 0)
  404. })
  405. })
  406. suite('false:', () => {
  407. setup(done => {
  408. const emitter = eventify(false)
  409. Object.keys(events).forEach(key => {
  410. emitter.on(events[key], spooks.fn({
  411. name: key,
  412. log: log
  413. }))
  414. })
  415. emitter.on(events.end, done)
  416. })
  417. test('literal event occurred once', () => {
  418. assert.strictEqual(log.counts.literal, 1)
  419. })
  420. test('literal event was dispatched correctly', () => {
  421. assert.lengthOf(log.args.literal[0], 1)
  422. assert.isFalse(log.args.literal[0][0])
  423. })
  424. test('end event occurred once', () => {
  425. assert.strictEqual(log.counts.end, 1)
  426. })
  427. test('array event did not occur', () => {
  428. assert.strictEqual(log.counts.array, 0)
  429. })
  430. test('object event did not occur', () => {
  431. assert.strictEqual(log.counts.object, 0)
  432. })
  433. test('property event did not occur', () => {
  434. assert.strictEqual(log.counts.property, 0)
  435. })
  436. test('string event did not occur', () => {
  437. assert.strictEqual(log.counts.string, 0)
  438. })
  439. test('number event did not occur', () => {
  440. assert.strictEqual(log.counts.number, 0)
  441. })
  442. test('endArray event did not occur', () => {
  443. assert.strictEqual(log.counts.endArray, 0)
  444. })
  445. test('endObject event did not occur', () => {
  446. assert.strictEqual(log.counts.endObject, 0)
  447. })
  448. test('error event did not occur', () => {
  449. assert.strictEqual(log.counts.error, 0)
  450. })
  451. test('endPrefix event did not occur', () => {
  452. assert.strictEqual(log.counts.endPrefix, 0)
  453. })
  454. })
  455. suite('true:', () => {
  456. setup(done => {
  457. const emitter = eventify(true)
  458. Object.keys(events).forEach(key => {
  459. emitter.on(events[key], spooks.fn({
  460. name: key,
  461. log: log
  462. }))
  463. })
  464. emitter.on(events.end, done)
  465. })
  466. test('literal event occurred once', () => {
  467. assert.strictEqual(log.counts.literal, 1)
  468. })
  469. test('literal event was dispatched correctly', () => {
  470. assert.isTrue(log.args.literal[0][0])
  471. })
  472. test('end event occurred once', () => {
  473. assert.strictEqual(log.counts.end, 1)
  474. })
  475. test('error event did not occur', () => {
  476. assert.strictEqual(log.counts.error, 0)
  477. })
  478. })
  479. suite('null:', () => {
  480. setup(done => {
  481. const emitter = eventify(null)
  482. Object.keys(events).forEach(key => {
  483. emitter.on(events[key], spooks.fn({
  484. name: key,
  485. log: log
  486. }))
  487. })
  488. emitter.on(events.end, done)
  489. })
  490. test('literal event occurred once', () => {
  491. assert.strictEqual(log.counts.literal, 1)
  492. })
  493. test('literal event was dispatched correctly', () => {
  494. assert.isNull(log.args.literal[0][0])
  495. })
  496. test('end event occurred once', () => {
  497. assert.strictEqual(log.counts.end, 1)
  498. })
  499. test('error event did not occur', () => {
  500. assert.strictEqual(log.counts.error, 0)
  501. })
  502. })
  503. suite('array with items:', () => {
  504. setup(done => {
  505. const emitter = eventify([ undefined, 'foo', () => {}, 'bar', Symbol('baz') ])
  506. Object.keys(events).forEach(key => {
  507. emitter.on(events[key], spooks.fn({
  508. name: key,
  509. log: log
  510. }))
  511. })
  512. emitter.on(events.end, done)
  513. })
  514. test('array event occurred once', () => {
  515. assert.strictEqual(log.counts.array, 1)
  516. })
  517. test('literal event occurred three times', () => {
  518. assert.strictEqual(log.counts.literal, 3)
  519. })
  520. test('literal event was dispatched correctly first time', () => {
  521. assert.isNull(log.args.literal[0][0])
  522. })
  523. test('literal event was dispatched correctly second time', () => {
  524. assert.isNull(log.args.literal[1][0])
  525. })
  526. test('literal event was dispatched correctly third time', () => {
  527. assert.isNull(log.args.literal[2][0])
  528. })
  529. test('string event occurred twice', () => {
  530. assert.strictEqual(log.counts.string, 2)
  531. })
  532. test('string event was dispatched correctly first time', () => {
  533. assert.strictEqual(log.args.string[0][0], 'foo')
  534. })
  535. test('string event was dispatched correctly second time', () => {
  536. assert.strictEqual(log.args.string[1][0], 'bar')
  537. })
  538. test('endArray event occurred once', () => {
  539. assert.strictEqual(log.counts.endArray, 1)
  540. })
  541. test('end event occurred once', () => {
  542. assert.strictEqual(log.counts.end, 1)
  543. })
  544. test('error event did not occur', () => {
  545. assert.strictEqual(log.counts.error, 0)
  546. })
  547. })
  548. suite('object with properties:', () => {
  549. setup(done => {
  550. const emitter = eventify({ foo: 42,
  551. bar: undefined,
  552. baz: 3.14159265359,
  553. qux: Symbol('qux')
  554. })
  555. Object.keys(events).forEach(key => {
  556. emitter.on(events[key], spooks.fn({
  557. name: key,
  558. log: log
  559. }))
  560. })
  561. emitter.on(events.end, done)
  562. })
  563. test('object event occurred once', () => {
  564. assert.strictEqual(log.counts.object, 1)
  565. })
  566. test('property event occurred twice', () => {
  567. assert.strictEqual(log.counts.property, 2)
  568. })
  569. test('property event was dispatched correctly first time', () => {
  570. assert.strictEqual(log.args.property[0][0], 'foo')
  571. })
  572. test('property event was dispatched correctly second time', () => {
  573. assert.strictEqual(log.args.property[1][0], 'baz')
  574. })
  575. test('number event occurred twice', () => {
  576. assert.strictEqual(log.counts.number, 2)
  577. })
  578. test('number event was dispatched correctly first time', () => {
  579. assert.strictEqual(log.args.number[0][0], 42)
  580. })
  581. test('number event was dispatched correctly second time', () => {
  582. assert.strictEqual(log.args.number[1][0], 3.14159265359)
  583. })
  584. test('endObject event occurred once', () => {
  585. assert.strictEqual(log.counts.endObject, 1)
  586. })
  587. test('end event occurred once', () => {
  588. assert.strictEqual(log.counts.end, 1)
  589. })
  590. test('literal event did not occur', () => {
  591. assert.strictEqual(log.counts.literal, 0)
  592. })
  593. test('error event did not occur', () => {
  594. assert.strictEqual(log.counts.error, 0)
  595. })
  596. })
  597. suite('nested array:', () => {
  598. setup(done => {
  599. const emitter = eventify([ 'foo', [ 'bar', [ 'baz', 'qux' ] ] ])
  600. Object.keys(events).forEach(key => {
  601. emitter.on(events[key], spooks.fn({
  602. name: key,
  603. log: log
  604. }))
  605. })
  606. emitter.on(events.end, done)
  607. })
  608. test('array event occurred three times', () => {
  609. assert.strictEqual(log.counts.array, 3)
  610. })
  611. test('string event occurred four times', () => {
  612. assert.strictEqual(log.counts.string, 4)
  613. })
  614. test('string event was dispatched correctly first time', () => {
  615. assert.strictEqual(log.args.string[0][0], 'foo')
  616. })
  617. test('string event was dispatched correctly second time', () => {
  618. assert.strictEqual(log.args.string[1][0], 'bar')
  619. })
  620. test('string event was dispatched correctly third time', () => {
  621. assert.strictEqual(log.args.string[2][0], 'baz')
  622. })
  623. test('string event was dispatched correctly fourth time', () => {
  624. assert.strictEqual(log.args.string[3][0], 'qux')
  625. })
  626. test('endArray event occurred three times', () => {
  627. assert.strictEqual(log.counts.endArray, 3)
  628. })
  629. test('end event occurred once', () => {
  630. assert.strictEqual(log.counts.end, 1)
  631. })
  632. test('error event did not occur', () => {
  633. assert.strictEqual(log.counts.error, 0)
  634. })
  635. })
  636. suite('nested object:', () => {
  637. setup(done => {
  638. const emitter = eventify({ foo: { bar: { baz: 1, qux: 2 }, wibble: 3 }, wobble: 4 })
  639. Object.keys(events).forEach(key => {
  640. emitter.on(events[key], spooks.fn({
  641. name: key,
  642. log: log
  643. }))
  644. })
  645. emitter.on(events.end, done)
  646. })
  647. test('object event occurred three times', () => {
  648. assert.strictEqual(log.counts.object, 3)
  649. })
  650. test('property event occurred six times', () => {
  651. assert.strictEqual(log.counts.property, 6)
  652. })
  653. test('property event was dispatched correctly first time', () => {
  654. assert.strictEqual(log.args.property[0][0], 'foo')
  655. })
  656. test('property event was dispatched correctly second time', () => {
  657. assert.strictEqual(log.args.property[1][0], 'bar')
  658. })
  659. test('property event was dispatched correctly third time', () => {
  660. assert.strictEqual(log.args.property[2][0], 'baz')
  661. })
  662. test('property event was dispatched correctly fourth time', () => {
  663. assert.strictEqual(log.args.property[3][0], 'qux')
  664. })
  665. test('property event was dispatched correctly fifth time', () => {
  666. assert.strictEqual(log.args.property[4][0], 'wibble')
  667. })
  668. test('property event was dispatched correctly sixth time', () => {
  669. assert.strictEqual(log.args.property[5][0], 'wobble')
  670. })
  671. test('number event occurred four times', () => {
  672. assert.strictEqual(log.counts.number, 4)
  673. })
  674. test('number event was dispatched correctly first time', () => {
  675. assert.strictEqual(log.args.number[0][0], 1)
  676. })
  677. test('number event was dispatched correctly second time', () => {
  678. assert.strictEqual(log.args.number[1][0], 2)
  679. })
  680. test('number event was dispatched correctly third time', () => {
  681. assert.strictEqual(log.args.number[2][0], 3)
  682. })
  683. test('number event was dispatched correctly fourth time', () => {
  684. assert.strictEqual(log.args.number[3][0], 4)
  685. })
  686. test('endObject event occurred three times', () => {
  687. assert.strictEqual(log.counts.endObject, 3)
  688. })
  689. test('end event occurred once', () => {
  690. assert.strictEqual(log.counts.end, 1)
  691. })
  692. test('error event did not occur', () => {
  693. assert.strictEqual(log.counts.error, 0)
  694. })
  695. })
  696. suite('promise:', () => {
  697. setup(done => {
  698. let resolve
  699. const emitter = eventify(new Promise(res => resolve = res), { poll: 4 })
  700. Object.keys(events).forEach(key => {
  701. emitter.on(events[key], spooks.fn({
  702. name: key,
  703. log: log
  704. }))
  705. })
  706. emitter.on(events.end, done)
  707. setTimeout(resolve.bind(null, 'foo'), 20)
  708. })
  709. test('string event occurred once', () => {
  710. assert.strictEqual(log.counts.string, 1)
  711. })
  712. test('string event was dispatched correctly', () => {
  713. assert.lengthOf(log.args.string[0], 1)
  714. assert.strictEqual(log.args.string[0][0], 'foo')
  715. })
  716. test('end event occurred once', () => {
  717. assert.strictEqual(log.counts.end, 1)
  718. })
  719. test('array event did not occur', () => {
  720. assert.strictEqual(log.counts.array, 0)
  721. })
  722. test('object event did not occur', () => {
  723. assert.strictEqual(log.counts.object, 0)
  724. })
  725. test('property event did not occur', () => {
  726. assert.strictEqual(log.counts.property, 0)
  727. })
  728. test('number event did not occur', () => {
  729. assert.strictEqual(log.counts.number, 0)
  730. })
  731. test('literal event did not occur', () => {
  732. assert.strictEqual(log.counts.literal, 0)
  733. })
  734. test('endArray event did not occur', () => {
  735. assert.strictEqual(log.counts.endArray, 0)
  736. })
  737. test('endObject event did not occur', () => {
  738. assert.strictEqual(log.counts.endObject, 0)
  739. })
  740. test('error event did not occur', () => {
  741. assert.strictEqual(log.counts.error, 0)
  742. })
  743. test('endPrefix event did not occur', () => {
  744. assert.strictEqual(log.counts.endPrefix, 0)
  745. })
  746. })
  747. suite('ignore promise:', () => {
  748. setup(done => {
  749. let resolve
  750. const emitter = eventify(new Promise(res => resolve = res), { poll: 4, promises: 'ignore' })
  751. Object.keys(events).forEach(key => {
  752. emitter.on(events[key], spooks.fn({
  753. name: key,
  754. log: log
  755. }))
  756. })
  757. emitter.on(events.end, done)
  758. setTimeout(resolve.bind(null, 'foo'), 20)
  759. })
  760. test('end event occurred once', () => {
  761. assert.strictEqual(log.counts.end, 1)
  762. })
  763. test('array event did not occur', () => {
  764. assert.strictEqual(log.counts.array, 0)
  765. })
  766. test('object event did not occur', () => {
  767. assert.strictEqual(log.counts.object, 0)
  768. })
  769. test('property event did not occur', () => {
  770. assert.strictEqual(log.counts.property, 0)
  771. })
  772. test('string event did not occur', () => {
  773. assert.strictEqual(log.counts.string, 0)
  774. })
  775. test('number event did not occur', () => {
  776. assert.strictEqual(log.counts.number, 0)
  777. })
  778. test('literal event did not occur', () => {
  779. assert.strictEqual(log.counts.literal, 0)
  780. })
  781. test('endArray event did not occur', () => {
  782. assert.strictEqual(log.counts.endArray, 0)
  783. })
  784. test('endObject event did not occur', () => {
  785. assert.strictEqual(log.counts.endObject, 0)
  786. })
  787. test('error event did not occur', () => {
  788. assert.strictEqual(log.counts.error, 0)
  789. })
  790. test('endPrefix event did not occur', () => {
  791. assert.strictEqual(log.counts.endPrefix, 0)
  792. })
  793. })
  794. suite('buffer:', () => {
  795. setup(done => {
  796. let resolve
  797. const emitter = eventify(new Buffer('foo bar baz qux'))
  798. Object.keys(events).forEach(key => {
  799. emitter.on(events[key], spooks.fn({
  800. name: key,
  801. log: log
  802. }))
  803. })
  804. emitter.on(events.end, done)
  805. })
  806. test('string event occurred once', () => {
  807. assert.strictEqual(log.counts.string, 1)
  808. })
  809. test('string event was dispatched correctly', () => {
  810. assert.lengthOf(log.args.string[0], 1)
  811. assert.strictEqual(log.args.string[0][0], 'foo bar baz qux')
  812. })
  813. test('end event occurred once', () => {
  814. assert.strictEqual(log.counts.end, 1)
  815. })
  816. test('array event did not occur', () => {
  817. assert.strictEqual(log.counts.array, 0)
  818. })
  819. test('object event did not occur', () => {
  820. assert.strictEqual(log.counts.object, 0)
  821. })
  822. test('property event did not occur', () => {
  823. assert.strictEqual(log.counts.property, 0)
  824. })
  825. test('number event did not occur', () => {
  826. assert.strictEqual(log.counts.number, 0)
  827. })
  828. test('literal event did not occur', () => {
  829. assert.strictEqual(log.counts.literal, 0)
  830. })
  831. test('endArray event did not occur', () => {
  832. assert.strictEqual(log.counts.endArray, 0)
  833. })
  834. test('endObject event did not occur', () => {
  835. assert.strictEqual(log.counts.endObject, 0)
  836. })
  837. test('error event did not occur', () => {
  838. assert.strictEqual(log.counts.error, 0)
  839. })
  840. test('endPrefix event did not occur', () => {
  841. assert.strictEqual(log.counts.endPrefix, 0)
  842. })
  843. })
  844. suite('ignore buffer:', () => {
  845. setup(done => {
  846. let resolve
  847. const emitter = eventify(new Buffer('foo bar baz qux'), { buffers: 'ignore' })
  848. Object.keys(events).forEach(key => {
  849. emitter.on(events[key], spooks.fn({
  850. name: key,
  851. log: log
  852. }))
  853. })
  854. emitter.on(events.end, done)
  855. })
  856. test('end event occurred once', () => {
  857. assert.strictEqual(log.counts.end, 1)
  858. })
  859. test('array event did not occur', () => {
  860. assert.strictEqual(log.counts.array, 0)
  861. })
  862. test('object event did not occur', () => {
  863. assert.strictEqual(log.counts.object, 0)
  864. })
  865. test('property event did not occur', () => {
  866. assert.strictEqual(log.counts.property, 0)
  867. })
  868. test('string event did not occur', () => {
  869. assert.strictEqual(log.counts.string, 0)
  870. })
  871. test('number event did not occur', () => {
  872. assert.strictEqual(log.counts.number, 0)
  873. })
  874. test('literal event did not occur', () => {
  875. assert.strictEqual(log.counts.literal, 0)
  876. })
  877. test('endArray event did not occur', () => {
  878. assert.strictEqual(log.counts.endArray, 0)
  879. })
  880. test('endObject event did not occur', () => {
  881. assert.strictEqual(log.counts.endObject, 0)
  882. })
  883. test('error event did not occur', () => {
  884. assert.strictEqual(log.counts.error, 0)
  885. })
  886. test('endPrefix event did not occur', () => {
  887. assert.strictEqual(log.counts.endPrefix, 0)
  888. })
  889. })
  890. suite('date:', () => {
  891. setup(done => {
  892. let resolve
  893. const emitter = eventify(new Date('1977-06-10T10:30:00.000Z'))
  894. Object.keys(events).forEach(key => {
  895. emitter.on(events[key], spooks.fn({
  896. name: key,
  897. log: log
  898. }))
  899. })
  900. emitter.on(events.end, done)
  901. })
  902. test('string event occurred once', () => {
  903. assert.strictEqual(log.counts.string, 1)
  904. })
  905. test('string event was dispatched correctly', () => {
  906. assert.lengthOf(log.args.string[0], 1)
  907. assert.strictEqual(log.args.string[0][0], '1977-06-10T10:30:00.000Z')
  908. })
  909. test('end event occurred once', () => {
  910. assert.strictEqual(log.counts.end, 1)
  911. })
  912. test('array event did not occur', () => {
  913. assert.strictEqual(log.counts.array, 0)
  914. })
  915. test('object event did not occur', () => {
  916. assert.strictEqual(log.counts.object, 0)
  917. })
  918. test('property event did not occur', () => {
  919. assert.strictEqual(log.counts.property, 0)
  920. })
  921. test('number event did not occur', () => {
  922. assert.strictEqual(log.counts.number, 0)
  923. })
  924. test('literal event did not occur', () => {
  925. assert.strictEqual(log.counts.literal, 0)
  926. })
  927. test('endArray event did not occur', () => {
  928. assert.strictEqual(log.counts.endArray, 0)
  929. })
  930. test('endObject event did not occur', () => {
  931. assert.strictEqual(log.counts.endObject, 0)
  932. })
  933. test('error event did not occur', () => {
  934. assert.strictEqual(log.counts.error, 0)
  935. })
  936. test('endPrefix event did not occur', () => {
  937. assert.strictEqual(log.counts.endPrefix, 0)
  938. })
  939. })
  940. suite('object with toJSON method:', () => {
  941. setup(done => {
  942. let resolve
  943. const emitter = eventify({ toJSON () { return 'foo' } })
  944. Object.keys(events).forEach(key => {
  945. emitter.on(events[key], spooks.fn({
  946. name: key,
  947. log: log
  948. }))
  949. })
  950. emitter.on(events.end, done)
  951. })
  952. test('string event occurred once', () => {
  953. assert.strictEqual(log.counts.string, 1)
  954. })
  955. test('string event was dispatched correctly', () => {
  956. assert.lengthOf(log.args.string[0], 1)
  957. assert.strictEqual(log.args.string[0][0], 'foo')
  958. })
  959. test('end event occurred once', () => {
  960. assert.strictEqual(log.counts.end, 1)
  961. })
  962. test('array event did not occur', () => {
  963. assert.strictEqual(log.counts.array, 0)
  964. })
  965. test('object event did not occur', () => {
  966. assert.strictEqual(log.counts.object, 0)
  967. })
  968. test('property event did not occur', () => {
  969. assert.strictEqual(log.counts.property, 0)
  970. })
  971. test('number event did not occur', () => {
  972. assert.strictEqual(log.counts.number, 0)
  973. })
  974. test('literal event did not occur', () => {
  975. assert.strictEqual(log.counts.literal, 0)
  976. })
  977. test('endArray event did not occur', () => {
  978. assert.strictEqual(log.counts.endArray, 0)
  979. })
  980. test('endObject event did not occur', () => {
  981. assert.strictEqual(log.counts.endObject, 0)
  982. })
  983. test('error event did not occur', () => {
  984. assert.strictEqual(log.counts.error, 0)
  985. })
  986. test('endPrefix event did not occur', () => {
  987. assert.strictEqual(log.counts.endPrefix, 0)
  988. })
  989. })
  990. suite('map:', () => {
  991. setup(done => {
  992. let resolve
  993. const emitter = eventify(new Map([['foo','bar'],['baz','qux']]))
  994. Object.keys(events).forEach(key => {
  995. emitter.on(events[key], spooks.fn({
  996. name: key,
  997. log: log
  998. }))
  999. })
  1000. emitter.on(events.end, done)
  1001. })
  1002. test('object event occurred once', () => {
  1003. assert.strictEqual(log.counts.object, 1)
  1004. })
  1005. test('property event occurred twice', () => {
  1006. assert.strictEqual(log.counts.property, 2)
  1007. })
  1008. test('property event was dispatched correctly first time', () => {
  1009. assert.strictEqual(log.args.property[0][0], 'foo')
  1010. })
  1011. test('property event was dispatched correctly second time', () => {
  1012. assert.strictEqual(log.args.property[1][0], 'baz')
  1013. })
  1014. test('string event occurred twice', () => {
  1015. assert.strictEqual(log.counts.string, 2)
  1016. })
  1017. test('string event was dispatched correctly first time', () => {
  1018. assert.strictEqual(log.args.string[0][0], 'bar')
  1019. })
  1020. test('string event was dispatched correctly second time', () => {
  1021. assert.strictEqual(log.args.string[1][0], 'qux')
  1022. })
  1023. test('endObject event occurred once', () => {
  1024. assert.strictEqual(log.counts.endObject, 1)
  1025. })
  1026. test('end event occurred once', () => {
  1027. assert.strictEqual(log.counts.end, 1)
  1028. })
  1029. test('array event did not occur', () => {
  1030. assert.strictEqual(log.counts.array, 0)
  1031. })
  1032. test('number event did not occur', () => {
  1033. assert.strictEqual(log.counts.number, 0)
  1034. })
  1035. test('literal event did not occur', () => {
  1036. assert.strictEqual(log.counts.literal, 0)
  1037. })
  1038. test('endArray event did not occur', () => {
  1039. assert.strictEqual(log.counts.endArray, 0)
  1040. })
  1041. test('error event did not occur', () => {
  1042. assert.strictEqual(log.counts.error, 0)
  1043. })
  1044. test('endPrefix event did not occur', () => {
  1045. assert.strictEqual(log.counts.endPrefix, 0)
  1046. })
  1047. })
  1048. suite('ignore map:', () => {
  1049. setup(done => {
  1050. let resolve
  1051. const emitter = eventify(new Map([['foo','bar'],['baz','qux']]), { maps: 'ignore' })
  1052. Object.keys(events).forEach(key => {
  1053. emitter.on(events[key], spooks.fn({
  1054. name: key,
  1055. log: log
  1056. }))
  1057. })
  1058. emitter.on(events.end, done)
  1059. })
  1060. test('end event occurred once', () => {
  1061. assert.strictEqual(log.counts.end, 1)
  1062. })
  1063. test('array event did not occur', () => {
  1064. assert.strictEqual(log.counts.array, 0)
  1065. })
  1066. test('object event did not occur', () => {
  1067. assert.strictEqual(log.counts.object, 0)
  1068. })
  1069. test('property event did not occur', () => {
  1070. assert.strictEqual(log.counts.property, 0)
  1071. })
  1072. test('string event did not occur', () => {
  1073. assert.strictEqual(log.counts.string, 0)
  1074. })
  1075. test('number event did not occur', () => {
  1076. assert.strictEqual(log.counts.number, 0)
  1077. })
  1078. test('literal event did not occur', () => {
  1079. assert.strictEqual(log.counts.literal, 0)
  1080. })
  1081. test('endArray event did not occur', () => {
  1082. assert.strictEqual(log.counts.endArray, 0)
  1083. })
  1084. test('endObject event did not occur', () => {
  1085. assert.strictEqual(log.counts.endObject, 0)
  1086. })
  1087. test('error event did not occur', () => {
  1088. assert.strictEqual(log.counts.error, 0)
  1089. })
  1090. test('endPrefix event did not occur', () => {
  1091. assert.strictEqual(log.counts.endPrefix, 0)
  1092. })
  1093. })
  1094. suite('set:', () => {
  1095. setup(done => {
  1096. let resolve
  1097. const emitter = eventify(new Set(['foo','bar']))
  1098. Object.keys(events).forEach(key => {
  1099. emitter.on(events[key], spooks.fn({
  1100. name: key,
  1101. log: log
  1102. }))
  1103. })
  1104. emitter.on(events.end, done)
  1105. })
  1106. test('array event occurred once', () => {
  1107. assert.strictEqual(log.counts.array, 1)
  1108. })
  1109. test('string event occurred twice', () => {
  1110. assert.strictEqual(log.counts.string, 2)
  1111. })
  1112. test('string event was dispatched correctly first time', () => {
  1113. assert.strictEqual(log.args.string[0][0], 'foo')
  1114. })
  1115. test('string event was dispatched correctly second time', () => {
  1116. assert.strictEqual(log.args.string[1][0], 'bar')
  1117. })
  1118. test('endArray event occurred once', () => {
  1119. assert.strictEqual(log.counts.endArray, 1)
  1120. })
  1121. test('end event occurred once', () => {
  1122. assert.strictEqual(log.counts.end, 1)
  1123. })
  1124. test('object event did not occur', () => {
  1125. assert.strictEqual(log.counts.object, 0)
  1126. })
  1127. test('property event did not occur', () => {
  1128. assert.strictEqual(log.counts.number, 0)
  1129. })
  1130. test('number event did not occur', () => {
  1131. assert.strictEqual(log.counts.number, 0)
  1132. })
  1133. test('literal event did not occur', () => {
  1134. assert.strictEqual(log.counts.literal, 0)
  1135. })
  1136. test('endObject event did not occur', () => {
  1137. assert.strictEqual(log.counts.endObject, 0)
  1138. })
  1139. test('error event did not occur', () => {
  1140. assert.strictEqual(log.counts.error, 0)
  1141. })
  1142. test('endPrefix event did not occur', () => {
  1143. assert.strictEqual(log.counts.endPrefix, 0)
  1144. })
  1145. })
  1146. suite('ignore set:', () => {
  1147. setup(done => {
  1148. let resolve
  1149. const emitter = eventify(new Set(['foo','bar']), { iterables: 'ignore' })
  1150. Object.keys(events).forEach(key => {
  1151. emitter.on(events[key], spooks.fn({
  1152. name: key,
  1153. log: log
  1154. }))
  1155. })
  1156. emitter.on(events.end, done)
  1157. })
  1158. test('end event occurred once', () => {
  1159. assert.strictEqual(log.counts.end, 1)
  1160. })
  1161. test('array event did not occur', () => {
  1162. assert.strictEqual(log.counts.array, 0)
  1163. })
  1164. test('object event did not occur', () => {
  1165. assert.strictEqual(log.counts.object, 0)
  1166. })
  1167. test('property event did not occur', () => {
  1168. assert.strictEqual(log.counts.property, 0)
  1169. })
  1170. test('string event did not occur', () => {
  1171. assert.strictEqual(log.counts.string, 0)
  1172. })
  1173. test('number event did not occur', () => {
  1174. assert.strictEqual(log.counts.number, 0)
  1175. })
  1176. test('literal event did not occur', () => {
  1177. assert.strictEqual(log.counts.literal, 0)
  1178. })
  1179. test('endArray event did not occur', () => {
  1180. assert.strictEqual(log.counts.endArray, 0)
  1181. })
  1182. test('endObject event did not occur', () => {
  1183. assert.strictEqual(log.counts.endObject, 0)
  1184. })
  1185. test('error event did not occur', () => {
  1186. assert.strictEqual(log.counts.error, 0)
  1187. })
  1188. test('endPrefix event did not occur', () => {
  1189. assert.strictEqual(log.counts.endPrefix, 0)
  1190. })
  1191. })
  1192. suite('promise resolved to a map:', () => {
  1193. setup(done => {
  1194. let resolve
  1195. const emitter = eventify(new Promise(res => resolve = res), { poll: 4 })
  1196. Object.keys(events).forEach(key => {
  1197. emitter.on(events[key], spooks.fn({
  1198. name: key,
  1199. log: log
  1200. }))
  1201. })
  1202. emitter.on(events.end, done)
  1203. setImmediate(resolve.bind(null, new Map([['foo','bar'],['baz','qux']])))
  1204. })
  1205. test('object event occurred once', () => {
  1206. assert.strictEqual(log.counts.object, 1)
  1207. })
  1208. test('property event occurred twice', () => {
  1209. assert.strictEqual(log.counts.property, 2)
  1210. })
  1211. test('property event was dispatched correctly first time', () => {
  1212. assert.strictEqual(log.args.property[0][0], 'foo')
  1213. })
  1214. test('property event was dispatched correctly second time', () => {
  1215. assert.strictEqual(log.args.property[1][0], 'baz')
  1216. })
  1217. test('string event occurred twice', () => {
  1218. assert.strictEqual(log.counts.string, 2)
  1219. })
  1220. test('string event was dispatched correctly first time', () => {
  1221. assert.strictEqual(log.args.string[0][0], 'bar')
  1222. })
  1223. test('string event was dispatched correctly second time', () => {
  1224. assert.strictEqual(log.args.string[1][0], 'qux')
  1225. })
  1226. test('endObject event occurred once', () => {
  1227. assert.strictEqual(log.counts.endObject, 1)
  1228. })
  1229. test('end event occurred once', () => {
  1230. assert.strictEqual(log.counts.end, 1)
  1231. })
  1232. test('array event did not occur', () => {
  1233. assert.strictEqual(log.counts.array, 0)
  1234. })
  1235. test('number event did not occur', () => {
  1236. assert.strictEqual(log.counts.number, 0)
  1237. })
  1238. test('literal event did not occur', () => {
  1239. assert.strictEqual(log.counts.literal, 0)
  1240. })
  1241. test('endArray event did not occur', () => {
  1242. assert.strictEqual(log.counts.endArray, 0)
  1243. })
  1244. test('error event did not occur', () => {
  1245. assert.strictEqual(log.counts.error, 0)
  1246. })
  1247. test('endPrefix event did not occur', () => {
  1248. assert.strictEqual(log.counts.endPrefix, 0)
  1249. })
  1250. })
  1251. suite('array circular reference:', () => {
  1252. setup(done => {
  1253. const array = [ 'foo' ]
  1254. array[1] = array
  1255. const emitter = eventify(array)
  1256. Object.keys(events).forEach(key => {
  1257. emitter.on(events[key], spooks.fn({
  1258. name: key,
  1259. log: log
  1260. }))
  1261. })
  1262. emitter.on(events.end, done)
  1263. })
  1264. test('array event occurred twice', () => {
  1265. assert.strictEqual(log.counts.array, 2)
  1266. })
  1267. test('string event occurred once', () => {
  1268. assert.strictEqual(log.counts.string, 1)
  1269. })
  1270. test('string event was dispatched correctly', () => {
  1271. assert.strictEqual(log.args.string[0][0], 'foo')
  1272. })
  1273. test('endArray event occurred twice', () => {
  1274. assert.strictEqual(log.counts.endArray, 2)
  1275. })
  1276. test('end event occurred once', () => {
  1277. assert.strictEqual(log.counts.end, 1)
  1278. })
  1279. test('error event occurred once', () => {
  1280. assert.strictEqual(log.counts.error, 1)
  1281. })
  1282. test('error event was dispatched correctly', () => {
  1283. assert.lengthOf(log.args.error[0], 1)
  1284. assert.instanceOf(log.args.error[0][0], Error)
  1285. assert.strictEqual(log.args.error[0][0].message, 'Circular reference.')
  1286. })
  1287. })
  1288. suite('object circular reference:', () => {
  1289. setup(done => {
  1290. const object = { foo: 'bar' }
  1291. object.self = object
  1292. const emitter = eventify(object)
  1293. Object.keys(events).forEach(key => {
  1294. emitter.on(events[key], spooks.fn({
  1295. name: key,
  1296. log: log
  1297. }))
  1298. })
  1299. emitter.on(events.end, done)
  1300. })
  1301. test('object event occurred twice', () => {
  1302. assert.strictEqual(log.counts.object, 2)
  1303. })
  1304. test('property event occurred twice', () => {
  1305. assert.strictEqual(log.counts.property, 2)
  1306. })
  1307. test('property event was dispatched correctly first time', () => {
  1308. assert.strictEqual(log.args.property[0][0], 'foo')
  1309. })
  1310. test('property event was dispatched correctly second time', () => {
  1311. assert.strictEqual(log.args.property[1][0], 'self')
  1312. })
  1313. test('endObject event occurred twice', () => {
  1314. assert.strictEqual(log.counts.endObject, 2)
  1315. })
  1316. test('end event occurred once', () => {
  1317. assert.strictEqual(log.counts.end, 1)
  1318. })
  1319. test('error event occurred once', () => {
  1320. assert.strictEqual(log.counts.error, 1)
  1321. })
  1322. test('error event was dispatched correctly', () => {
  1323. assert.strictEqual(log.args.error[0][0].message, 'Circular reference.')
  1324. })
  1325. })
  1326. suite('array circular reference with ignore set:', () => {
  1327. setup(done => {
  1328. const array = [ 'foo' ]
  1329. array[1] = array
  1330. const emitter = eventify(array, { circular: 'ignore' })
  1331. Object.keys(events).forEach(key => {
  1332. emitter.on(events[key], spooks.fn({
  1333. name: key,
  1334. log: log
  1335. }))
  1336. })
  1337. emitter.on(events.end, done)
  1338. })
  1339. test('array event occurred twice', () => {
  1340. assert.strictEqual(log.counts.array, 2)
  1341. })
  1342. test('string event occurred once', () => {
  1343. assert.strictEqual(log.counts.string, 1)
  1344. })
  1345. test('endArray event occurred twice', () => {
  1346. assert.strictEqual(log.counts.endArray, 2)
  1347. })
  1348. test('end event occurred once', () => {
  1349. assert.strictEqual(log.counts.end, 1)
  1350. })
  1351. test('error event did not occur', () => {
  1352. assert.strictEqual(log.counts.error, 0)
  1353. })
  1354. })
  1355. suite('object circular reference with ignore set:', () => {
  1356. setup(done => {
  1357. const object = { foo: 'bar' }
  1358. object.self = object
  1359. const emitter = eventify(object, { circular: 'ignore' })
  1360. Object.keys(events).forEach(key => {
  1361. emitter.on(events[key], spooks.fn({
  1362. name: key,
  1363. log: log
  1364. }))
  1365. })
  1366. emitter.on(events.end, done)
  1367. })
  1368. test('object event occurred twice', () => {
  1369. assert.strictEqual(log.counts.object, 2)
  1370. })
  1371. test('property event occurred twice', () => {
  1372. assert.strictEqual(log.counts.property, 2)
  1373. })
  1374. test('endObject event occurred twice', () => {
  1375. assert.strictEqual(log.counts.endObject, 2)
  1376. })
  1377. test('end event occurred once', () => {
  1378. assert.strictEqual(log.counts.end, 1)
  1379. })
  1380. test('error event did not occur', () => {
  1381. assert.strictEqual(log.counts.error, 0)
  1382. })
  1383. })
  1384. suite('parallel array reference:', () => {
  1385. setup(done => {
  1386. const array = [ 'foo' ]
  1387. const emitter = eventify([ array, array ])
  1388. Object.keys(events).forEach(key => {
  1389. emitter.on(events[key], spooks.fn({
  1390. name: key,
  1391. log: log
  1392. }))
  1393. })
  1394. emitter.on(events.end, done)
  1395. })
  1396. test('array event occurred three times', () => {
  1397. assert.strictEqual(log.counts.array, 3)
  1398. })
  1399. test('string event occurred twice', () => {
  1400. assert.strictEqual(log.counts.string, 2)
  1401. })
  1402. test('endArray event occurred three times', () => {
  1403. assert.strictEqual(log.counts.endArray, 3)
  1404. })
  1405. test('end event occurred once', () => {
  1406. assert.strictEqual(log.counts.end, 1)
  1407. })
  1408. test('error event did not occur', () => {
  1409. assert.strictEqual(log.counts.error, 0)
  1410. })
  1411. })
  1412. suite('parallel object reference:', () => {
  1413. setup(done => {
  1414. const object = { foo: 'bar' }
  1415. const emitter = eventify({ baz: object, qux: object })
  1416. Object.keys(events).forEach(key => {
  1417. emitter.on(events[key], spooks.fn({
  1418. name: key,
  1419. log: log
  1420. }))
  1421. })
  1422. emitter.on(events.end, done)
  1423. })
  1424. test('object event occurred three times', () => {
  1425. assert.strictEqual(log.counts.object, 3)
  1426. })
  1427. test('property event occurred four times', () => {
  1428. assert.strictEqual(log.counts.property, 4)
  1429. })
  1430. test('endObject event occurred three times', () => {
  1431. assert.strictEqual(log.counts.endObject, 3)
  1432. })
  1433. test('end event occurred once', () => {
  1434. assert.strictEqual(log.counts.end, 1)
  1435. })
  1436. test('error event did not occur', () => {
  1437. assert.strictEqual(log.counts.error, 0)
  1438. })
  1439. })
  1440. suite('throw errors from event handlers:', () => {
  1441. setup(done => {
  1442. const emitter = eventify([null,false,true,0,"",{"foo":"bar"}])
  1443. Object.keys(events).forEach(key => {
  1444. const event = events[key]
  1445. emitter.on(event, spooks.fn({
  1446. name: key,
  1447. log: log
  1448. }))
  1449. if (event !== events.end) {
  1450. emitter.on(event, () => { throw 0 })
  1451. }
  1452. })
  1453. emitter.on(events.end, done)
  1454. })
  1455. test('end event occurred once', () => {
  1456. assert.strictEqual(log.counts.end, 1)
  1457. })
  1458. test('array event occured once', () => {
  1459. assert.strictEqual(log.counts.array, 1)
  1460. })
  1461. test('literal event occured three times', () => {
  1462. assert.strictEqual(log.counts.literal, 3)
  1463. })
  1464. test('number event occured once', () => {
  1465. assert.strictEqual(log.counts.number, 1)
  1466. })
  1467. test('string event occured twice', () => {
  1468. assert.strictEqual(log.counts.string, 2)
  1469. })
  1470. test('object event occured once', () => {
  1471. assert.strictEqual(log.counts.object, 1)
  1472. })
  1473. test('property event occured once', () => {
  1474. assert.strictEqual(log.counts.property, 1)
  1475. })
  1476. test('endObject event occured once', () => {
  1477. assert.strictEqual(log.counts.endObject, 1)
  1478. })
  1479. test('endArray event occured once', () => {
  1480. assert.strictEqual(log.counts.endArray, 1)
  1481. })
  1482. test('error event occured eleven times', () => {
  1483. assert.strictEqual(log.counts.error, 11)
  1484. })
  1485. })
  1486. })
  1487. })