Replace karma with jsdom + mocha + chai

* Also a bit refactoring for better testability
This commit is contained in:
Riku Rouvila
2015-07-08 20:23:48 +03:00
parent 361f18115b
commit 8ed68ff60d
8 changed files with 87 additions and 104 deletions

View File

@@ -1,33 +1,10 @@
'use strict';
import {partialRight, invoke} from 'lodash';
import {getCommits, getRepo} from './services/github';
import {render} from './utils/renderer';
Promise.all([
fetch('https://api.github.com/repos/leonidas/gulp-project-template'),
fetch('https://api.github.com/repos/leonidas/gulp-project-template/commits')
])
.then(partialRight(invoke, 'json'))
.then(Promise.all.bind(Promise))
.then(data => {
const [repository, commits] = data;
const {html_url, description, full_name} = repository;
const commitItems = commits.map(item => {
const {commit} = item;
return `
<li>
<span>${commit.message.replace(/\n/g, '<br />')}<span>
<br /><br />
<small>${commit.committer.name}</small>
</li>`;
});
document.body.innerHTML = `
<h1>${description}</h1>
<h2><a href="${html_url}">${full_name}</a></h2>
<ul>${commitItems.join('')}</ul>
`;
Promise.all([getRepo(), getCommits()])
.then(([repository, commits]) => {
document.body.innerHTML = render(repository, commits);
});

13
src/js/services/github.js Normal file
View File

@@ -0,0 +1,13 @@
const REPO_URL = 'https://api.github.com/repos/leonidas/gulp-project-template';
export function getRepo() {
return fetch(REPO_URL).then(res => res.json());
}
export function getCommits() {
return fetch(`${REPO_URL}/commits`)
.then(res => res.json())
.then(commits => {
return commits.map(({commit}) => commit);
});
}

View File

@@ -0,0 +1,37 @@
/* globals beforeEach, describe, it */
import {render} from '../renderer';
import {expect} from 'chai';
const REPO_DATA = {
html_url: 'http://example.com',
full_name: 'Gulp project template',
description: 'Hello world!'
};
const COMMITS = [{
message: 'initial commit',
committer: {
name: 'Riku'
}
}, {
message: 'final commit',
committer: {
name: 'L.H.Ahne'
}
}];
describe('View renderer', function() {
beforeEach(function() {
document.body.innerHTML = render(REPO_DATA, COMMITS);
});
it('should render a title with the string "Hello world!"', function() {
const $title = document.getElementsByTagName('h1')[0];
expect($title.innerHTML).to.equal('Hello world!');
});
it('should render 2 list items', function() {
const $listItems = document.querySelectorAll('li');
expect($listItems.length).to.equal(2);
});
});

20
src/js/utils/renderer.js Normal file
View File

@@ -0,0 +1,20 @@
export function render(repository, commits) {
const $commits = commits.map(commit => {
return `
<li>
<span>${commit.message.replace(/\n/g, '<br />')}<span>
<br /><br />
<small>${commit.committer.name}</small>
</li>`;
});
return `
<h1>${repository.description}</h1>
<h2>
<a href="${repository.html_url}">${repository.full_name}</a>
</h2>
<ul>${$commits.join('')}</ul>
`;
}