trackergroup.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 'use strict'
  2. var test = require('tap').test
  3. var TrackerGroup = require('../index.js').TrackerGroup
  4. var testEvent = require('./lib/test-event.js')
  5. test('TrackerGroup', function (t) {
  6. var name = 'test'
  7. var track = new TrackerGroup(name)
  8. t.is(track.completed(), 0, 'Nothing todo is 0 completion')
  9. testEvent(track, 'change', afterFinishEmpty)
  10. track.finish()
  11. var a, b
  12. function afterFinishEmpty (er, onChangeName, completion) {
  13. t.is(er, null, 'finishEmpty: on change event fired')
  14. t.is(onChangeName, name, 'finishEmpty: on change emits the correct name')
  15. t.is(completion, 1, 'finishEmpty: passed through completion was correct')
  16. t.is(track.completed(), 1, 'finishEmpty: Finishing an empty group actually finishes it')
  17. track = new TrackerGroup(name)
  18. a = track.newItem('a', 10, 1)
  19. b = track.newItem('b', 10, 1)
  20. t.is(track.completed(), 0, 'Initially empty')
  21. testEvent(track, 'change', afterCompleteWork)
  22. a.completeWork(5)
  23. }
  24. function afterCompleteWork (er, onChangeName, completion) {
  25. t.is(er, null, 'on change event fired')
  26. t.is(onChangeName, 'a', 'on change emits the correct name')
  27. t.is(completion, 0.25, 'Complete half of one is a quarter overall')
  28. t.is(track.completed(), 0.25, 'Complete half of one is a quarter overall')
  29. testEvent(track, 'change', afterFinishAll)
  30. track.finish()
  31. }
  32. function afterFinishAll (er, onChangeName, completion) {
  33. t.is(er, null, 'finishAll: on change event fired')
  34. t.is(onChangeName, name, 'finishAll: on change emits the correct name')
  35. t.is(completion, 1, 'Finishing everything ')
  36. t.is(track.completed(), 1, 'Finishing everything ')
  37. track = new TrackerGroup(name)
  38. a = track.newItem('a', 10, 2)
  39. b = track.newItem('b', 10, 1)
  40. t.is(track.completed(), 0, 'weighted: Initially empty')
  41. testEvent(track, 'change', afterWeightedCompleteWork)
  42. a.completeWork(5)
  43. }
  44. function afterWeightedCompleteWork (er, onChangeName, completion) {
  45. t.is(er, null, 'weighted: on change event fired')
  46. t.is(onChangeName, 'a', 'weighted: on change emits the correct name')
  47. t.is(Math.floor(completion * 100), 33, 'weighted: Complete half of double weighted')
  48. t.is(Math.floor(track.completed() * 100), 33, 'weighted: Complete half of double weighted')
  49. testEvent(track, 'change', afterWeightedFinishAll)
  50. track.finish()
  51. }
  52. function afterWeightedFinishAll (er, onChangeName, completion) {
  53. t.is(er, null, 'weightedFinishAll: on change event fired')
  54. t.is(onChangeName, name, 'weightedFinishAll: on change emits the correct name')
  55. t.is(completion, 1, 'weightedFinishaAll: Finishing everything ')
  56. t.is(track.completed(), 1, 'weightedFinishaAll: Finishing everything ')
  57. track = new TrackerGroup(name)
  58. a = track.newGroup('a', 10)
  59. b = track.newGroup('b', 10)
  60. var a1 = a.newItem('a.1', 10)
  61. a1.completeWork(5)
  62. t.is(track.completed(), 0.25, 'nested: Initially quarter done')
  63. testEvent(track, 'change', afterNestedComplete)
  64. b.finish()
  65. }
  66. function afterNestedComplete (er, onChangeName, completion) {
  67. t.is(er, null, 'nestedComplete: on change event fired')
  68. t.is(onChangeName, 'b', 'nestedComplete: on change emits the correct name')
  69. t.is(completion, 0.75, 'nestedComplete: Finishing everything ')
  70. t.is(track.completed(), 0.75, 'nestedComplete: Finishing everything ')
  71. t.end()
  72. }
  73. })
  74. test('cycles', function (t) {
  75. var track = new TrackerGroup('top')
  76. testCycle(track, track)
  77. var layer1 = track.newGroup('layer1')
  78. testCycle(layer1, track)
  79. t.end()
  80. function testCycle (addTo, toAdd) {
  81. try {
  82. addTo.addUnit(toAdd)
  83. t.fail(toAdd.name)
  84. } catch (ex) {
  85. console.log(ex)
  86. t.pass(toAdd.name)
  87. }
  88. }
  89. })