1 | "use strict"
|
2 | var test = require("tap").test
|
3 | var Tracker = require("../index.js").Tracker
|
4 | var TrackerGroup = require("../index.js").TrackerGroup
|
5 |
|
6 | var timeoutError = new Error("timeout")
|
7 | var testEvent = function (obj,event,next) {
|
8 | var timeout = setTimeout(function(){
|
9 | obj.removeListener(event, eventHandler)
|
10 | next(timeoutError)
|
11 | }, 10)
|
12 | var eventHandler = function () {
|
13 | var args = Array.prototype.slice.call(arguments)
|
14 | args.unshift(null)
|
15 | clearTimeout(timeout)
|
16 | next.apply(null, args)
|
17 | }
|
18 | obj.once(event, eventHandler)
|
19 | }
|
20 |
|
21 | test("TrackerGroup", function (t) {
|
22 | var name = "test"
|
23 |
|
24 | var track = new TrackerGroup(name)
|
25 | t.is(track.completed(), 0, "Nothing todo is 0 completion")
|
26 | testEvent(track, "change", afterFinishEmpty)
|
27 | track.finish()
|
28 | var a, b
|
29 | function afterFinishEmpty(er, onChangeName) {
|
30 | t.is(er, null, "finishEmpty: on change event fired")
|
31 | t.is(onChangeName, name, "finishEmpty: on change emits the correct name")
|
32 | t.is(track.completed(), 1, "finishEmpty: Finishing an empty group actually finishes it")
|
33 |
|
34 | track = new TrackerGroup(name)
|
35 | a = track.newItem("a", 10, 1)
|
36 | b = track.newItem("b", 10, 1)
|
37 | t.is(track.completed(), 0, "Initially empty")
|
38 | testEvent(track, "change", afterCompleteWork)
|
39 | a.completeWork(5)
|
40 | }
|
41 | function afterCompleteWork(er, onChangeName) {
|
42 | t.is(er, null, "on change event fired")
|
43 | t.is(onChangeName, "a", "on change emits the correct name")
|
44 | t.is(track.completed(), 0.25, "Complete half of one is a quarter overall")
|
45 | testEvent(track, "change", afterFinishAll)
|
46 | track.finish()
|
47 | }
|
48 | function afterFinishAll(er, onChangeName) {
|
49 | t.is(er, null, "finishAll: on change event fired")
|
50 | t.is(onChangeName, name, "finishAll: on change emits the correct name")
|
51 | t.is(track.completed(), 1, "Finishing everything ")
|
52 |
|
53 | track = new TrackerGroup(name)
|
54 | a = track.newItem("a", 10, 2)
|
55 | b = track.newItem("b", 10, 1)
|
56 | t.is(track.completed(), 0, "weighted: Initially empty")
|
57 | testEvent(track, "change", afterWeightedCompleteWork)
|
58 | a.completeWork(5)
|
59 | }
|
60 | function afterWeightedCompleteWork(er, onChangeName) {
|
61 | t.is(er, null, "weighted: on change event fired")
|
62 | t.is(onChangeName, "a", "weighted: on change emits the correct name")
|
63 | t.is(Math.round(track.completed()*100), 33, "weighted: Complete half of double weighted")
|
64 | testEvent(track, "change", afterWeightedFinishAll)
|
65 | track.finish()
|
66 | }
|
67 | function afterWeightedFinishAll(er, onChangeName) {
|
68 | t.is(er, null, "weightedFinishAll: on change event fired")
|
69 | t.is(onChangeName, name, "weightedFinishAll: on change emits the correct name")
|
70 | t.is(track.completed(), 1, "weightedFinishaAll: Finishing everything ")
|
71 |
|
72 | track = new TrackerGroup(name)
|
73 | a = track.newGroup("a", 10)
|
74 | b = track.newGroup("b", 10)
|
75 | var a1 = a.newItem("a.1",10)
|
76 | a1.completeWork(5)
|
77 | t.is(track.completed(), 0.25, "nested: Initially quarter done")
|
78 | testEvent(track, "change", afterNestedComplete)
|
79 | b.finish()
|
80 | }
|
81 | function afterNestedComplete(er, onChangeName) {
|
82 | t.is(er, null, "nestedComplete: on change event fired")
|
83 | t.is(onChangeName, "b", "nestedComplete: on change emits the correct name")
|
84 | t.is(track.completed(), 0.75, "nestedComplete: Finishing everything ")
|
85 | t.end()
|
86 | }
|
87 | })
|