Skip to content

Commit 0944b5e

Browse files
committed
Added unit tests for loadFromCanvas and interval features
1 parent b8874b9 commit 0944b5e

File tree

6 files changed

+178
-15
lines changed

6 files changed

+178
-15
lines changed

package-lock.json

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
"express"
3333
],
3434
"license": "MIT",
35+
"jest": {
36+
"setupFiles": [
37+
"jest-canvas-mock"
38+
]
39+
},
3540
"dependencies": {
3641
"autoprefixer": "^9.7.2",
3742
"blob-stream": "^0.1.3",
@@ -107,6 +112,7 @@
107112
"html-webpack-plugin": "^3.2.0",
108113
"husky": "^3.0.0",
109114
"jest": "^24.8.0",
115+
"jest-canvas-mock": "^2.3.1",
110116
"lint-staged": "^9.2.0",
111117
"prettier": "^1.18.2",
112118
"stylelint": "^10.1.0",

src/utils/intervals.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
export default function getTimeInterval(currentFrameIndex, totalFrames) {
2-
const equalPercentage = 100 / totalFrames;
3-
return totalFrames === 1
1+
export default function getTimeInterval(
2+
currentFrameIndex = 0,
3+
totalFrames = 1
4+
) {
5+
const equalPercentage = 100 / (totalFrames || 1);
6+
return totalFrames === 1 || totalFrames === 0
47
? 100
58
: Math.round((currentFrameIndex + 1) * equalPercentage * 10) / 10;
69
}

src/utils/loadFromCanvas.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@ import { fromJS } from 'immutable';
22
import shortid from 'shortid';
33
import getTimeInterval from './intervals';
44

5-
const getHeightIntervals = (imageHeight, numberOfFrames) => {
6-
const heightPerFrame = Math.floor(imageHeight / numberOfFrames);
5+
export const getDimensionIntervals = (dimension, numberOfFrames) => {
6+
const dimensionPerFrame = Math.floor(dimension / numberOfFrames);
77
const intervals = [];
8-
let top = 0;
9-
let bottom = heightPerFrame;
8+
let start = 0;
9+
let end = dimensionPerFrame;
1010
for (let i = 0; i < numberOfFrames; i++) {
1111
intervals.push({
12-
top,
13-
bottom,
12+
start,
13+
end,
1414
timePercentage: getTimeInterval(i, numberOfFrames)
1515
});
16-
top += heightPerFrame;
17-
bottom += heightPerFrame;
16+
start += dimensionPerFrame;
17+
end += dimensionPerFrame;
1818
}
1919
return intervals;
2020
};
2121

2222
const generateFrames = (imageContext, numberOfFrames, pixSize = 1) => {
2323
const { width, height } = imageContext.canvas;
24-
const heightIntervals = getHeightIntervals(height, numberOfFrames);
24+
const heightIntervals = getDimensionIntervals(height, numberOfFrames);
2525
const frameCollection = [];
2626

2727
heightIntervals.forEach(heightInterval => {
@@ -30,8 +30,8 @@ const generateFrames = (imageContext, numberOfFrames, pixSize = 1) => {
3030

3131
const grid = [];
3232
for (
33-
let y = heightInterval.top;
34-
y + pixelHeight <= heightInterval.bottom;
33+
let y = heightInterval.start;
34+
y + pixelHeight <= heightInterval.end;
3535
y += pixelWidth
3636
) {
3737
for (let x = 0; x + pixelWidth <= width; x += pixelWidth) {
@@ -54,11 +54,13 @@ const generateFrames = (imageContext, numberOfFrames, pixSize = 1) => {
5454
});
5555
});
5656

57+
console.log(frameCollection);
58+
5759
return fromJS(frameCollection);
5860
};
5961

6062
export const getCanvasDimensions = canvasRef => {
61-
if (canvasRef) {
63+
if (canvasRef && canvasRef.current) {
6264
const canvas = canvasRef.current;
6365
const context = canvas.getContext('2d');
6466
return { w: context.canvas.width, h: context.canvas.height };

test/utils/intervals.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import getTimeInterval from '../../src/utils/intervals';
2+
3+
describe('intervals tests', () => {
4+
describe('getTimeInterval', () => {
5+
describe('When the total of frames value is 0 or 1', () => {
6+
it('should return always 100', () => {
7+
expect(getTimeInterval(0, 0)).toEqual(100);
8+
expect(getTimeInterval(1, 0)).toEqual(100);
9+
expect(getTimeInterval(0, 1)).toEqual(100);
10+
expect(getTimeInterval(1, 1)).toEqual(100);
11+
});
12+
});
13+
describe('When the total of frames value is greater than 1', () => {
14+
it('should return the proper interval', () => {
15+
expect(getTimeInterval(0, 2)).toEqual(50);
16+
expect(getTimeInterval(1, 2)).toEqual(100);
17+
expect(getTimeInterval(0, 3)).toEqual(33.3);
18+
expect(getTimeInterval(1, 3)).toEqual(66.7);
19+
expect(getTimeInterval(2, 3)).toEqual(100);
20+
expect(getTimeInterval(0, 4)).toEqual(25);
21+
expect(getTimeInterval(1, 4)).toEqual(50);
22+
expect(getTimeInterval(2, 4)).toEqual(75);
23+
expect(getTimeInterval(3, 4)).toEqual(100);
24+
});
25+
});
26+
});
27+
});

test/utils/loadFromCanvas.test.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import 'jest-canvas-mock';
2+
import {
3+
getDimensionIntervals,
4+
getCanvasDimensions
5+
} from '../../src/utils/loadFromCanvas';
6+
7+
describe('loadFromCanvas tests', () => {
8+
describe('getCanvasDimensions', () => {
9+
describe('When the canvas ref received is not valid', () => {
10+
it('should return width and height 0', () => {
11+
const expectedOutput = { w: 0, h: 0 };
12+
expect(getCanvasDimensions(null)).toEqual(expectedOutput);
13+
expect(getCanvasDimensions({})).toEqual(expectedOutput);
14+
});
15+
});
16+
describe('When canvasRef contains a reference to a canvas', () => {
17+
it('should return its width and height dimensions', () => {
18+
const CANVAS_WIDTH = 100;
19+
const CANVAS_HEIGHT = 200;
20+
const canvas = document.createElement('canvas');
21+
canvas.width = CANVAS_WIDTH;
22+
canvas.height = CANVAS_HEIGHT;
23+
const canvasRef = { current: canvas };
24+
const expectedOutput = { w: CANVAS_WIDTH, h: CANVAS_HEIGHT };
25+
26+
expect(getCanvasDimensions(canvasRef)).toEqual(expectedOutput);
27+
});
28+
});
29+
});
30+
describe('getDimensionIntervals', () => {
31+
describe('If only one frame is received', () => {
32+
describe('and dimension is greater than 0', () => {
33+
it('should return only an interval 0-100, 100%', () => {
34+
const expectedOutput = [
35+
{
36+
start: 0,
37+
end: 100,
38+
timePercentage: 100
39+
}
40+
];
41+
expect(getDimensionIntervals(100, 1)).toEqual(expectedOutput);
42+
});
43+
});
44+
describe('and dimension is 0 or null', () => {
45+
it('should return only an interval 0-0, 100%', () => {
46+
const expectedOutput = [
47+
{
48+
start: 0,
49+
end: 0,
50+
timePercentage: 100
51+
}
52+
];
53+
expect(getDimensionIntervals(0, 1)).toEqual(expectedOutput);
54+
expect(getDimensionIntervals(null, 1)).toEqual(expectedOutput);
55+
});
56+
});
57+
});
58+
59+
describe('For a given dimension size and more than one frame', () => {
60+
it('should return each equal interval with start, end and time percentage', () => {
61+
const expectedOutput = [
62+
{
63+
start: 0,
64+
end: 25,
65+
timePercentage: 25
66+
},
67+
{
68+
start: 25,
69+
end: 50,
70+
timePercentage: 50
71+
},
72+
{
73+
start: 50,
74+
end: 75,
75+
timePercentage: 75
76+
},
77+
{
78+
start: 75,
79+
end: 100,
80+
timePercentage: 100
81+
}
82+
];
83+
expect(getDimensionIntervals(100, 4)).toEqual(expectedOutput);
84+
});
85+
});
86+
describe('If the frame value is 0 or null', () => {
87+
it('should return an empty array', () => {
88+
expect(getDimensionIntervals(100, 0)).toEqual([]);
89+
});
90+
});
91+
});
92+
});

0 commit comments

Comments
 (0)