Meteor-Community-Packages / meteor-publication-collector

Test a Meteor publication by collecting its output.
https://atmospherejs.com/johanbrook/publication-collector
MIT License
33 stars 20 forks source link

publication returns [] but collection is undefined #5

Closed hoolymama closed 8 years ago

hoolymama commented 8 years ago

Hi,

I have a publication that returns a cursor. If the collection exists but the cursor has no documents, I would expect publication-collector have an empty array for that collection. Instead it is undefined.

Error: Cannot read property 'length' of undefined

If I add one document its fine.

Is this behavior expected?

Here's my publication:

Meteor.publish("pairsAsParticipant", function (participantId) {
  authorizeOwner.call(this, participantId);
  return Pairs.find( { $and:   [ {"inviteeId":{$ne:null}},  { $or: [ { inviterId: participantId }, { inviteeId: participantId } ] } ]  } );
});

and my test:

  describe('No Pairs', () => {
    beforeEach(function (){
      resetDatabase()
      // make 5 pairs
      const fiveRandomPairs = [...Array(5)].map( () => {
        return Factory.create('pair', { inviterId: Random.id(), inviteeId: Random.id()  })
      })
    })

    it('should find zero pairs if currentUser is not a participant', function() {
      const collector = new PublicationCollector({userId: currentUserId})
      collector.collect('pairsAsParticipant', currentUserId,  (collections) => {
        expect(collections.pairs).to.have.lengthOf(0)
      })
    })
  })
johanbrook commented 8 years ago

True, I agree. Investigating.

hoolymama commented 8 years ago

Hi there,

I'm sorry, I didn't realise I had hit "send" on this. I intended to investigate further, because maybe your code behaves the same as a Meteor subscription.

In the meantime I changed my test to this:

expect(collections.pairs).to.not.exist

johanbrook commented 8 years ago

Well, if you call .fetch() on a collection with no documents, it'll return an empty array, not undefined. I still think you had the correct reasoning at the top.

hoolymama commented 8 years ago

Yes true. (I was confused earlier). Thanks for fixing it!

johanbrook commented 8 years ago

Fixed in https://github.com/johanbrook/meteor-publication-collector/pull/7.

shelagh-lewins-ucl commented 5 years ago

Hi, I seem to be seeing this problem again. When my publish function returns nothing::

this.ready();
return;

collections.myCollection is undefined. I'd expect it to be an empty array.

I also tried

return [];

but the test still sees undefined.

Have I missed something or has this bug reappeared?

versions: johanbrook:publication-collector@1.1.0 METEOR@1.6.1

Thank you for this awesome package, it's really good.