Skip to content

Commit

Permalink
Added some more tests - mainly for for the templating
Browse files Browse the repository at this point in the history
  • Loading branch information
iamphill committed Feb 24, 2015
1 parent fbb4616 commit df89cdb
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
9 changes: 9 additions & 0 deletions dist/Tweetsie.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,11 @@ var Tweetsie = (function () {
var regex = new RegExp(/{{(.*?)}}/g);
var matches = temp.match(regex);

if (matches === null) {
this.error("No variables entered into the template");
return;
}

// Loop all tweets
this.tweets.forEach(function (tweet) {
var tweethtml = "<div data-tweetsie-id=\"" + tweet.id + "\">" + temp + "</div>";
Expand Down Expand Up @@ -489,6 +494,10 @@ var Tweetsie = (function () {
// Get container
var container = this.getContainer();
container.innerHTML = html;

if (this.opts.callback !== undefined) {
this.opts.callback(this.tweets);
}
},
writable: true,
configurable: true
Expand Down
9 changes: 9 additions & 0 deletions lib/Tweetsie.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ class Tweetsie {
let regex = new RegExp(/{{(.*?)}}/g);
let matches = temp.match(regex);

if (matches === null) {
this.error('No variables entered into the template');
return;
}

// Loop all tweets
this.tweets.forEach((tweet) => {
let tweethtml = `<div data-tweetsie-id="${tweet.id}">${temp}</div>`;
Expand Down Expand Up @@ -362,5 +367,9 @@ class Tweetsie {
// Get container
let container = this.getContainer();
container.innerHTML = html;

if (this.opts.callback !== undefined) {
this.opts.callback(this.tweets);
}
}
}
1 change: 1 addition & 0 deletions tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<div id="tweetsie-container"></div>
<script src="js/qunit.js"></script>
<script src="../dist/Tweetsie.js"></script>
<script src="test.js"></script>
Expand Down
53 changes: 53 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
var widgetid = '570149068425674752';
var WIDGETID_ERROR_MSG = 'Must pass in a `widgetid`';
var NOT_FOUND_MSG = 'Twitter responded with: Widget configuration is not found.';
var CONTAINER_ID = 'tweetsie-container';
var NO_CONTAINER = 'No container passed to Tweetsie';
var TEMPLATE_EMPTY_MATCHES = 'No variables entered into the template';

test('Tweetsie creation', function (assert) {
var done = assert.async();
Expand Down Expand Up @@ -78,4 +81,54 @@
}
});
});

test('Tweet template with empty template', function (assert) {
var done = assert.async();

new Tweetsie({
widgetid: widgetid,
count: 1,
template: '',
error: function (msg) {
assert.equal(msg, TEMPLATE_EMPTY_MATCHES, 'Template matches found?!');
done();
}
});
});

test('Tweet template without container', function (assert) {
var done = assert.async();

new Tweetsie({
widgetid: widgetid,
count: 1,
template: '{{ body }}',
error: function (msg) {
assert.equal(msg, NO_CONTAINER, 'Somehow found a container?');
done();
}
});
});

test('Tweet template with container', function (assert) {
var done = assert.async();
var i = 0;

new Tweetsie({
container: 'tweetsie-container',
widgetid: widgetid,
count: 1,
template: '<p>{{ body }}</p>',
callback: function (tweets) {
if (i === 0) {
setTimeout(function () {
var el = document.getElementById(CONTAINER_ID);
assert.equal(el.getElementsByTagName('p').length, 1, 'Template didn\'t render right!');
done();
}, 200);
}
i++;
}
});
});
})();

0 comments on commit df89cdb

Please sign in to comment.