Skip to content

Commit e68bc39

Browse files
committed
Modify setDirection.js and add tests
1 parent 675f7f8 commit e68bc39

File tree

7 files changed

+49
-41
lines changed

7 files changed

+49
-41
lines changed

dist/granim.js

-10
Original file line numberDiff line numberDiff line change
@@ -942,34 +942,24 @@ module.exports = function() {
942942
var ctx = this.context;
943943

944944
switch(this.direction) {
945-
default:
946-
this.triggerError('direction');
947-
break;
948-
949945
case 'diagonal':
950946
return ctx.createLinearGradient(0, 0, this.x1, this.y1);
951-
break;
952947

953948
case 'left-right':
954949
return ctx.createLinearGradient(0, 0, this.x1, 0);
955-
break;
956950

957951
case 'top-bottom':
958952
return ctx.createLinearGradient(this.x1 / 2, 0, this.x1 / 2, this.y1);
959-
break;
960953

961954
case 'radial':
962955
return ctx.createRadialGradient(this.x1 / 2, this.y1 / 2, this.x1 / 2, this.x1 / 2, this.y1 / 2, 0);
963-
break;
964956
case 'custom':
965957
return ctx.createLinearGradient(
966958
getCustomCoordinateInPixels(this.customDirection.x0, this.x1),
967959
getCustomCoordinateInPixels(this.customDirection.y0, this.y1),
968960
getCustomCoordinateInPixels(this.customDirection.x1, this.x1),
969961
getCustomCoordinateInPixels(this.customDirection.y1, this.y1)
970962
);
971-
break;
972-
973963
}
974964
};
975965

dist/granim.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/assets/js/vendor/granim.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/setDirection.js

+1-10
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,25 @@ module.exports = function() {
44
var ctx = this.context;
55

66
switch(this.direction) {
7-
default:
8-
this.triggerError('direction');
9-
break;
10-
117
case 'diagonal':
128
return ctx.createLinearGradient(0, 0, this.x1, this.y1);
13-
break;
149

1510
case 'left-right':
1611
return ctx.createLinearGradient(0, 0, this.x1, 0);
17-
break;
1812

1913
case 'top-bottom':
2014
return ctx.createLinearGradient(this.x1 / 2, 0, this.x1 / 2, this.y1);
21-
break;
2215

2316
case 'radial':
2417
return ctx.createRadialGradient(this.x1 / 2, this.y1 / 2, this.x1 / 2, this.x1 / 2, this.y1 / 2, 0);
25-
break;
18+
2619
case 'custom':
2720
return ctx.createLinearGradient(
2821
getCustomCoordinateInPixels(this.customDirection.x0, this.x1),
2922
getCustomCoordinateInPixels(this.customDirection.y0, this.y1),
3023
getCustomCoordinateInPixels(this.customDirection.x1, this.x1),
3124
getCustomCoordinateInPixels(this.customDirection.y1, this.y1)
3225
);
33-
break;
34-
3526
}
3627
};
3728

test/animationSpec.js

+35-14
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,7 @@ describe('Animation: ', function() {
22
var event = document.createEvent('HTMLEvents');
33
var granimInstance, gradientColor, canvas,
44
canvasWidthMiddle, canvasHeightMiddle;
5-
6-
beforeEach(function(done) {
7-
setTimeout(function() {
8-
value = 0;
9-
done();
10-
}, 1);
11-
});
12-
13-
it('should support async execution of test preparation and expectations', function(done) {
14-
canvas = setCanvas();
15-
granimInstance = new Granim({
5+
var validOptions = {
166
element: '#granim-canvas',
177
name: 'granim',
188
direction: 'left-right',
@@ -22,16 +12,26 @@ describe('Animation: ', function() {
2212
gradients: [
2313
['hsl(333, 56%,89%)', '#181818', 'rgba(25,63, 48, .75)'],
2414
['#7b4397', 'hsla(126,0%, 19%,.9)', 'rgb(69, 89,169)'],
25-
2615
[{ color: '#833ab4', pos: .2 }, { color: 'rgb(255, 0,25)', pos: .6 }, { color: '#ff0080', pos: .95 }],
27-
2816
['#222', 'hsl(0, 5%, 5%)', 'rgb(255, 0,0)'],
2917
],
3018
transitionSpeed: 100,
3119
loop: true
3220
}
3321
}
34-
});
22+
};
23+
var newGranimInstance = function(options) { return new Granim(options) };
24+
25+
beforeEach(function(done) {
26+
setTimeout(function() {
27+
value = 0;
28+
done();
29+
}, 1);
30+
});
31+
32+
it('should support async execution of test preparation and expectations', function(done) {
33+
canvas = setCanvas();
34+
granimInstance = newGranimInstance(validOptions);
3535
event.initEvent('resize', true, false);
3636
window.dispatchEvent(event);
3737
canvasWidthMiddle = (canvas.width - 50) / 2;
@@ -57,6 +57,27 @@ describe('Animation: ', function() {
5757
}, 300);
5858
});
5959

60+
it('Gradient animation with custom direction is working', function(done) {
61+
granimInstance.destroy();
62+
granimInstance = newGranimInstance(Object.assign(
63+
Object.assign({}, validOptions),
64+
{
65+
direction: 'custom',
66+
customDirection: {
67+
x0: '10%',
68+
y0: '25px',
69+
x1: '30%',
70+
y1: '322px'
71+
}
72+
}
73+
));
74+
75+
setTimeout(function() {
76+
expect(granimInstance).toBeDefined();
77+
done();
78+
}, 300);
79+
});
80+
6081
afterEach(function() {
6182
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
6283
});

test/coreSpec.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ describe('Granim Core:', function() {
4343
expect(func).toThrowError('The element you used is neither a String, nor a HTMLCanvasElement');
4444
});
4545

46+
it('throws an error on invalid direction', function() {
47+
var invalidOptions = Object.assign(
48+
Object.assign({}, validOptions),
49+
{ direction: 'wrongDirectiion' }
50+
);
51+
var func = function() { return new Granim(invalidOptions) };
52+
expect(func).toThrowError(errorMessage('direction'));
53+
});
54+
4655
it('throws an error when the color type of the gradient is not valid', function() {
4756
var invalidOptions = Object.assign(
4857
Object.assign({}, validOptions),
@@ -76,10 +85,7 @@ describe('Granim Core:', function() {
7685
{
7786
states: {
7887
"default-state": {
79-
gradients: [
80-
[{ pos: .5 }, { color: '#fff', pos: .6 },],
81-
['#1CD8D2', '#93EDC7']
82-
]
88+
gradients: [[{ pos: .5 }, { color: '#fff', pos: .6 }], ['#1CD8D2', '#93EDC7']]
8389
}
8490
}
8591
}

test/methodsSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
describe('Methods: ', function() {
2-
var value, granimInstance;
2+
var granimInstance;
33

44
beforeEach(function(done) {
55
setTimeout(function() {

0 commit comments

Comments
 (0)