Skip to content

Commit

Permalink
Add jest test (#20)
Browse files Browse the repository at this point in the history
Add unittest
  • Loading branch information
okoala authored Dec 13, 2017
1 parent 698e52e commit 4efa7ea
Show file tree
Hide file tree
Showing 9 changed files with 371 additions and 10 deletions.
13 changes: 7 additions & 6 deletions packages/avet-build/lib/babel/plugins/handle-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ function getModulePath(sourceFileName, moduleName) {
: moduleName;

const cleanedModulePath = modulePath
.replace(/(index){0, 1}\.js$/, '') // remove .js, index.js
.replace(/(index){0,1}\.js$/, '') // remove .js, index.js
.replace(/[/\\]$/, ''); // remove end slash

return cleanedModulePath;
}

module.exports = () => ({
module.exports = exports = () => ({
inherits: syntax,

visitor: {
Expand All @@ -72,10 +72,9 @@ module.exports = () => ({
''
);

const name = `${relativeModulePath.replace(
/[^\w]/g,
'_'
)}_${modulePathHash}`;
const name = `${relativeModulePath.replace(/[^\w]/g, '_')}_${
modulePathHash
}`;

const newImport = buildImport({
name,
Expand All @@ -87,3 +86,5 @@ module.exports = () => ({
},
},
});

exports.getModulePath = getModulePath;
12 changes: 10 additions & 2 deletions packages/avet-build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"description": "avet build",
"version": "1.0.0-12",
"scripts": {
"autod": "autod"
"autod": "autod",
"test": "jest \\.test.js"
},
"files": [
"lib",
Expand Down Expand Up @@ -59,7 +60,14 @@
"autod": "^2.10.1",
"babel-eslint": "^8.0.2",
"eslint": "^4.10.0",
"eslint-config-avet": "^0.3.6"
"eslint-config-avet": "^0.3.6",
"jest-cli": "^21.2.1"
},
"jest": {
"testEnvironment": "node",
"roots": [
"test/"
]
},
"tnpm": {
"mode": "npm"
Expand Down
37 changes: 37 additions & 0 deletions packages/avet-build/test/handle-import-babel-plugin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* global describe, it, expect */

import { getModulePath } from '../lib/babel/plugins/handle-import';

function cleanPath(mPath) {
return mPath.replace(/\\/g, '/').replace(/^.*:/, '');
}

describe('handle-import-babel-plugin', () => {
it('should not do anything to NPM modules', () => {
const mPath = getModulePath('/abc/pages/about.js', 'cool-module');
expect(mPath).toBe('cool-module');
});

it('should not do anything to private NPM modules', () => {
const mPath = getModulePath('/abc/pages/about.js', '@zeithq/cool-module');
expect(mPath).toBe('@zeithq/cool-module');
});

it('should resolve local modules', () => {
const mPath = getModulePath(
'/abc/pages/about.js',
'../components/hello.js'
);
expect(cleanPath(mPath)).toBe('/abc/components/hello');
});

it('should remove .js', () => {
const mPath = getModulePath('/abc/pages/about.js', '../components/bb.js');
expect(cleanPath(mPath)).toBe('/abc/components/bb');
});

it('should remove end slash', () => {
const mPath = getModulePath('/abc/pages/about.js', '../components/bb/');
expect(cleanPath(mPath)).toBe('/abc/components/bb');
});
});
8 changes: 7 additions & 1 deletion packages/avet-shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"version": "1.0.0-12",
"scripts": {
"dev": "taskr dev",
"build": "taskr release"
"build": "taskr release",
"test": "jest \\.test.js"
},
"dependencies": {
"ansi-html": "^0.0.7",
Expand Down Expand Up @@ -32,9 +33,14 @@
"babel-preset-react": "^6.24.1",
"eslint": "^4.10.0",
"eslint-config-avet": "^0.3.7",
"jest-cli": "^21.2.1",
"node-notifier": "^5.1.2",
"taskr": "^1.1.0"
},
"jest": {
"testEnvironment": "node",
"roots": ["test/"]
},
"tnpm": {
"mode": "npm"
},
Expand Down
134 changes: 134 additions & 0 deletions packages/avet-shared/test/dynamic.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/* global describe, it, expect */

import { SameLoopPromise } from '../lib/dynamic';

describe('SameLoopPromise', () => {
describe('basic api', () => {
it('should support basic promise resolving', done => {
const promise = new SameLoopPromise(resolve => {
setTimeout(() => {
resolve(1000);
}, 100);
});

promise.then(value => {
expect(value).toBe(1000);
done();
});
});

it('should support resolving in the same loop', () => {
let gotValue = null;
const promise = new SameLoopPromise(resolve => {
resolve(1000);
});

promise.then(value => {
gotValue = value;
});

expect(gotValue).toBe(1000);
});

it('should support basic promise rejecting', done => {
const error = new Error('Hello Error');
const promise = new SameLoopPromise((resolve, reject) => {
setTimeout(() => {
reject(error);
}, 100);
});

promise.catch(err => {
expect(err).toBe(error);
done();
});
});

it('should support rejecting in the same loop', () => {
const error = new Error('Hello Error');
let gotError = null;
const promise = new SameLoopPromise((resolve, reject) => {
reject(error);
});

promise.catch(err => {
gotError = err;
});

expect(gotError).toBe(error);
});
});

describe('complex usage', () => {
it('should support a chain of promises', done => {
const promise = new SameLoopPromise(resolve => {
setTimeout(() => {
resolve(1000);
}, 100);
});

promise
.then(value => value * 2)
.then(value => value + 10)
.then(value => {
expect(value).toBe(2010);
done();
});
});

it('should handle the error inside the then', done => {
const error = new Error('1000');
const promise = new SameLoopPromise((resolve, reject) => {
setTimeout(() => {
reject(error);
}, 100);
});

promise
.then(() => 4000, err => parseInt(err.message, 10))
.then(value => value + 10)
.then(value => {
expect(value).toBe(1010);
done();
});
});

it('should catch the error at the end', done => {
const error = new Error('1000');
const promise = new SameLoopPromise((resolve, reject) => {
setTimeout(() => {
reject(error);
}, 100);
});

promise
.then(value => value * 2)
.then(value => value + 10)
.catch(err => {
expect(err).toBe(error);
done();
});
});

it('should catch and proceed', done => {
const error = new Error('1000');
const promise = new SameLoopPromise((resolve, reject) => {
setTimeout(() => {
reject(error);
}, 100);
});

promise
.then(value => value * 2)
.then(value => value + 10)
.catch(err => {
expect(err).toBe(error);
return 5000;
})
.then(value => {
expect(value).toBe(5000);
done();
});
});
});
});
71 changes: 71 additions & 0 deletions packages/avet-shared/test/event-emitter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* global describe, it, expect */

import EventEmitter from '../lib/event-emiiter';

describe('EventEmiiter', () => {
describe('With listeners', () => {
it('should listen to a event', done => {
const event = new EventEmitter();
event.on('sample', done);
event.emit('sample');
});

it('should listen to multiple listeners', () => {
const event = new EventEmitter();
let count = 0;

event.on('sample', () => {
count += 1;
});
event.on('sample', () => {
count += 1;
});
event.emit('sample');
expect(count).toBe(2);
});

it('should support multiple arguments', () => {
const event = new EventEmitter();
let data;

event.on('sample', (...args) => {
data = args;
});
event.emit('sample', 'one', 'two');
expect(data).toEqual([ 'one', 'two' ]);
});

it('should possible to stop listening an event', () => {
const event = new EventEmitter();
let count = 0;

const cb = () => {
count += 1;
};

event.on('sample', cb);
event.emit('sample');
expect(count).toBe(1);

event.off('sample', cb);
event.emit('sample');
expect(count).toBe(1);
});

it('should throw when try to add the same listener multiple times', () => {
const event = new EventEmitter();
const cb = () => {};

event.on('sample', cb);
const run = () => event.on('sample', cb);
expect(run).toThrow(/The listener already exising in event: sample/);
});
});

describe('Without a listener', () => {
it('should not fail to emit', () => {
const event = new EventEmitter();
event.emit('aaaa', 10, 20);
});
});
});
Loading

0 comments on commit 4efa7ea

Please sign in to comment.