2016-02-29 21:08:23 +00:00
import test from 'ava' ;
import addContributor from './addContributor' ;
function getUserLine ( content , { login } ) {
return content
. split ( '\n' )
. filter ( line => line . indexOf ( login ) !== - 1 )
[ 0 ] ;
}
function fixtures ( ) {
const options = {
projectOwner : 'kentcdodds' ,
projectName : 'all-contributors' ,
imageSize : 100 ,
contributions : [ 'doc' ] ,
emoji : {
code : ':code:' ,
doc : ':doc:' ,
test : ':test:'
}
} ;
2016-02-29 22:32:46 +00:00
const jfmengelsUser = {
2016-02-29 21:08:23 +00:00
login : 'jfmengels' ,
name : 'Jeroen Engels' ,
html _url : 'https://github.com/jfmengels' ,
avatar _url : 'https://avatars.githubusercontent.com/u/3869412?v=3'
} ;
2016-02-29 22:32:46 +00:00
const kentcdoddsUser = {
login : 'kentcdodds' ,
name : 'Kent C. Dodds' ,
html _url : 'https://github.com/kentcdodds' ,
avatar _url : 'https://avatars.githubusercontent.com/u/1500684?v=3'
} ;
2016-02-29 21:08:23 +00:00
const content = `
# project
Description
# # Contributors
These people contributed to the project :
: -- - : | : -- - :
2016-02-29 22:32:46 +00:00
[ ! [ Kent C . Dodds ] ( https : //avatars.githubusercontent.com/u/1500684?v=3&s=100)<br />Kent C. Dodds](https://github.com/kentcdodds) | [:doc:](https://github.com/kentcdodds/all-contributors/commits?author=kentcdodds)
[ ! [ Divjot Singh ] ( https : //avatars1.githubusercontent.com/u/6177621?s=130)<br />Divjot Singh](http://bogas04.github.io) | [:doc:](https://github.com/kentcdodds/all-contributors/commits?author=bogas04)
2016-02-29 21:08:23 +00:00
Thanks a lot guys !
` ;
2016-02-29 22:32:46 +00:00
return { options , jfmengelsUser , kentcdoddsUser , content } ;
2016-02-29 21:08:23 +00:00
}
test ( 'should add a new contributor to the end of the list' , t => {
2016-02-29 22:32:46 +00:00
t . pass ( 1 ) ;
const { options , jfmengelsUser , content } = fixtures ( ) ;
2016-02-29 21:08:23 +00:00
const expected = `
# project
Description
# # Contributors
These people contributed to the project :
: -- - : | : -- - :
2016-02-29 22:32:46 +00:00
[ ! [ Kent C . Dodds ] ( https : //avatars.githubusercontent.com/u/1500684?v=3&s=100)<br />Kent C. Dodds](https://github.com/kentcdodds) | [:doc:](https://github.com/kentcdodds/all-contributors/commits?author=kentcdodds)
[ ! [ Divjot Singh ] ( https : //avatars1.githubusercontent.com/u/6177621?s=130)<br />Divjot Singh](http://bogas04.github.io) | [:doc:](https://github.com/kentcdodds/all-contributors/commits?author=bogas04)
2016-02-29 21:08:23 +00:00
[ ! [ Jeroen Engels ] ( https : //avatars.githubusercontent.com/u/3869412?v=3&s=100)<br />Jeroen Engels](https://github.com/jfmengels) | [:doc:](https://github.com/kentcdodds/all-contributors/commits?author=jfmengels)
Thanks a lot guys !
` ;
2016-02-29 22:32:46 +00:00
t . is ( addContributor ( options , jfmengelsUser , content ) , expected ) ;
2016-02-29 21:08:23 +00:00
} ) ;
test ( 'should be able to inject several contributions' , t => {
2016-02-29 22:32:46 +00:00
t . pass ( 1 ) ;
const { options , jfmengelsUser , content } = fixtures ( ) ;
2016-02-29 21:08:23 +00:00
options . contributions = [ 'doc' , 'code' ] ;
const expected = '[![Jeroen Engels](https://avatars.githubusercontent.com/u/3869412?v=3&s=100)<br />Jeroen Engels](https://github.com/jfmengels) | [:doc::code:](https://github.com/kentcdodds/all-contributors/commits?author=jfmengels)' ;
2016-02-29 22:32:46 +00:00
const result = addContributor ( options , jfmengelsUser , content ) ;
2016-02-29 21:08:23 +00:00
2016-02-29 22:32:46 +00:00
t . is ( getUserLine ( result , jfmengelsUser ) , expected ) ;
2016-02-29 21:08:23 +00:00
} ) ;
test ( 'should be able to specify a new template in options' , t => {
2016-02-29 22:32:46 +00:00
t . pass ( 1 ) ;
const { options , jfmengelsUser , content } = fixtures ( ) ;
2016-02-29 21:08:23 +00:00
options . template = '<%= user.login %> made awesome contributions!' ;
const expected = 'jfmengels made awesome contributions!' ;
2016-02-29 22:32:46 +00:00
const result = addContributor ( options , jfmengelsUser , content ) ;
t . is ( getUserLine ( result , jfmengelsUser ) , expected ) ;
} ) ;
test ( 'should not modify content when adding an existing contributor that has the given contribution types' , t => {
t . pass ( 1 ) ;
const { options , kentcdoddsUser , content } = fixtures ( ) ;
const expected = content ;
const result = addContributor ( options , kentcdoddsUser , content ) ;
t . is ( result , expected ) ;
} ) ;
test ( 'should add new contributions types to an existing contributor' , t => {
t . pass ( 1 ) ;
const { options , kentcdoddsUser , content } = fixtures ( ) ;
options . contributions = [ 'doc' , 'code' ] ;
const expected = '[![Kent C. Dodds](https://avatars.githubusercontent.com/u/1500684?v=3&s=100)<br />Kent C. Dodds](https://github.com/kentcdodds) | [:doc::code:](https://github.com/kentcdodds/all-contributors/commits?author=kentcdodds)' ;
const result = addContributor ( options , kentcdoddsUser , content ) ;
2016-02-29 21:08:23 +00:00
2016-02-29 22:32:46 +00:00
t . is ( getUserLine ( result , kentcdoddsUser ) , expected ) ;
2016-02-29 21:08:23 +00:00
} ) ;