all-contributors-cli/lib/contributors/add.test.js

144 lines
3.7 KiB
JavaScript
Raw Normal View History

2016-03-02 22:45:23 +00:00
import test from 'ava';
import addContributor from './add';
function mockInfoFetcher(username) {
return Promise.resolve({
2016-03-02 22:45:23 +00:00
login: username,
name: 'Some name',
avatar_url: 'www.avatar.url',
profile: 'www.profile.url'
2016-03-02 22:45:23 +00:00
});
}
function fixtures() {
const options = {
contributors: [{
login: 'login1',
name: 'Some name',
avatar_url: 'www.avatar.url',
profile: 'www.profile.url',
2016-03-02 22:45:23 +00:00
contributions: [
'code'
]
}, {
login: 'login2',
name: 'Some name',
avatar_url: 'www.avatar.url',
profile: 'www.profile.url',
2016-03-02 22:45:23 +00:00
contributions: [
2016-03-06 23:20:24 +00:00
{type: 'blog', url: 'www.blog.url/path'},
2016-03-02 22:45:23 +00:00
'code'
]
}]
};
return {options};
}
test('should callback with error if infoFetcher fails', t => {
2016-03-02 22:45:23 +00:00
const {options} = fixtures();
const username = 'login3';
const contributions = ['doc'];
function infoFetcher() {
return Promise.reject(new Error('infoFetcher error'));
2016-03-02 22:45:23 +00:00
}
return t.throws(
addContributor(options, username, contributions, infoFetcher),
'infoFetcher error'
);
2016-03-02 22:45:23 +00:00
});
test('should add new contributor at the end of the list of contributors', t => {
2016-03-02 22:45:23 +00:00
const {options} = fixtures();
const username = 'login3';
const contributions = ['doc'];
2016-03-02 22:45:23 +00:00
return addContributor(options, username, contributions, mockInfoFetcher)
.then(contributors => {
2016-03-02 22:45:23 +00:00
t.is(contributors.length, 3);
2016-04-07 22:22:11 +00:00
t.deepEqual(contributors[2], {
2016-03-02 22:45:23 +00:00
login: 'login3',
name: 'Some name',
avatar_url: 'www.avatar.url',
profile: 'www.profile.url',
2016-03-02 22:45:23 +00:00
contributions: [
'doc'
]
});
});
});
test('should add new contributor at the end of the list of contributors with a url link', t => {
2016-03-02 22:45:23 +00:00
const {options} = fixtures();
const username = 'login3';
const contributions = ['doc'];
2016-03-02 22:45:23 +00:00
options.url = 'www.foo.bar';
return addContributor(options, username, contributions, mockInfoFetcher)
.then(contributors => {
2016-03-02 22:45:23 +00:00
t.is(contributors.length, 3);
2016-04-07 22:22:11 +00:00
t.deepEqual(contributors[2], {
2016-03-02 22:45:23 +00:00
login: 'login3',
name: 'Some name',
avatar_url: 'www.avatar.url',
profile: 'www.profile.url',
2016-03-02 22:45:23 +00:00
contributions: [
2016-03-06 23:20:24 +00:00
{type: 'doc', url: 'www.foo.bar'}
2016-03-02 22:45:23 +00:00
]
});
});
});
test(`should not update an existing contributor's contributions where nothing has changed`, t => {
2016-03-02 22:45:23 +00:00
const {options} = fixtures();
const username = 'login2';
const contributions = ['blog', 'code'];
2016-03-02 22:45:23 +00:00
return addContributor(options, username, contributions, mockInfoFetcher)
.then(contributors => {
2016-04-07 22:22:11 +00:00
t.deepEqual(contributors, options.contributors);
2016-03-02 22:45:23 +00:00
});
});
test(`should update an existing contributor's contributions if a new type is added`, t => {
2016-03-02 22:45:23 +00:00
const {options} = fixtures();
const username = 'login1';
const contributions = ['bug'];
return addContributor(options, username, contributions, mockInfoFetcher)
.then(contributors => {
2016-03-02 22:45:23 +00:00
t.is(contributors.length, 2);
2016-04-07 22:22:11 +00:00
t.deepEqual(contributors[0], {
2016-03-02 22:45:23 +00:00
login: 'login1',
name: 'Some name',
avatar_url: 'www.avatar.url',
profile: 'www.profile.url',
2016-03-02 22:45:23 +00:00
contributions: [
'code',
'bug'
]
});
});
});
test(`should update an existing contributor's contributions if a new type is added with a link`, t => {
2016-03-02 22:45:23 +00:00
const {options} = fixtures();
const username = 'login1';
const contributions = ['bug'];
2016-03-02 22:45:23 +00:00
options.url = 'www.foo.bar';
return addContributor(options, username, contributions, mockInfoFetcher)
.then(contributors => {
2016-03-02 22:45:23 +00:00
t.is(contributors.length, 2);
2016-04-07 22:22:11 +00:00
t.deepEqual(contributors[0], {
2016-03-02 22:45:23 +00:00
login: 'login1',
name: 'Some name',
avatar_url: 'www.avatar.url',
profile: 'www.profile.url',
2016-03-02 22:45:23 +00:00
contributions: [
'code',
2016-03-06 23:20:24 +00:00
{type: 'bug', url: 'www.foo.bar'}
2016-03-02 22:45:23 +00:00
]
});
});
});