|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Module dependencies. |
| 5 | + */ |
| 6 | + |
| 7 | +const assert = require('assert'); |
| 8 | +const mongoose = require('../browser'); |
| 9 | +const exec = require('child_process').exec; |
| 10 | + |
| 11 | +/** |
| 12 | + * Test. |
| 13 | + */ |
| 14 | +describe('browser', function() { |
| 15 | + it('require() works with no other require calls (gh-5842)', function(done) { |
| 16 | + exec('node --eval "require(\'./browser\')"', done); |
| 17 | + }); |
| 18 | + |
| 19 | + it('using schema (gh-7170)', function(done) { |
| 20 | + exec('node --eval "const mongoose = require(\'./browser\'); new mongoose.Schema();"', done); |
| 21 | + }); |
| 22 | + |
| 23 | + it('document works (gh-4987)', function() { |
| 24 | + const schema = new mongoose.Schema({ |
| 25 | + name: { type: String, required: true }, |
| 26 | + quest: { type: String, match: /Holy Grail/i, required: true }, |
| 27 | + favoriteColor: { type: String, enum: ['Red', 'Blue'], required: true } |
| 28 | + }); |
| 29 | + |
| 30 | + assert.doesNotThrow(function() { |
| 31 | + new mongoose.Document({}, schema); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + it('document validation with arrays (gh-6175)', async function() { |
| 36 | + const Point = new mongoose.Schema({ |
| 37 | + latitude: { |
| 38 | + type: Number, |
| 39 | + required: true, |
| 40 | + min: -90, |
| 41 | + max: 90 |
| 42 | + }, |
| 43 | + longitude: { |
| 44 | + type: Number, |
| 45 | + required: true, |
| 46 | + min: -180, |
| 47 | + max: 180 |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + const schema = new mongoose.Schema({ |
| 52 | + name: { |
| 53 | + type: String, |
| 54 | + required: true |
| 55 | + }, |
| 56 | + vertices: { |
| 57 | + type: [Point], |
| 58 | + required: true |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + let test = new mongoose.Document({ |
| 63 | + name: 'Test Polygon', |
| 64 | + vertices: [ |
| 65 | + { |
| 66 | + latitude: -37.81902680201739, |
| 67 | + longitude: 144.9821037054062 |
| 68 | + } |
| 69 | + ] |
| 70 | + }, schema); |
| 71 | + |
| 72 | + // Should not throw |
| 73 | + await test.validate(); |
| 74 | + |
| 75 | + test = new mongoose.Document({ |
| 76 | + name: 'Test Polygon', |
| 77 | + vertices: [ |
| 78 | + { |
| 79 | + latitude: -37.81902680201739 |
| 80 | + } |
| 81 | + ] |
| 82 | + }, schema); |
| 83 | + |
| 84 | + const error = await test.validate().then(() => null, err => err); |
| 85 | + assert.ok(error.errors['vertices.0.longitude']); |
| 86 | + }); |
| 87 | + |
| 88 | + it('throws errors when using db operations', async function () { |
| 89 | + const TestModel = mongoose.model('Test', mongoose.Schema({ name: String })); |
| 90 | + const doc = new TestModel({ name: 'test' }); |
| 91 | + await assert.rejects(doc.save(), /Database operations not supported in @mongoosejs\/browser/); |
| 92 | + |
| 93 | + await assert.rejects(TestModel.aggregate([{ $match: {} }]), /Database operations not supported in @mongoosejs\/browser/); |
| 94 | + await assert.rejects(TestModel.create({ name: 'test' }), /Database operations not supported in @mongoosejs\/browser/); |
| 95 | + await assert.rejects(TestModel.deleteMany({ name: 'test' }), /Database operations not supported in @mongoosejs\/browser/); |
| 96 | + await assert.rejects(TestModel.deleteOne({ name: 'test' }), /Database operations not supported in @mongoosejs\/browser/); |
| 97 | + await assert.rejects(TestModel.find({ name: 'test' }), /Database operations not supported in @mongoosejs\/browser/); |
| 98 | + await assert.rejects(TestModel.findOne({ name: 'test' }), /Database operations not supported in @mongoosejs\/browser/); |
| 99 | + await assert.rejects(TestModel.updateMany({ name: 'test' }, { name: 'test2' }), /Database operations not supported in @mongoosejs\/browser/); |
| 100 | + await assert.rejects(TestModel.updateOne({ name: 'test' }, { name: 'test2' }), /Database operations not supported in @mongoosejs\/browser/); |
| 101 | + }); |
| 102 | +}); |
0 commit comments