Skip to content

Instantly share code, notes, and snippets.

@ameensol
Created March 16, 2015 01:34
Show Gist options
  • Save ameensol/e2368bea84c98006f70c to your computer and use it in GitHub Desktop.
Save ameensol/e2368bea84c98006f70c to your computer and use it in GitHub Desktop.
var chai = require('chai');
var assert = chai.assert;
var sinon = require('sinon');
var sinonChai = require('sinon-chai');
var Fixture = require('sequelize-fixtures')
var isStream = require('isstream')
var streamify = require('stream-array')
chai.use(sinonChai);
chai.should();
var Twit = require('twit')
var Twitter = require('../../twitter')
var ListMembers = require('../../streams/listMembers')
// helper function to get Ids
var getIds = function (arr) {
return arr.map(function (e) {
return parseInt(e.id)
})
}
module.exports = function (sequelize, models, auths) {
var List = models.List
var Tweeter = models.Tweeter
var listMembers = new ListMembers({}, models)
describe.only('listMembers', function () {
// authorities 1, 2, and 3 start in DB
// authorities 1, 2 start associated to List
// authorities 2, 3, 4 are returned by the API
// This results in:
// remove 1
// do nothing with 2
// find + associate 3
// create + associate 4
var get
var list1 = {id: 1}
var authority1 = {id: 22, AuthorityPosition: [1]}
var authority2 = {id: 33, AuthorityPosition: [1]}
var authority3 = {id: 44}
var authority4 = {id: 55}
var apiResponse = {
users: [
authority2,
authority3,
authority4
]
}
var mockLists
var mockList
var fixtures = [
{model: 'List', data: list1},
{model: 'Tweeter', data: authority1},
{model: 'Tweeter', data: authority2},
{model: 'Tweeter', data: authority3}
]
beforeEach(function (done) {
get = sinon.stub(Twit.prototype, 'get')
sequelize.sync({force: true}).then(function () {
Fixture.loadFixtures(fixtures, models).then(function () {
List.findAll({ include: [
{model: Tweeter, as: 'Authorities'}
]}).then(function (savedLists) {
mockLists = savedLists
mockList = savedLists[0]
done()
})
})
})
})
afterEach(function () {
get.restore()
})
it('should be a stream', function () {
assert(isStream(listMembers))
})
it('should sync a lists members to the api results', function (done) {
get.onCall(0).callsArgWith(2, null, apiResponse, {})
mockList.auths = auths
streamify(mockLists).pipe(listMembers)
listMembers.once('readable', function () {
mockList.getAuthorities().then(function (savedAuthorities) {
var finalAuthIds = getIds(savedAuthorities)
var apiAuthIds = getIds(apiResponse.users)
assert.sameMembers(finalAuthIds, apiAuthIds)
done()
})
})
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment