Skip to content
This repository has been archived by the owner on Oct 24, 2021. It is now read-only.

Suggestion for better approach to StubCollection when importing collection models #595

Closed
abcd-ca opened this issue Dec 29, 2016 · 1 comment

Comments

@abcd-ca
Copy link

abcd-ca commented Dec 29, 2016

I went through a problem when migrating my code into ES6 modules and found a solution that worked for me I'd like to propose for the guide:

I found StubCollection wasn't ideal when creating client-side tests. Problems:

  • I have a project where in the client-server ES6 module that defines the collection, we attach a simpleSchema and some other decorated items.
  • Those decorators don't get attached run if you create a client-side null collection
  • Other ES6 modules import the non-null version of the model even if your test doesn't because models are no longer global once they're placed under /imports
  • StubCollection is just one more piece of overhead that I found I could get rid of once I came up with an alternative solution.

Given a directory structure like this,

/imports/startup/clientAndServer/model/
/imports/startup/client/other/
/imports/startup/server/collections/
/meteorTests/integration/client/

My client-server collections are defined in model, publications are defined in collections and client-side tests are in client. When I had a Race model defined in ...model/Race.js which is defined as export default Race = new Meteor.Collection('Race'), StubCollection would create a null collection which wasn't getting decorated and a module in other might import a non-null version.

Instead of using CollectionStubber at all, I can define the collection like this in ...model/Race.js:

let Race;
if (Meteor.isTest && Meteor.isClient){
	Race = new Meteor.Collection(null);
}else{
	Race = new Meteor.Collection('Race');
}
export default Race;

//decorations begin here, whether its' a null or non-null collection
Race.schema = new SimpleSchema({
    organization_id: {type: String},
    racer_id: {type: String}
});

Race.attachSchema(Race.schema);

...
@filipenevola
Copy link
Collaborator

I'm closing this issue because it's too old.

We are going to make many updates to the guide in the next weeks and it's better to focus on issues with recent activity. If you think this issue is still relevant please open a new one.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants