-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from dburles/meteor-3
Meteor 2.3 and 3 support
- Loading branch information
Showing
5 changed files
with
190 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
name: Tests | ||
on: [push, pull_request] | ||
jobs: | ||
tests: | ||
name: tests | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
meteor-release: | ||
- "--release 2.8.2" | ||
- "--release 3.1" | ||
steps: | ||
- name: checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Install Dependencies | ||
run: | | ||
curl https://install.meteor.com | /bin/sh | ||
npm i -g @zodern/mtest | ||
- name: Run Tests | ||
run: | | ||
# Fix using old versions of Meteor | ||
export NODE_TLS_REJECT_UNAUTHORIZED=0 | ||
mtest --package ./ --once ${{ matrix.meteorRelease }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,12 @@ | ||
Mongo.Collection.prototype.helpers = function(helpers) { | ||
var self = this; | ||
|
||
if (self._transform && ! self._helpers) | ||
if (this._transform && !this._helpers) | ||
throw new Meteor.Error("Can't apply helpers to '" + | ||
self._name + "' a transform function already exists!"); | ||
this._name + "' a transform function already exists!"); | ||
|
||
if (! self._helpers) { | ||
self._helpers = function Document(doc) { return _.extend(this, doc); }; | ||
self._transform = function(doc) { | ||
return new self._helpers(doc); | ||
}; | ||
if (!this._helpers) { | ||
this._helpers = function Document(doc) { return Object.assign(this, doc); }; | ||
this._transform = doc => new this._helpers(doc); | ||
} | ||
|
||
_.each(helpers, function(helper, key) { | ||
self._helpers.prototype[key] = helper; | ||
}); | ||
Object.keys(helpers).forEach(key => (this._helpers.prototype[key] = helpers[key])); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,146 @@ | ||
Tinytest.add("works", function(test) { | ||
Books = new Mongo.Collection('books' + test.id); | ||
Authors = new Mongo.Collection('authors' + test.id); | ||
const meteorVersion = Meteor.release | ||
? parseFloat(Meteor.release.split("@")[1]) | ||
: null; | ||
|
||
var author1 = Authors.insert({ | ||
firstName: 'Charles', | ||
lastName: 'Darwin' | ||
}); | ||
if (meteorVersion && meteorVersion >= 3) { | ||
Tinytest.addAsync("works", async function (test) { | ||
const Books = new Mongo.Collection( | ||
Meteor.isClient ? null : "books" + test.id | ||
); | ||
const Authors = new Mongo.Collection( | ||
Meteor.isClient ? null : "authors" + test.id | ||
); | ||
|
||
var author2 = Authors.insert({ | ||
firstName: 'Carl', | ||
lastName: 'Sagan' | ||
}); | ||
const author1 = await Authors.insertAsync({ | ||
firstName: "Charles", | ||
lastName: "Darwin", | ||
}); | ||
|
||
var book1 = Books.insert({ | ||
authorId: author1, | ||
name: 'On the Origin of Species' | ||
}); | ||
const author2 = await Authors.insertAsync({ | ||
firstName: "Carl", | ||
lastName: "Sagan", | ||
}); | ||
|
||
var book2 = Books.insert({ | ||
authorId: author2, | ||
name: 'Contact' | ||
}); | ||
const book1 = await Books.insertAsync({ | ||
authorId: author1, | ||
name: "On the Origin of Species", | ||
}); | ||
|
||
Books.helpers({ | ||
author: function() { | ||
return Authors.findOne(this.authorId); | ||
} | ||
}); | ||
const book2 = await Books.insertAsync({ | ||
authorId: author2, | ||
name: "Contact", | ||
}); | ||
|
||
// We should be able to apply more if we wish | ||
Books.helpers({ | ||
foo: 'bar' | ||
}); | ||
Books.helpers({ | ||
author() { | ||
return Authors.findOneAsync(this.authorId); | ||
}, | ||
}); | ||
|
||
// We should be able to apply more if we wish | ||
Books.helpers({ | ||
foo: "bar", | ||
}); | ||
|
||
Authors.helpers({ | ||
fullName() { | ||
return this.firstName + " " + this.lastName; | ||
}, | ||
books() { | ||
return Books.find({ authorId: this._id }); | ||
}, | ||
}); | ||
|
||
const book = await Books.findOneAsync(book1); | ||
const author = await book.author(); | ||
|
||
Authors.helpers({ | ||
fullName: function() { | ||
return this.firstName + ' ' + this.lastName; | ||
}, | ||
books: function() { | ||
return Books.find({ authorId: this._id }); | ||
} | ||
test.equal(author.firstName, "Charles"); | ||
test.equal(author.fullName(), "Charles Darwin"); | ||
test.equal(book.foo, "bar"); | ||
|
||
const authorCarl = await Authors.findOneAsync(author2); | ||
const booksByCarl = await authorCarl.books().fetchAsync(); | ||
|
||
test.equal(booksByCarl.length, 1); | ||
}); | ||
} else { | ||
// Meteor < 3.0 | ||
Tinytest.add("works", function (test) { | ||
const Books = new Mongo.Collection("books" + test.id); | ||
const Authors = new Mongo.Collection("authors" + test.id); | ||
|
||
var book = Books.findOne(book1); | ||
var author = book.author(); | ||
test.equal(author.firstName, 'Charles'); | ||
test.equal(book.foo, 'bar'); | ||
const author1 = Authors.insert({ | ||
firstName: "Charles", | ||
lastName: "Darwin", | ||
}); | ||
|
||
book = Books.findOne(book2); | ||
author = book.author(); | ||
test.equal(author.fullName(), 'Carl Sagan'); | ||
const author2 = Authors.insert({ | ||
firstName: "Carl", | ||
lastName: "Sagan", | ||
}); | ||
|
||
author = Authors.findOne(author1); | ||
books = author.books(); | ||
test.equal(books.count(), 1); | ||
}); | ||
const book1 = Books.insert({ | ||
authorId: author1, | ||
name: "On the Origin of Species", | ||
}); | ||
|
||
Tinytest.add("throw error if transform function already exists", function(test) { | ||
Author = function(doc) { return _.extend(this, doc); }; | ||
const book2 = Books.insert({ | ||
authorId: author2, | ||
name: "Contact", | ||
}); | ||
|
||
Author.prototype.fullName = 'Charles Darwin'; | ||
Books.helpers({ | ||
author: function () { | ||
return Authors.findOne(this.authorId); | ||
}, | ||
}); | ||
|
||
Authors = new Meteor.Collection('authors' + test.id, { | ||
transform: function(doc) { return new Author(doc); }}); | ||
// We should be able to apply more if we wish | ||
Books.helpers({ | ||
foo: "bar", | ||
}); | ||
|
||
test.throws(function() { | ||
Authors.helpers({ | ||
fullName: function() { | ||
return this.firstName + ' ' + this.lastName; | ||
} | ||
fullName: function () { | ||
return this.firstName + " " + this.lastName; | ||
}, | ||
books: function () { | ||
return Books.find({ authorId: this._id }); | ||
}, | ||
}); | ||
|
||
const book = Books.findOne(book1); | ||
const author = book.author(); | ||
test.equal(author.firstName, "Charles"); | ||
test.equal(book.foo, "bar"); | ||
|
||
const authorCarl = Authors.findOne(author1); | ||
const booksByCarl = author.books(); | ||
test.equal(booksByCarl.count(), 1); | ||
}); | ||
}); | ||
} | ||
|
||
Tinytest.add( | ||
"throw error if transform function already exists", | ||
function (test) { | ||
Author = function (doc) { | ||
return _.extend(this, doc); | ||
}; | ||
|
||
Author.prototype.fullName = "Charles Darwin"; | ||
|
||
Authors = new Meteor.Collection("authors" + test.id, { | ||
transform: function (doc) { | ||
return new Author(doc); | ||
}, | ||
}); | ||
|
||
test.throws(function () { | ||
Authors.helpers({ | ||
fullName: function () { | ||
return this.firstName + " " + this.lastName; | ||
}, | ||
}); | ||
}); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters