mirror of https://github.com/lukechilds/AtHash.git
Luke Childs
9 years ago
2 changed files with 64 additions and 85 deletions
@ -1,99 +1,79 @@ |
|||||
var expect = require('chai').expect; |
import test from 'ava'; |
||||
|
|
||||
var AtHash = require('../dist/athash'); |
import AtHash from '../dist/athash'; |
||||
var Parser = require('../dist/parser'); |
import Parser from '../dist/parser'; |
||||
var twitterFilter = require('../dist/filters/twitter'); |
import twitterFilter from '../dist/filters/twitter'; |
||||
|
|
||||
var text = "#test #text with #hashtags, @multiple @mentions and a http://url.com"; |
const text = "#test #text with #hashtags, @multiple @mentions and a http://url.com"; |
||||
|
|
||||
describe('AtHash()', function() { |
test('AtHash() should return instance of parser', t => { |
||||
|
t.true(AtHash() instanceof Parser); |
||||
it('Should return instance of parser', function () { |
}); |
||||
expect(AtHash()).to.be.an.instanceof(Parser); |
|
||||
}); |
|
||||
|
|
||||
describe('.text()', function() { |
test('AtHash() can be instantiated by new or factory function', t => { |
||||
|
t.true(AtHash() instanceof Parser); |
||||
|
t.true(new AtHash instanceof Parser); |
||||
|
}); |
||||
|
|
||||
it('Should set text property', function () { |
test('.text() should set text property', t => { |
||||
var atHash = AtHash(); |
const atHash = AtHash(); |
||||
expect(atHash._text).to.equal(null); |
t.is(atHash._text, null); |
||||
atHash.text('foo'); |
atHash.text('foo'); |
||||
expect(atHash._text).to.equal('foo'); |
t.is(atHash._text, 'foo'); |
||||
}); |
}); |
||||
|
|
||||
it('Should be chainable', function () { |
|
||||
expect(AtHash().text()).to.be.an.instanceof(Parser); |
|
||||
}); |
|
||||
|
|
||||
}); |
|
||||
|
|
||||
describe('.get()', function() { |
|
||||
|
|
||||
it('Should run with no text', function () { |
|
||||
expect(AtHash().get('hashtags')).to.deep.equal([]); |
|
||||
expect(AtHash().get('mentions')).to.deep.equal([]); |
|
||||
expect(AtHash().get('urls')).to.deep.equal([]); |
|
||||
}); |
|
||||
|
|
||||
it('Should return empty array on no matches', function () { |
|
||||
expect(AtHash('').get('hashtags')).to.deep.equal([]); |
|
||||
expect(AtHash('').get('mentions')).to.deep.equal([]); |
|
||||
expect(AtHash('').get('urls')).to.deep.equal([]); |
|
||||
}); |
|
||||
|
|
||||
it('Should get array from matches', function () { |
test('.text() should be chainable', t => { |
||||
expect(AtHash(text).get('hashtags')).to.deep.equal(['#test', '#text', '#hashtags']); |
t.true(AtHash().text() instanceof Parser); |
||||
expect(AtHash(text).get('mentions')).to.deep.equal(['@multiple', '@mentions']); |
}); |
||||
expect(AtHash(text).get('urls')).to.deep.equal(['http://url.com']); |
|
||||
}); |
|
||||
|
|
||||
it('Should throw error if trying to use nonexistent filter', function () { |
test('.get() should return empty array on no matches', t => { |
||||
expect(function() { AtHash().get('nonexistentfilter') }).to.throw(Error); |
t.deepEqual(AtHash().get('hashtags'), []); |
||||
}); |
t.deepEqual(AtHash().get('mentions'), []); |
||||
|
t.deepEqual(AtHash().get('urls'), []); |
||||
|
}); |
||||
|
|
||||
}); |
test('.get() should get array from matches', t => { |
||||
|
t.deepEqual(AtHash(text).get('hashtags'), ['#test', '#text', '#hashtags']); |
||||
|
t.deepEqual(AtHash(text).get('mentions'), ['@multiple', '@mentions']); |
||||
|
t.deepEqual(AtHash(text).get('urls'), ['http://url.com']); |
||||
|
}); |
||||
|
|
||||
describe('.addFilter()', function() { |
test('.get() should throw error if trying to use nonexistent filter', t => { |
||||
|
t.throws(() => AtHash().get('nonexistentfilter')); |
||||
|
}); |
||||
|
|
||||
it('Should add default filters', function () { |
test('.addFilter() should add default filters', t => { |
||||
var atHash = AtHash(); |
t.throws(() => AtHash().get('nonexistentfilter')); |
||||
expect(atHash.filters.hashtags.filter).to.be.undefined; |
const atHash = AtHash(); |
||||
|
t.is(atHash.filters.hashtags.filter, undefined); |
||||
atHash.addFilter('twitter'); |
atHash.addFilter('twitter'); |
||||
expect(atHash.filters.hashtags.filter).to.equal(twitterFilter.hashtags.filter); |
t.is(atHash.filters.hashtags.filter, twitterFilter.hashtags.filter); |
||||
}); |
}); |
||||
|
|
||||
it('Should add custom filters', function () { |
test('.addFilter() should add custom filters', t => { |
||||
var atHash = AtHash(); |
const atHash = AtHash(); |
||||
var customFilter = { hashtags: { filter: tag => tag } }; |
const customFilter = { hashtags: { filter: tag => tag } }; |
||||
expect(atHash.filters.hashtags.filter).to.be.undefined; |
t.is(atHash.filters.hashtags.filter, undefined); |
||||
atHash.addFilter(customFilter); |
atHash.addFilter(customFilter); |
||||
expect(atHash.filters.hashtags.filter).to.equal(customFilter.hashtags.filter); |
t.is(atHash.filters.hashtags.filter, customFilter.hashtags.filter); |
||||
}); |
}); |
||||
|
|
||||
it('Should throw error if filter is invalid', function () { |
|
||||
expect(function() { AtHash().addFilter('nonexistentfilter') }).to.throw(Error); |
|
||||
}); |
|
||||
|
|
||||
it('Should be chainable', function () { |
|
||||
expect(AtHash().addFilter({})).to.be.an.instanceof(Parser); |
|
||||
}); |
|
||||
|
|
||||
}); |
|
||||
|
|
||||
describe('.parse()', function() { |
|
||||
|
|
||||
it('Should return empty string on instance with no text', function () { |
test('.addFilter() throw error if filter is invalid', t => { |
||||
expect(AtHash().parse()).to.equal(''); |
t.throws(() => AtHash().addFilter('nonexistentfilter')); |
||||
}); |
}); |
||||
|
|
||||
it('Should return text on instance with text', function () { |
test('.addFilter() should be chainable', t => { |
||||
expect(AtHash('foo').parse()).to.equal('foo'); |
t.true(AtHash().addFilter({}) instanceof Parser); |
||||
}); |
}); |
||||
|
|
||||
it('Should run url filter on url', function () { |
test('.parse() should return empty string on instance with no text', t => { |
||||
expect(AtHash('http://url.com').parse()).to.equal('<a target="_blank" href="http://url.com">http://url.com</a>'); |
t.is(AtHash().parse(), ''); |
||||
}); |
}); |
||||
|
|
||||
}); |
test('.parse() should return text on instance with text', t => { |
||||
|
t.is(AtHash('foo').parse(), 'foo'); |
||||
|
}); |
||||
|
|
||||
|
test('.parse() should run url filter on url', t => { |
||||
|
t.is(AtHash('http://url.com').parse(), '<a target="_blank" href="http://url.com">http://url.com</a>'); |
||||
}); |
}); |
||||
|
Loading…
Reference in new issue