Last active
March 27, 2019 12:06
-
-
Save gucu112/e1a1e6430bb7bb7038114e318d2ad49e to your computer and use it in GitHub Desktop.
Patterns and snippets that I have made while learning JavaScript back in 2016.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('jasmine-beforeall'); | |
describe('Console class', function () { | |
var intercept = require('intercept-stdout'); | |
var Console = require('../../core/console'); | |
var ConsoleMock = Console.getMock(); | |
var Data = new Object(); | |
beforeAll(function () { | |
Data.unhookIntercept = intercept( | |
function (data) { | |
Data.stdout += data; | |
}, | |
function (data) { | |
Data.stderr += data; | |
} | |
); | |
}); | |
beforeEach(function () { | |
Data.stdout = ''; | |
Data.stderr = ''; | |
}); | |
afterAll(function () { | |
Data.unhookIntercept(); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let request = require('request'); | |
describe('Index routes', function () { | |
describe('GET /', function () { | |
let url = 'http://localhost:3000'; | |
it('returns status code 200', function (done) { | |
request(url, function (err, res, body) { | |
expect(res.statusCode).toEqual(200); | |
done(); | |
}); | |
}); | |
it('returns body', function (done) { | |
request(url, function (err, res, body) { | |
expect(body).toBeDefined(); | |
done(); | |
}); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('jasmine-beforeall'); | |
describe('Logger class', function () { | |
var fs = require('fs'); | |
var path = require('path'); | |
var Logger = require('../../core/logger'); | |
var LoggerMock = Logger.getMock(); | |
var Data = new Object(); | |
beforeAll(function () { | |
LoggerMock.dir = 'tests'; | |
LoggerMock.base = 'logger.log'; | |
Data.values = [ | |
undefined, | |
null, | |
NaN, | |
Infinity, | |
0, | |
10, | |
3.21, | |
-8, | |
'', | |
'string', | |
true, | |
false, | |
[1, 2, 3], | |
['a', 'b', 'c'], | |
[{}, 2, 'c'], | |
/(.*)/g, | |
function () { return; }, | |
(a, b) => a + b, | |
{}, | |
{ property: 'value' } | |
]; | |
}); | |
beforeEach(function (done) { | |
this.addMatchers({ | |
toBeProperlyLoggedAs: function (stamp, expected) { | |
this.actual = this.actual.split('\n').slice(0, -1); | |
if (expected instanceof Array === false) { | |
expected = new Array(expected); | |
} | |
if (this.actual.length !== expected.length) { | |
this.message = function () { | |
return 'Expected ' + this.actual.length + ' to equal ' + expected.length + '.'; | |
}; | |
return false; | |
} | |
var information; | |
expected.forEach((element, index) => { | |
information = '[' + stamp + '] ' + element; | |
expect(this.actual[index]).toMatch(/^\d{2}:\d{2}:\d{2}.\d{3}(.*)$/); | |
expect(this.actual[index]).toContain(information); | |
}); | |
return true; | |
} | |
}); | |
fs.writeFile(path.format(LoggerMock), '', 'utf-8', (err) => { | |
if (err) throw err; | |
done(); | |
}); | |
}); | |
it('converted to string represents Logger object', function () { | |
expect(Logger.toString()).toEqual('[object Logger]'); | |
expect(LoggerMock.toString()).toEqual('[object Logger]'); | |
}); | |
it('has truthy dir member', function () { | |
expect(Logger.dir).toBeTruthy(); | |
expect(LoggerMock.dir).toEqual('tests'); | |
}); | |
it('has truthy base member', function () { | |
expect(Logger.base).toBeTruthy(); | |
expect(LoggerMock.base).toEqual('logger.log'); | |
}); | |
it('logs Error object as error message properly', function (done) { | |
LoggerMock.error(new Error('custom')); | |
fs.readFile(path.format(LoggerMock), 'utf-8', (err, data) => { | |
if (err) throw err; | |
expect(data).toBeProperlyLoggedAs(Logger.ERROR, 'Error: custom'); | |
done(); | |
}); | |
}); | |
it('logs example values as error messages properly', function (done) { | |
Logger.error.apply(LoggerMock, Data.values); | |
fs.readFile(path.format(LoggerMock), 'utf-8', (err, data) => { | |
if (err) throw err; | |
expect(data).toBeProperlyLoggedAs(Logger.ERROR, Data.values); | |
done(); | |
}); | |
}); | |
it('logs example values as warn messages properly', function (done) { | |
Logger.warn.apply(LoggerMock, Data.values); | |
fs.readFile(path.format(LoggerMock), 'utf-8', (err, data) => { | |
if (err) throw err; | |
expect(data).toBeProperlyLoggedAs(Logger.WARN, Data.values); | |
done(); | |
}); | |
}); | |
it('logs example values as info messages properly', function (done) { | |
Logger.info.apply(LoggerMock, Data.values); | |
fs.readFile(path.format(LoggerMock), 'utf-8', (err, data) => { | |
if (err) throw err; | |
expect(data).toBeProperlyLoggedAs(Logger.INFO, Data.values); | |
done(); | |
}); | |
}); | |
afterAll(function (done) { | |
fs.unlink(path.format(LoggerMock), (err) => { | |
if (err) throw err; | |
done(); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Person(name, age) { | |
this.setName(name); | |
this.setAge(age); | |
} | |
Person.prototype.getName = function () { | |
return this.name; | |
} | |
Person.prototype.setName = function (name) { | |
this.name = name; | |
} | |
Person.prototype.getAge = function () { | |
return this.age; | |
} | |
Person.prototype.setAge = function (age) { | |
this.age = age; | |
} | |
Person.prototype.incrementAge = function () { | |
this.age += 1; | |
} | |
Person.prototype.decrementAge = function () { | |
this.age -= 1; | |
} | |
window.Person = Person; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @module Person | |
* @requires path | |
*/ | |
var Person = (function () { | |
var requiresExample = require('path'); | |
/** | |
* [[Inner member desc]] | |
* @member {object} privateMember | |
*/ | |
var privateMember; | |
/** | |
* [[Ctor desc]] | |
* @class Person | |
* @extends Object | |
* @classdesc [[Class desc]] | |
*/ | |
function Person() { | |
/** | |
* [[Instance member desc]] | |
* @alias module:Person~Person#publicMember | |
*/ | |
this.publicMember; | |
/** | |
* [[Instance method desc]] | |
* @alias module:Person~Person#publicMethod | |
* @returns {object} [[Returns desc]] | |
*/ | |
this.publicMethod = function () { | |
return null; | |
} | |
} | |
Person.prototype = Object.create(Object.prototype); | |
/** | |
* [[Static member desc]] | |
* @memberof module:Person~Person | |
* @member {object} staticMember | |
*/ | |
Person.prototype.staticMember = null; | |
/** | |
* [[Static method desc]] | |
* @memberof module:Person~Person | |
* @method staticMethod | |
* @returns {object} [[Returns desc]] | |
*/ | |
Person.prototype.staticMethod = function () { | |
return null; | |
} | |
/** | |
* [[Inner method desc]] | |
* @method privateMethod | |
* @returns {object} [[Returns desc]] | |
*/ | |
function privateMethod() { | |
return null; | |
} | |
return null; | |
})(); | |
/** | |
* [[Module desc]] | |
* @returns {object} [[Returns desc]] | |
*/ | |
module.exports = Person; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let Person = window.Person; | |
let person = new Person('Bartek', 25); | |
describe('Person class', function () { | |
it('has name', function () { | |
expect(person.getName()).toBeDefined(); | |
}); | |
it('name should be equal to "Bartek"', function () { | |
expect(person.getName()).toEqual('Bartek'); | |
}); | |
it('has age', function () { | |
expect(person.getAge()).toBeDefined(); | |
}); | |
it('age should be greater than 18', function () { | |
expect(person.getAge()).toBeGreaterThan(18); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// class | |
let Person = (function () { | |
// static fields | |
let _static; | |
// fields | |
let _name = Symbol('Person.name'); | |
// constructor | |
function Person(name) { | |
if (this instanceof Person) { | |
this[_name] = name; | |
} else { | |
return new Person(name); | |
} | |
} | |
// prototype | |
Person.prototype = Object.create(Config.prototype, { | |
// constructor assignment | |
constructor: { | |
value: Person, | |
configurable: true, | |
writable: true | |
}, | |
// getter and setter | |
name: { | |
get: function () { | |
return this[_name]; | |
}, | |
set: function (value) { | |
this[_name] = value; | |
}, | |
configurable: true | |
}, | |
// function | |
sayName: { | |
value: function () { | |
console.log(this.name); | |
}, | |
configurable: true, | |
writable: true | |
}, | |
// overrides | |
valueOf: { | |
value: function () { | |
return 'Person(name:"' + this.name + '")'; | |
}, | |
configurable: true, | |
writable: true | |
}, | |
toString: { | |
value: function () { | |
return '[object Person]'; | |
}, | |
configurable: true, | |
writable: true | |
} | |
}); | |
// return | |
return Person; | |
}()); | |
// export | |
module.exports = Person; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment