Skip to content

Commit

Permalink
Add source maps; add trap location generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Ant-f committed Oct 25, 2018
1 parent 432fd70 commit 479e824
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 1 deletion.
1 change: 1 addition & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module.exports = function(config) {

webpack: {
module: webpackConfig.module,
plugins: webpackConfig.plugins,
resolve: webpackConfig.resolve
},

Expand Down
21 changes: 21 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"karma-mocha": "^1.3.0",
"karma-webpack": "^3.0.5",
"mocha": "^5.2.0",
"source-map-loader": "^0.2.4",
"webpack": "^4.22.0",
"webpack-cli": "^3.1.2",
"webpack-dev-server": "^3.1.10"
Expand Down
25 changes: 25 additions & 0 deletions src/services/trap-location-generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class LocationGenerator {
getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}

generateLocations(width, height, trapCount) {
const traps = {};

for (let n = 0; n < trapCount; n++) {
const x = this.getRandomInt(width);
const y = this.getRandomInt(height);

if (traps[x]) {
traps[x].push(y);
}
else {
traps[x] = [y];
}
}

return traps;
}
}

export default LocationGenerator;
52 changes: 52 additions & 0 deletions test/services/trap-location-generator.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* global describe, it */

import { expect } from 'chai';
import Generator from '../../src/services/trap-location-generator';

describe('trap location generator', function() {
it('returns new trap locations', function() {

// Arrange

const count = 3;
const height = 3;
const width = 3;

// Act

const generator = new Generator();
const locations = generator.generateLocations(width, height, count);

// Assert

const keys = Object.keys(locations);
expect(keys.length).to.be.greaterThan(0);

keys.forEach(xValue => {
expect(Number(xValue), 'x co-ordinate should be at least 0').to.be.at.least(0);

locations[xValue].forEach(yValue => {
expect(yValue, 'y co-ordinate should be at least 0').to.be.at.least(0);
});
});
});

it('returns different locations each time', function() {

// Arrange

const count = 3;
const height = 3;
const width = 3;

// Act

const generator = new Generator();
const locations1 = generator.generateLocations(width, height, count);
const locations2 = generator.generateLocations(width, height, count);

// Assert

expect(locations1).not.to.deep.equal(locations2);
});
});
11 changes: 10 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* global module, __dirname */
/* global __dirname, module, require */

const webpack = require('webpack');

module.exports = {
devServer: {
Expand All @@ -7,6 +9,13 @@ module.exports = {
},

mode: 'development',

plugins: [
new webpack.SourceMapDevToolPlugin({
filename: null, // inline sourcemap
test: /\.js($|\?)/i // case-insensitive match for js files
})
],

entry: './src/index.jsx',

Expand Down

0 comments on commit 479e824

Please sign in to comment.