Skip to content

Instantly share code, notes, and snippets.

@mlms13
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlms13/e9f9389825f63de5de6e to your computer and use it in GitHub Desktop.
Save mlms13/e9f9389825f63de5de6e to your computer and use it in GitHub Desktop.
Unit tests for a function that calculates items that fit in a row, given row width, item width, and gutter between items.
module.exports = {
/**
* Given the width of a container, the width of an item, and the gutter between inner items,
* calculate the number of items that can fit on a single row inside the container.
* @param {number} rowWidth the width, in pixels, of the container
* @param {number} itemWidth the width of each individual item in the container
* @param {number} [gutter] the space, in pixels, between each item (optional)
* @return {number} the number of items that fit in a row without overflow, never less than 1
*/
calculateItemsPerRow: function (rowWidth, itemWidth, gutter) {
var itemsPerRow;
// magic
return itemsPerRow;
}
}
var should = require('should'); // from should.js, but optionally chai.should
var calculateItemsPerRow = require('./module').calculateItemsPerRow;
describe("calculateItemsPerRow", function () {
it("should return the number of items that can fit in a row", function () {
calculateItemsPerRow(900, 300).should.equal(3);
calculateItemsPerRow(1000, 300, 50).should.equal(3);
calculateItemsPerRow(1000, 300, 51).should.equal(2);
calculateItemsPerRow(1000, 50).should.equal(20);
calculateItemsPerRow(1000, 50, 200).should.equal(4);
});
it("should never return less than one", function () {
calculateItemsPerRow(200, 300).should.equal(1);
calculateItemsPerRow(200, 200, 10).should.equal(1);
calculateItemsPerRow(100, 1, 200).should.equal(1);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment