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