1
0
mirror of https://github.com/ascribe/onion.git synced 2024-11-14 17:15:08 +01:00

merge conflict

This commit is contained in:
Tim Daubenschütz 2015-06-08 13:47:24 +02:00
commit 6d337890d0
7 changed files with 159 additions and 43 deletions

5
.eslintignore Normal file
View File

@ -0,0 +1,5 @@
.
gulpfile.js
node_modules
js/**/__tests__

View File

@ -10,6 +10,7 @@ The code is JavaScript ECMA 6.
Getting started
===============
Install some nice extensions for Chrom(e|ium):
<<<<<<< HEAD
- [Allow-Control-Allow-Origin](https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi):
we need this to open connection to external hosts ([staging.ascribe.io](http://staging.ascribe.io/) in our case).
Please note that there is an [open issue](https://github.com/vitvad/Access-Control-Allow-Origin/issues/22) that prevents the extension to save the changes in the "intercepted URL or URL pattern". You can follow [this workaround](https://github.com/vitvad/Access-Control-Allow-Origin/issues/22#issuecomment-109898052) to fix the problem.
@ -34,11 +35,27 @@ For this project, we're using:
* We don't use camel case for file naming but in everything Javascript related
* We use `let` instead of `var`: [SA Post](http://stackoverflow.com/questions/762011/javascript-let-keyword-vs-var-keyword)
Testing
===============
We're using Facebook's jest to do testing as it integrates nicely with react.js as well.
Tests are always created per directory by creating a `__tests__` folder. To test a specific file, a `<file_name>_tests.js` file needs to be created.
Since we're using mixed syntax, test files are not linted using ES6Lint.
This is due to the fact that jest's function mocking and ES6 module syntax are [fundamentally incompatible](https://github.com/babel/babel-jest/issues/16).
Therefore, to require a module in your test file, you need to use CommonJS's `require` syntax. Except for this, all tests can be written in ES6 syntax.
## Workflow
Generally, when you're runing `gulp serve`, all tests are being run.
If you want to test exclusively (without having the obnoxious ES6Linter warnings), you can just run `gulp jest:watch`.
Troubleshooting
===============
Q: OMG nothing works
A: try `npm install`. Someone may have updated some dependencies

View File

@ -1,5 +1,7 @@
'use strict';
require("harmonize")();
var gulp = require('gulp');
var gulpif = require('gulp-if');
var sourcemaps = require('gulp-sourcemaps');
@ -15,23 +17,52 @@ var sass = require('gulp-sass');
var concat = require('gulp-concat');
var _ = require('lodash');
var eslint = require('gulp-eslint');
var jest = require('jest-cli');
var config = {
bootstrapDir: './node_modules/bootstrap-sass'
bootstrapDir: './node_modules/bootstrap-sass',
jestOptions: {
rootDir: 'js',
scriptPreprocessor: '../node_modules/babel-jest',
testFileExtensions: [
'es6',
'js'
],
unmockedModulePathPatterns: [
'<rootDir>/node_modules/react',
'<rootDir>/node_modules/react-tools'
],
moduleFileExtensions: [
'js',
'json',
'react',
'es6'
]
}
};
gulp.task('build', function() {
bundle(false);
});
gulp.task('serve', ['browser-sync', 'lint:watch', 'sass', 'sass:watch', 'copy'], function() {
gulp.task('serve', ['browser-sync', 'jest:watch', 'lint:watch', 'sass', 'sass:watch', 'copy'], function() {
bundle(true);
});
gulp.task('jest', function(done) {
jest.runCLI({ config : config.jestOptions }, ".", function() {
done();
});
});
gulp.task('jest:watch', function(done) {
gulp.watch([ config.jestOptions.rootDir + "/**/*.js" ], [ 'jest' ]);
});
gulp.task('browser-sync', function() {
browserSync({
server: {
baseDir: "."
baseDir: '.'
},
port: process.env.PORT || 3000
});
@ -44,7 +75,8 @@ gulp.task('sass', function () {
includePaths: [
config.bootstrapDir + '/assets/stylesheets'
]
}).on('error', sass.logError))
})
.on('error', sass.logError))
.pipe(sourcemaps.write('./maps'))
.pipe(gulp.dest('./build/css'))
.pipe(browserSync.stream());;
@ -74,10 +106,9 @@ gulp.task('lint', function () {
.pipe(eslint())
// eslint.format() outputs the lint results to the console.
// Alternatively use eslint.formatEach() (see Docs).
.pipe(eslint.format())
.pipe(eslint.format());
// To have the process exit with an error code (1) on
// lint error, return the stream and pipe to failOnError last.
.pipe(eslint.failOnError());
});
gulp.task('lint:watch', function () {
@ -86,7 +117,6 @@ gulp.task('lint:watch', function () {
function bundle(watch) {
var bro;
if (watch) {
bro = watchify(browserify('./js/app.js',
// Assigning debug to have sourcemaps
@ -101,11 +131,11 @@ function bundle(watch) {
debug: true
});
}
bro.transform(babelify.configure({
compact: false
}));
function rebundle(bundler, watch) {
return bundler.bundle()
.on('error', notify.onError('Error: <%= error.message %>'))
@ -118,6 +148,6 @@ function bundle(watch) {
.pipe(gulp.dest('./build'))
.pipe(browserSync.stream());
}
return rebundle(bro);
}

View File

@ -0,0 +1,69 @@
jest.dontMock('../general_utils.js');
let GeneralUtils = require('../general_utils')
let sanitize = GeneralUtils.sanitize;
let sumNumList = GeneralUtils.sumNumList;
let formatText = GeneralUtils.formatText;
let mergeOptions = GeneralUtils.mergeOptions;
describe('GeneralUtils', () => {
it('should sanitize object (delete all empty strings, null and undefined values)', () => {
let obj = {
definedValue: 1,
undefinedValue: undefined,
nullValue: null,
emptyString: '',
notEmptyString: 'Something else'
};
let sanitizedObj = sanitize(obj);
expect(sanitizedObj.definedValue).toBeDefined();
expect(sanitizedObj.notEmptyString).toBeDefined();
expect(sanitizedObj.undefinedValue).toBeUndefined();
expect(sanitizedObj.nullValue).toBeUndefined();
expect(sanitizedObj.emptyString).toBeUndefined();
});
it('should sum up all values of a list', () => {
expect(sumNumList([1,2,3])).toBe(6);
expect(sumNumList([1,-2,3])).toBe(2);
expect(sumNumList(['string',2,3])).toBe(5);
expect(sumNumList([0,0,0])).toBe(0);
expect(sumNumList([() => 'asdasd',2,3])).toBe(5);
});
it('should format a string with inline variables to a plain string using an arbitrary number of arguments', () => {
expect(formatText('Number %d', 1)).toBe('Number 1');
expect(formatText('Number %s', 1)).toBe('Number 1');
expect(formatText('Number %d %s', '1', 2)).toBe('Number 1 2');
expect(formatText('Number %d %d %d %s %d %d %d', 1, 2, 3, 4, 5, 6, 7)).toBe('Number 1 2 3 4 5 6 7');
});
it('should merge n objects keys and values and throw an error on matching-keys-overwrite', () => {
let obj1 = {
name: 'Obj1',
someValue: 'hello'
};
let obj2 = {
othername: 'Obj2',
someValue: 'world'
};
let obj3 = {
othername: 'Obj3',
someFn: () => parseFloat(0.9),
numberValue: 2
};
expect(() => mergeOptions(obj1, obj2)).toThrow();
// jasmine actually does a deep comparison for functions as well
expect(mergeOptions(obj1, obj3)).toEqual({
name: obj1.name,
someValue: obj1.someValue,
othername: obj3.othername,
someFn: obj3.someFn,
numberValue: obj3.numberValue
});
});
});

View File

@ -1,7 +1,16 @@
'use strict';
// TODO: Create Unittests that test all functions
/**
* Takes an object and deletes all keys that are
*
* - empty strings; or
* - null; or
* - undefined
*
*
* @param {object} obj regular javascript object
* @return {object} regular javascript object without null values or empty strings
*/
export function sanitize(obj) {
Object
.keys(obj)
@ -16,15 +25,6 @@ export function sanitize(obj) {
return obj;
}
/**
* Returns the values of an object.
*/
export function valuesOfObject(obj) {
return Object
.keys(obj)
.map(key => obj[key]);
}
/**
* Sums up a list of numbers. Like a Epsilon-math-kinda-sum...
*/
@ -37,17 +37,14 @@ export function sumNumList(l) {
/*
Taken from http://stackoverflow.com/a/4795914/1263876
Behaves like C's format string function
REFACTOR TO ES6 (let instead of var)
*/
export function formatText() {
var args = arguments,
let args = arguments,
string = args[0],
i = 1;
return string.replace(/%((%)|s|d)/g, function (m) {
return string.replace(/%((%)|s|d)/g, (m) => {
// m is the matched format, e.g. %s, %d
var val = null;
let val = null;
if (m[2]) {
val = m[2];
} else {
@ -79,7 +76,6 @@ export function mergeOptions(...l) {
for(let i = 1; i < l.length; i++) {
newObj = _mergeOptions(newObj, _mergeOptions(l[i - 1], l[i]));
}
return newObj;
}
@ -92,7 +88,16 @@ export function mergeOptions(...l) {
*/
function _mergeOptions(obj1, obj2){
let obj3 = {};
for (let attrname in obj1) { obj3[attrname] = obj1[attrname]; }
for (let attrname in obj2) { obj3[attrname] = obj2[attrname]; }
for (let attrname in obj1) {
obj3[attrname] = obj1[attrname];
}
for (let attrname in obj2) {
if(attrname in obj3) {
throw Error('Overwrite Conflict: You\'re merging two objects with the same keys: ' + attrname);
} else {
obj3[attrname] = obj2[attrname];
}
}
return obj3;
}

View File

@ -29,4 +29,4 @@ export function getLangText(s, ...args) {
}
}
}
}

View File

@ -3,15 +3,12 @@
"version": "0.0.1",
"description": "Das neue web client for Ascribe",
"main": "js/app.js",
"scripts": {
"lint": "eslint ./js"
},
"author": "Ascribe",
"license": "Copyright",
"private": true,
"devDependencies": {
"babel-eslint": "^3.1.11",
"babel-jest": "^4.0.0",
"babel-jest": "^5.2.0",
"babelify": "^6.1.2",
"bootstrap-sass": "^3.3.4",
"browser-sync": "^2.7.5",
@ -27,6 +24,7 @@
"gulp-sass": "^2.0.1",
"gulp-sourcemaps": "^1.5.2",
"gulp-util": "^3.0.4",
"harmonize": "^1.4.2",
"jest-cli": "^0.4.0",
"lodash": "^3.9.3",
"react-textarea-autosize": "^2.2.3",
@ -47,13 +45,5 @@
"react-router": "^0.13.3",
"shmui": "^0.1.0",
"uglifyjs": "^2.4.10"
},
"jest": {
"scriptPreprocessor": "node_modules/babel-jest",
"unmockedModulePathPatterns": [
"<rootDir>/node_modules/react",
"<rootDir>/node_modules/alt",
"<rootDir>/js/alt.js"
]
}
}