Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dwnz committed Nov 1, 2016
1 parent 92fb2db commit d7bbe49
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ When the image has loaded

## Example

See [test/index.html](test/index.html)
See [example/index.html](example/index.html)
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
"test",
"tests"
],
"version": "1.0.0"
"version": "1.0.1"
}
File renamed without changes.
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ function ImageLoader(grid, imagePath, width, height, swatchColor, onclick, callb

if (self.image.width > self.image.height) {
diff = gridWidth / self.image.width;
self.display.width = self.image.width * diff;
self.display.height = self.image.height * diff;
self.display.width = Math.ceil(self.image.width * diff);
self.display.height = Math.ceil(self.image.height * diff);
} else {
diff = gridWidth / self.image.height;
self.display.width = gridWidth;
self.display.height = self.image.height * diff;
diff = gridWidth / self.image.width;
self.display.width = Math.ceil(gridWidth);
self.display.height = Math.ceil(self.image.height * diff);
}
};

Expand Down
1 change: 1 addition & 0 deletions node_modules/.bin/nodeunit

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "smartimageloader",
"version": "1.0.1",
"description": "``` new ImageLoader(GridID, ImageUrl, Width, Height, PreloadBackgroundHex, OnClick, Callback) ```",
"main": "index.js",
"directories": {
"test": "tests"
},
"repository": {
"type": "git",
"url": "git+https://github.com/dwnz/image-loader.git"
},
"author": "DWNZ",
"license": "MIT",
"bugs": {
"url": "https://github.com/dwnz/image-loader/issues"
},
"homepage": "https://github.com/dwnz/image-loader#readme"
}
157 changes: 157 additions & 0 deletions test/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>smartimageloader tests</title>
<link href="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.css" rel="stylesheet"/>
</head>
<body>
<div id="mocha"></div>
<div style="visibility: hidden">
<div id="grid1" style="width: 1300px"></div>
<div id="grid2" style="width: 300px"></div>
<div id="grid3" style="width: 1300px"></div>
<div id="grid4" style="width: 300px"></div>
<div id="grid5" style="width: 1000px"></div>
</div>

<script src="https://cdn.rawgit.com/jquery/jquery/2.1.4/dist/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/Automattic/expect.js/0.3.1/index.js"></script>
<script src="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.js"></script>
<script src="https://cdn.rawgit.com/Automattic/expect.js/master/index.js"></script>

<script src="../index.js"></script>

<script>mocha.setup('bdd')</script>

<script>
describe("when creating an image", function () {
it("applies values correctly", function (done) {
var imageLoader = new ImageLoader(
'grid1',
'https://images.unsplash.com/photo-1435777940218-be0b632d06db?dpr=1&auto=compress,format&fit=crop&w=1199&h=675&q=80&cs=tinysrgb&crop=',
1199,
675,
'#efefef',
null,
function (item) {
expect(item.image.path).to.be('https://images.unsplash.com/photo-1435777940218-be0b632d06db?dpr=1&auto=compress,format&fit=crop&w=1199&h=675&q=80&cs=tinysrgb&crop=');
expect(item.image.width).to.be(1199);
expect(item.image.height).to.be(675);
expect(item.image.swatch).to.be('#efefef');
expect(item.image.click).to.be(null);

expect(item.id).to.not.be(null);
expect(item.img).to.not.be(null);
expect(item.grid).to.not.be(null);
expect(item.callback).to.not.be(null);

done();
}
);
});

describe('when creating a horizontal image loader', function () {
it('should use full width and height when grid is bigger', function (done) {
var imageLoader = new ImageLoader(
'grid1',
'https://images.unsplash.com/photo-1435777940218-be0b632d06db?dpr=1&auto=compress,format&fit=crop&w=1199&h=675&q=80&cs=tinysrgb&crop=',
1199,
675,
'#efefef',
null,
function (item) {
expect(item.display.width).to.be(1300);
expect(item.display.height).to.be(732);
done();
}
);
});

it('should use resized values when grid is smaller', function (done) {
var imageLoader = new ImageLoader(
'grid2',
'https://images.unsplash.com/photo-1435777940218-be0b632d06db?dpr=1&auto=compress,format&fit=crop&w=1199&h=675&q=80&cs=tinysrgb&crop=',
1199,
675,
'#efefef',
null,
function (item) {
expect(item.display.width).to.be(300);
expect(item.display.height).to.be(169);
done();
}
);
});
});

describe('when creating a vertical image loader', function () {
it('should use full width and height when grid is bigger', function (done) {
var imageLoader = new ImageLoader(
'grid3',
'https://images.unsplash.com/photo-1435777940218-be0b632d06db?dpr=1&auto=compress,format&fit=crop&w=675&h=1199&q=80&cs=tinysrgb&crop=',
675,
1199,
'#efefef',
null,
function (item) {
expect(item.display.width).to.be(1300);
expect(item.display.height).to.be(2310);

done();
}
);
});

it('should use resized values when grid is smaller', function (done) {
var imageLoader = new ImageLoader(
'grid4',
'https://images.unsplash.com/photo-1435777940218-be0b632d06db?dpr=1&auto=compress,format&fit=crop&w=675&h=1199&q=80&cs=tinysrgb&crop=',
675,
1199,
'#efefef',
null,
function (item) {
expect(item.display.width).to.be(300);
expect(item.display.height).to.be(533);
done();
}
);
});
});
});

describe("when clicking an image", function () {
it("should fire click event when attached", function (done) {
var imageId = null;

var imageLoader = new ImageLoader(
'grid5',
'https://images.unsplash.com/photo-1435777940218-be0b632d06db?dpr=1&auto=compress,format&fit=crop&w=675&h=1199&q=80&cs=tinysrgb&crop=',
675,
1199,
'#efefef',
function (image, imageId, event) {
expect(imageId).to.be(imageId);
expect(image).to.not.be(null);
expect(event).to.not.be(null);
done();
},
function (item) {
imageId = item.id;
$('#grid5').find('img').click();
}
);
});
});
</script>


<script>
mocha.checkLeaks();
mocha.globals(['jQuery']);
mocha.run();
</script>

</body>
</html>

0 comments on commit d7bbe49

Please sign in to comment.