This repository has been archived by the owner on May 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathindex.test.js
91 lines (75 loc) · 3.1 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import _ from 'lodash';
import angular from 'angular';
import showAngularStats from './index';
describe(`ng-stats`, () => {
describe(`function`, () => {
// TODO...
it(`should expose give me a function I can call`, () => {
expect(showAngularStats).to.be.a('function');
});
});
describe('angular-stats', () => {
let $injector;
let $compile, scope, el, node, appNode;
let basicTemplate = `
<div angular-stats watch-count=".watch-count" digest-length=".digest-length"
on-watch-count-update="onWatchCountUpdate(watchCount)"
on-digest-length-update="onDigestLengthUpdate(digestLength)">
Watch Count: <span class="watch-count"></span><br />
Digest Cycle Length: <span class="digest-length"></span>
</div>
`;
beforeEach(() => {
appNode = document.createElement('div');
document.body.appendChild(appNode);
angular.module('app', ['angularStats']);
$injector = angular.bootstrap(appNode, ['app']);
$compile = $injector.get('$compile');
scope = $injector.get('$rootScope').$new();
scope.onWatchCountUpdate = function(count) {
scope.onWatchCountUpdate.invokes.push(arguments);
};
scope.onWatchCountUpdate.invokes = [];
scope.onDigestLengthUpdate = function(digestLength) {
scope.onDigestLengthUpdate.invokes.push(arguments);
};
scope.onDigestLengthUpdate.invokes = [];
});
it(`invoke on-digest-length-update and update the node`, () => {
compileAndDigest();
const digestLengthNode = node.querySelector('.digest-length');
expect(scope.onDigestLengthUpdate.invokes).to.have.length(1);
nodeContentSameAsLatestValue(digestLengthNode, scope.onDigestLengthUpdate);
scope.$digest();
expect(scope.onDigestLengthUpdate.invokes).to.have.length(2);
nodeContentSameAsLatestValue(digestLengthNode, scope.onDigestLengthUpdate);
});
// not sure what's up with this. I think it has to do with the 350ms timeout on updating the watch count...
it.skip(`should invoke on-watch-count-update`, () => {
compileAndDigest();
const watchCountNode = node.querySelector('.watch-count');
expect(scope.onWatchCountUpdate.invokes).to.have.length(1);
nodeContentSameAsLatestValue(watchCountNode, scope.onWatchCountUpdate);
scope.$digest();
expect(scope.onWatchCountUpdate.invokes).to.have.length(2);
nodeContentSameAsLatestValue(watchCountNode, scope.onWatchCountUpdate);
});
afterEach(() => {
appNode.remove();
});
function compileAndDigest(template, extraProps = {}) {
_.assign(scope, extraProps);
el = $compile(template || basicTemplate)(scope);
node = el[0];
scope.$digest();
}
function nodeContentSameAsLatestValue(theNode, tracker) {
const nodeNumber = parseFloat(theNode.textContent);
const latestInvoke = parseFloat(getLatestFirstArg(tracker).toFixed(2));
expect(nodeNumber).to.equal(latestInvoke);
}
function getLatestFirstArg(invokeable) {
return invokeable.invokes[invokeable.invokes.length - 1][0];
}
});
});