Skip to content

Commit

Permalink
Cleanup and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jabbany committed Oct 22, 2016
1 parent 6a2bd3a commit 6874e37
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 138 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ gem 'rails-assets-comment-core-library'

## Examples and Documentation
- [Documentation](docs/) can be found inside the `docs/` folder.
- Some sample extension modules may be found in `src/extend/`.
- Experimental modules are in `experimental/`.
- You may test using test data found in `test/`.

Expand Down Expand Up @@ -77,7 +76,6 @@ implementation of a video player with CommentCoreLibrary.

## 使用
- 有关本项目的[文档](docs/) 可以在 `docs/` 文件夹里面找到。
- 一些功能性扩展模块会出现在 `src/extend/` 中。
- 一些实验性模块在 `experimental/` 里。
- 测试数据在 `test/` 里。

Expand Down
6 changes: 3 additions & 3 deletions dist/CommentCoreLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ var CommentProvider = (function () {

xhr.onerror = function () {
reject(new Error(this.status + " " + this.statusText));
}
};

xhr.open(method, uri);
if (typeof body !== 'undefined') {
Expand Down Expand Up @@ -1119,7 +1119,7 @@ var CommentProvider = (function () {
CommentProvider.TextProvider = function (method, url, args, body) {
return CommentProvider.BaseHttpProvider(
method, url, "text", args, body).then(function (response) {
return response.text;
return response;
});
};

Expand Down Expand Up @@ -1211,7 +1211,7 @@ var CommentProvider = (function () {
return new Promise(function (resolve, reject) {
if (!(type in this._parsers)) {
reject(new Error('No parsers defined for "' + type + '"'));
return;s
return;
}
for (var i = 0; i < this._parsers[type].length; i++) {
var output = null;
Expand Down
2 changes: 1 addition & 1 deletion dist/CommentCoreLibrary.min.js

Large diffs are not rendered by default.

66 changes: 66 additions & 0 deletions spec/CommentProvider_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,62 @@ describe 'CommentProvider', ->
provider.destroy()
expect( => provider.addTarget(commentManager)).toThrow()

describe '.applyParsersOne', ->
rejectingParser =
parseOne: () -> null
parseMany: () -> null
resolvingParser =
parseOne: (t) -> t
parseMany: (t) -> t

it 'rejects if no parsers for type', (done) ->
promise = provider.applyParsersOne {}, CommentProvider.SOURCE_JSON
promise.catch (e) ->
expect(e instanceof Error).toBe true
done()

it 'rejects if no parser accepts', (done) ->
provider.addParser rejectingParser, CommentProvider.SOURCE_JSON
promise = provider.applyParsersOne {}, CommentProvider.SOURCE_JSON
promise.catch (e) ->
expect(e instanceof Error).toBe true
done()

it 'accepts if parser accepts', (done) ->
provider.addParser resolvingParser, CommentProvider.SOURCE_JSON
promise = provider.applyParsersOne {}, CommentProvider.SOURCE_JSON
promise.then (item) ->
expect(item).toEqual {}
done()

describe '.applyParsersList', ->
rejectingParser =
parseOne: () -> null
parseMany: () -> null
resolvingParser =
parseOne: (t) -> t
parseMany: (t) -> t

it 'rejects if no parsers for type', (done) ->
promise = provider.applyParsersList [], CommentProvider.SOURCE_JSON
promise.catch (e) ->
expect(e instanceof Error).toBe true
done()

it 'rejects if no parser accepts', (done) ->
provider.addParser rejectingParser, CommentProvider.SOURCE_JSON
promise = provider.applyParsersList [], CommentProvider.SOURCE_JSON
promise.catch (e) ->
expect(e instanceof Error).toBe true
done()

it 'accepts if parser accepts', (done) ->
provider.addParser resolvingParser, CommentProvider.SOURCE_JSON
promise = provider.applyParsersList [], CommentProvider.SOURCE_JSON
promise.then (items) ->
expect(items).toEqual []
done()

describe '.load', ->
it 'requests static sources', (done) ->
provider.addStaticSource (Promise.resolve 'Foo'), CommentProvider.SOURCE_TEXT
Expand Down Expand Up @@ -139,6 +195,11 @@ describe 'CommentProvider', ->
addEventListener: () ->
spy = sinon.spy dynamicSource, "addEventListener"

it 'fails if called on destroyed object', ->
p = new CommentProvider()
p.destroy()
expect( => p.start()).toThrow()

it 'calls load', ->
loadspy = sinon.spy provider, 'load'
provider.start()
Expand All @@ -161,3 +222,8 @@ describe 'CommentProvider', ->
it 'sets destroyed flag', ->
provider.destroy()
expect(provider._destroyed).toBe true

it 'destroy can be called multiple times', ->
provider.destroy()
provider.destroy()
expect(provider._destroyed).toBe true
6 changes: 3 additions & 3 deletions src/CommentProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ var CommentProvider = (function () {

xhr.onerror = function () {
reject(new Error(this.status + " " + this.statusText));
}
};

xhr.open(method, uri);
if (typeof body !== 'undefined') {
Expand Down Expand Up @@ -121,7 +121,7 @@ var CommentProvider = (function () {
CommentProvider.TextProvider = function (method, url, args, body) {
return CommentProvider.BaseHttpProvider(
method, url, "text", args, body).then(function (response) {
return response.text;
return response;
});
};

Expand Down Expand Up @@ -213,7 +213,7 @@ var CommentProvider = (function () {
return new Promise(function (resolve, reject) {
if (!(type in this._parsers)) {
reject(new Error('No parsers defined for "' + type + '"'));
return;s
return;
}
for (var i = 0; i < this._parsers[type].length; i++) {
var output = null;
Expand Down
29 changes: 29 additions & 0 deletions src/core/CommentFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Comment Factory Abstraction
*
* @author Jim Chen
* @license MIT License
* @description Factory to allow creation of different kinds of comments with
* different underlying abstractions.
*/
/// <reference path="Core.d.ts" />
interface ICommentFactory {
(comment:Object):IComment;
}
class CommentFactory {
private _bindings:{[key:number]:ICommentFactory;} = {};

public bind (mode:number, factory:ICommentFactory):void {
this._bindings[mode] = factory;
}

public create (comment:Object):IComment {
if (comment === null || !comment.hasOwnProperty('mode')) {
throw new Error('Comment format incorrect');
}
if (!this._bindings.hasOwnProperty(comment.mode)) {
throw new Error('No binding for comment type ' + comment.mode);
}
return this._bindings[comment.mode](comment);
}
}
Loading

0 comments on commit 6874e37

Please sign in to comment.