Browse Source

Migrate tests to AVA

master
Luke Childs 9 years ago
parent
commit
c5d0281450
  1. 5
      package.json
  2. 144
      test/unit.js

5
package.json

@ -7,13 +7,12 @@
"my-name-is-url": "^1.3.1"
},
"devDependencies": {
"ava": "^0.14.0",
"babel-cli": "^6.7.5",
"babel-plugin-add-module-exports": "^0.1.2",
"babel-preset-es2015": "^6.6.0",
"chai": "^3.5.0",
"coveralls": "^2.11.9",
"eslint": "^2.8.0",
"mocha": "^2.4.5",
"nyc": "^6.4.2",
"pre-commit": "^1.1.2"
},
@ -23,7 +22,7 @@
"prebuild:map": "npm run prebuild",
"build:map": "babel --source-maps=true -d dist src",
"pretest": "npm run build:map",
"test": "nyc mocha test",
"test": "nyc ava test",
"lint": "eslint src",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"prepublish": "npm run build"

144
test/unit.js

@ -1,99 +1,79 @@
var expect = require('chai').expect;
import test from 'ava';
var AtHash = require('../dist/athash');
var Parser = require('../dist/parser');
var twitterFilter = require('../dist/filters/twitter');
import AtHash from '../dist/athash';
import Parser from '../dist/parser';
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() {
it('Should return instance of parser', function () {
expect(AtHash()).to.be.an.instanceof(Parser);
});
describe('.text()', function() {
it('Should set text property', function () {
var atHash = AtHash();
expect(atHash._text).to.equal(null);
atHash.text('foo');
expect(atHash._text).to.equal('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 () {
expect(AtHash(text).get('hashtags')).to.deep.equal(['#test', '#text', '#hashtags']);
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 () {
expect(function() { AtHash().get('nonexistentfilter') }).to.throw(Error);
});
test('AtHash() should return instance of parser', t => {
t.true(AtHash() instanceof Parser);
});
});
test('AtHash() can be instantiated by new or factory function', t => {
t.true(AtHash() instanceof Parser);
t.true(new AtHash instanceof Parser);
});
describe('.addFilter()', function() {
test('.text() should set text property', t => {
const atHash = AtHash();
t.is(atHash._text, null);
atHash.text('foo');
t.is(atHash._text, 'foo');
});
it('Should add default filters', function () {
var atHash = AtHash();
expect(atHash.filters.hashtags.filter).to.be.undefined;
atHash.addFilter('twitter');
expect(atHash.filters.hashtags.filter).to.equal(twitterFilter.hashtags.filter);
});
test('.text() should be chainable', t => {
t.true(AtHash().text() instanceof Parser);
});
it('Should add custom filters', function () {
var atHash = AtHash();
var customFilter = { hashtags: { filter: tag => tag } };
expect(atHash.filters.hashtags.filter).to.be.undefined;
atHash.addFilter(customFilter);
expect(atHash.filters.hashtags.filter).to.equal(customFilter.hashtags.filter);
});
test('.get() should return empty array on no matches', t => {
t.deepEqual(AtHash().get('hashtags'), []);
t.deepEqual(AtHash().get('mentions'), []);
t.deepEqual(AtHash().get('urls'), []);
});
it('Should throw error if filter is invalid', function () {
expect(function() { AtHash().addFilter('nonexistentfilter') }).to.throw(Error);
});
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']);
});
it('Should be chainable', function () {
expect(AtHash().addFilter({})).to.be.an.instanceof(Parser);
});
test('.get() should throw error if trying to use nonexistent filter', t => {
t.throws(() => AtHash().get('nonexistentfilter'));
});
});
test('.addFilter() should add default filters', t => {
t.throws(() => AtHash().get('nonexistentfilter'));
const atHash = AtHash();
t.is(atHash.filters.hashtags.filter, undefined);
atHash.addFilter('twitter');
t.is(atHash.filters.hashtags.filter, twitterFilter.hashtags.filter);
});
describe('.parse()', function() {
test('.addFilter() should add custom filters', t => {
const atHash = AtHash();
const customFilter = { hashtags: { filter: tag => tag } };
t.is(atHash.filters.hashtags.filter, undefined);
atHash.addFilter(customFilter);
t.is(atHash.filters.hashtags.filter, customFilter.hashtags.filter);
});
it('Should return empty string on instance with no text', function () {
expect(AtHash().parse()).to.equal('');
});
test('.addFilter() throw error if filter is invalid', t => {
t.throws(() => AtHash().addFilter('nonexistentfilter'));
});
it('Should return text on instance with text', function () {
expect(AtHash('foo').parse()).to.equal('foo');
});
test('.addFilter() should be chainable', t => {
t.true(AtHash().addFilter({}) instanceof Parser);
});
it('Should run url filter on url', function () {
expect(AtHash('http://url.com').parse()).to.equal('<a target="_blank" href="http://url.com">http://url.com</a>');
});
test('.parse() should return empty string on instance with no text', t => {
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…
Cancel
Save