Skip to content

Commit 8e171d5

Browse files
committed
Add animation and methods tests
1 parent 199c3d9 commit 8e171d5

File tree

6 files changed

+213
-53
lines changed

6 files changed

+213
-53
lines changed

karma.conf.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ module.exports = function(config) {
1515

1616
// list of files / patterns to load in the browser
1717
files: [
18-
'dist/**/granim.js',
18+
'dist/granim.js',
19+
'test/testUtils.js',
1920
'test/**/*Spec.js'
2021
],
2122

@@ -64,7 +65,7 @@ module.exports = function(config) {
6465

6566
// start these browsers
6667
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
67-
browsers: ['PhantomJS'],
68+
browsers: ['phantomJS'],
6869

6970

7071
// Continuous Integration mode

test/GranimSpec.js

-51
This file was deleted.

test/animationSpec.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
describe('Animation: ', function() {
2+
var value, granimInstance, gradientColor, canvas;
3+
4+
beforeEach(function(done) {
5+
setTimeout(function() {
6+
value = 0;
7+
done();
8+
}, 1);
9+
});
10+
11+
it("should support async execution of test preparation and expectations", function(done) {
12+
setCanvas();
13+
granimInstance = new Granim({
14+
element: '#granim-canvas',
15+
name: 'granim',
16+
direction: 'left-right',
17+
//isPausedWhenNotInView: true,
18+
opacity: [1, 1],
19+
states : {
20+
"default-state": {
21+
gradients: [
22+
['#BA8B02', '#181818'],
23+
['#7b4397', '#dc2430']
24+
],
25+
transitionSpeed: 100,
26+
loop: true
27+
}
28+
}
29+
});
30+
canvas = document.querySelector(granimInstance.element);
31+
gradientColor = granimInstance.context.getImageData(150, 75, 5, 5).data;
32+
33+
expect(granimInstance).toBeDefined();
34+
done();
35+
});
36+
37+
describe('Asynchronous specs:', function() {
38+
var originalTimeout;
39+
beforeEach(function() {
40+
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
41+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
42+
});
43+
44+
it('Gradient animation is working', function(done) {
45+
setTimeout(function() {
46+
expect(gradientColor).not.toEqual(granimInstance.context.getImageData(150, 75, 5, 5).data);
47+
done();
48+
}, 205);
49+
});
50+
51+
afterEach(function() {
52+
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
53+
});
54+
});
55+
});

test/coreSpec.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
describe('Prelim Checks: ', function(){
2+
it('Granim is defined', function(){
3+
expect(Granim).toBeDefined();
4+
});
5+
it('Granim is a function', function(){
6+
expect(Granim).toEqual(jasmine.any(Function));
7+
});
8+
});
9+
10+
var validOptions = {
11+
element: document.createElement('canvas'),
12+
name: 'granim',
13+
opacity: [1, 1],
14+
states : {
15+
"default-state": {
16+
gradients: [
17+
['#834D9B', '#D04ED6'],
18+
['#1CD8D2', '#93EDC7']
19+
]
20+
}
21+
}
22+
};
23+
24+
describe('Granim Core: ', function(){
25+
beforeEach(function(){
26+
unsetCanvas();
27+
spyOn(console, 'error');
28+
});
29+
30+
describe('Invalid options: ', function(){
31+
var func = function(){ return new Granim({element: '#granim-canvas'})};
32+
33+
it('throws error on invalid element id ', function(){
34+
expect(func).toThrowError("`#granim-canvas` could not be found in the DOM");
35+
});
36+
37+
it('throws an error when passed in anything other than a HTMLCanvasElement as an element', function(){
38+
var invalidOptions = {
39+
element: document.createElement('div'),
40+
name: validOptions.name,
41+
opactiy: validOptions.opactiy,
42+
states: validOptions.states
43+
};
44+
var func = function(){ return new Granim(invalidOptions)};
45+
expect(func).toThrowError("The element you used is neither a String, nor a HTMLCanvasElement");
46+
});
47+
});
48+
49+
it('Can use a HTMLCanvasElement as an element', function(){
50+
new Granim(validOptions);
51+
});
52+
});

test/methodsSpec.js

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
describe('Methods: ', function() {
2+
var value, granimInstance;
3+
4+
beforeEach(function(done) {
5+
setTimeout(function() {
6+
value = 0;
7+
done();
8+
}, 1);
9+
});
10+
11+
it("should support async execution of test preparation and expectations", function(done) {
12+
setCanvas();
13+
granimInstance = new Granim({
14+
element: '#granim-canvas',
15+
direction: 'left-right',
16+
//isPausedWhenNotInView: true,
17+
opacity: [1, 1],
18+
states : {
19+
"default-state": {
20+
gradients: [
21+
['#BA8B02', '#181818'],
22+
['#111', '#252525'],
23+
['#7b4397', '#dc2430']
24+
],
25+
transitionSpeed: 100,
26+
loop: false
27+
},
28+
"second-state": {
29+
gradients: [['#00bf8f', '#001510']],
30+
transitionSpeed: 100,
31+
loop: false
32+
}
33+
}
34+
});
35+
36+
expect(granimInstance).toBeDefined();
37+
done();
38+
});
39+
40+
describe("Asynchronous specs:", function() {
41+
var originalTimeout;
42+
beforeEach(function() {
43+
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
44+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
45+
});
46+
47+
it("Pause method is working", function(done) {
48+
setTimeout(function() {
49+
granimInstance.pause();
50+
expect(granimInstance.isPaused).toBe(true);
51+
done();
52+
}, 105);
53+
});
54+
55+
it("Play method is working", function(done) {
56+
setTimeout(function() {
57+
granimInstance.play();
58+
expect(granimInstance.isPaused).toBe(false);
59+
done();
60+
}, 10);
61+
});
62+
63+
it("ChangeState method is working", function(done) {
64+
setTimeout(function() {
65+
granimInstance.changeState('default-state');
66+
granimInstance.changeState('second-state');
67+
expect(granimInstance.activeState).toEqual('second-state');
68+
done();
69+
}, 200);
70+
});
71+
72+
it("Clear method is working", function(done) {
73+
setTimeout(function() {
74+
granimInstance.clear();
75+
expect(granimInstance.context.getImageData(1, 1, 1, 1).data[0]).toEqual(0);
76+
done();
77+
}, 10);
78+
});
79+
80+
afterEach(function() {
81+
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
82+
});
83+
});
84+
});

test/testUtils.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function setCanvas(canvasId) {
2+
var canvas;
3+
if (canvasId && document.querySelector(canvasId)) {
4+
return;
5+
6+
} else if (document.querySelector('#granim-canvas')) {
7+
return;
8+
}
9+
10+
canvas = document.createElement('canvas');
11+
canvas.setAttribute('id', canvasId || 'granim-canvas');
12+
canvas.setAttribute('style', 'position:absolute; width: 100%; height: 150px; left: 0; right: 0;');
13+
document.body.appendChild(canvas);
14+
}
15+
16+
function unsetCanvas(canvasId) {
17+
var canvas = document.querySelector('#' + (canvasId || 'granim-canvas'));
18+
if (canvas) canvas.remove();
19+
}

0 commit comments

Comments
 (0)