Permalink
Browse files

added failing indexOf() test

  • Loading branch information...
tj committed Apr 18, 2011
1 parent 5a12992 commit a764a391e36a1c295dbad7e8c06cf99a4a8e5b3c
Showing with 48 additions and 4 deletions.
  1. +3 −1 Makefile
  2. +45 −3 test/types.array.test.js
View
@@ -1,12 +1,14 @@
+TESTS = $(shell find test/ -name '*.test.js')
+
test:
@NODE_ENV=test ./support/expresso/bin/expresso \
-I lib \
-I support \
-I support/should.js/lib \
-I support/cli-table/lib \
$(TESTFLAGS) \
- $(shell find test/ -name '*.test.js')
+ $(TESTS)
test-cov:
@TESTFLAGS=--cov $(MAKE) test
View
@@ -3,22 +3,64 @@
* Module dependencies.
*/
-var mongoose = require('./common').mongoose
+var start = require('./common')
+ , should = require('should')
+ , mongoose = require('./common').mongoose
+ , Schema = mongoose.Schema
, MongooseArray = mongoose.Types.Array;
+var User = new Schema({
+ name: String
+ , pets: [Schema.ObjectId]
+});
+
+mongoose.model('User', User);
+
+var Pet = new Schema({
+ name: String
+});
+
+mongoose.model('Pet', Pet);
+
/**
* Test.
*/
module.exports = {
'test that a mongoose array behaves and quacks like an array': function(){
- var a = new MongooseArray();
+ var a = new MongooseArray;
a.should.be.an.instanceof(Array);
a.should.be.an.instanceof(MongooseArray);
Array.isArray(a).should.be.true;
(a._atomics.constructor).should.eql(Object);
- }
+ },
+
+ 'test indexOf()': function(){
+ var db = start()
+ , a = new MongooseArray
+ , User = db.model('User')
+ , Pet = db.model('Pet');
+
+ var tj = new User({ name: 'tj' })
+ , tobi = new Pet({ name: 'tobi' })
+ , loki = new Pet({ name: 'loki' })
+ , jane = new Pet({ name: 'jane' });
+ tj.pets.push(tobi);
+ tj.pets.push(loki);
+ tj.pets.push(jane);
+
+ tj.save(function(err){
+ User.findOne({ name: 'tj' }, function(err, user){
+ should.equal(null, err, 'error in callback');
+ user.pets.should.have.length(3);
+ user.pets.indexOf(tobi.id).should.equal(0);
+ user.pets.indexOf(loki.id).should.equal(1);
+ user.pets.indexOf(jane.id).should.equal(2);
+ db.close();
+ });
+ });
+ }
};

0 comments on commit a764a39

Please sign in to comment.