-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unittest
- Loading branch information
Showing
9 changed files
with
371 additions
and
10 deletions.
There are no files selected for viewing
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
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
37 changes: 37 additions & 0 deletions
37
packages/avet-build/test/handle-import-babel-plugin.test.js
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
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'); | ||
}); | ||
}); |
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
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
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(); | ||
}); | ||
}); | ||
}); | ||
}); |
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
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); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.