Skip to content

Instantly share code, notes, and snippets.

View gist:889f3843ba8299677f21de16afcbb882
//guess where this fails.
this.x = function() {
hello();
}
(foo || bar).call();
@jhartikainen
jhartikainen / lol.js
Last active Dec 13, 2016
JavaScript doesn't have classes lololo
View lol.js
//java
class X extends Y {
public X() {
super();
}
public void method() {
}
public static staticMethod() {
@jhartikainen
jhartikainen / test.js
Last active Nov 25, 2016
Avoiding multiple asserts
View test.js
//easy way to check if object has the expected "shape":
it('tests something', function() {
var expectedObject = {
some: 'prop',
values: 'here'
};
var result = doStuff();
assert.deepEqual(result, expectedObject);
View timeouts.js
var moves = []..
function makeMove() {
var m = moves.shift();
if(!m) {
return everythingDone();
}
setTimeout(function() {
activateButton(m);
View chain.js
//let's say all the moves are in this array
var moves = [...];
moves.reduce(function(p, move) {
return p.then(activateWithDelay(move.button))
.then(deactivateWithDelay(move.button));
}, Promise.resolve());
View foo.js
it('"moveRight()" should increment the "x" propety by 1', function () {
var blocky = new Block(15, 30);
blocky.moveRight();
assert.equal(blocky.x, 16);
});
//....vs....
it('"moveRight()" should increment the "x" propety by 1', function () {
var startingX = 15;
@jhartikainen
jhartikainen / paramTest.js
Created Jun 12, 2016
Parametrized test with Mocha example
View paramTest.js
describe('Something', function() {
var values = ['a', 'b', 'c'];
values.forEach(function(val) {
it('should do something with ' + val + ' in this test', function() {
assert.equal(foo(), val);
});
})
});
@jhartikainen
jhartikainen / foo.js
Last active Dec 4, 2016
Using angular isolated scopes as event emitters
View foo.js
someModule.service('something', function($rootScope) {
var emitter = $rootScope.$new(true);
var data = whateverStuffHere;
return {
onDataUpdated: function(callback) {
//by returning the result from $on, we get
//the ability to remove event listeners easily
return emitter.$on('update', callback);
@jhartikainen
jhartikainen / tests.js
Created May 13, 2016
Example of stubbing a complex object
View tests.js
describe('something', function() {
var oldXrm;
beforeEach(function() {
oldXrm = Xrm;
Xrm = {
Page: {
getAttribute: sinon.stub()
}
};
@jhartikainen
jhartikainen / sandbox.js
Last active Mar 24, 2016
Manual Sinon sandboxes
View sandbox.js
describe('something', function() {
var sandbox;
beforeEach(function() {
sandbox = sinon.sandbox.create();
});
afterEach(function() {
sandbox.restore();
});