-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest.js
80 lines (60 loc) · 2.27 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
require('dotenv').config();
const mongoose = require('mongoose');
const moment = require('moment-timezone');
const offset = moment().utcOffset() * 60 * 1000;
const { ObjectId } = mongoose.Types;
let MongooseSchema;
let Schema;
let Collection;
const timeZone = require('./index');
beforeAll(async () => {
const connectionString = process.env.DATABASE_URL;
if (!connectionString) throw new Error('Environment variable "DATABASE_URL" must be a valid connection string');
await mongoose.connect(process.env.DATABASE_URL, { useNewUrlParser: true });
const db = mongoose.connections[0].db;
Collection = db.collection('schemas');
Schema = new mongoose.Schema({
date1: {
type: Date,
default: new Date(),
},
date2: {
type: Date,
default: new Date(),
},
subDoc: {
subProp: {
type: Date,
default: new Date(),
},
},
});
Schema.plugin(timeZone);
MongooseSchema = mongoose.model('Schema', Schema);
});
afterAll(() => mongoose.disconnect());
describe('timeZone', () => {
it('should save the model and return the exact saved values', async () => {
const now = new Date();
const fixedOffset = now.valueOf() + offset;
const mongooseDoc = await (new MongooseSchema({ date1: now }).save());
const mongoDocs = await Collection.find({ _id: mongooseDoc._id }).toArray();
expect(mongooseDoc.date1.valueOf()).toEqual(now.valueOf());
expect(mongoDocs[0].date1.valueOf()).toEqual(fixedOffset);
});
it('should update the model and return the exact updated values', async () => {
const now = new Date();
const tomorrow = new Date(new Date().valueOf() + 86400000);
const tomorrowFixedOffset = tomorrow.valueOf() + offset;
const mongooseDoc = await (new MongooseSchema({ date1: now }).save());
mongooseDoc.date1 = tomorrow;
await mongooseDoc.save();
const retrieved = await MongooseSchema.findOne({ _id: mongooseDoc._id });
const mongoDocs = await Collection.find({ _id: retrieved._id }).toArray();
expect(retrieved.date1.valueOf()).toEqual(tomorrow.valueOf());
expect(mongoDocs[0].date1.valueOf()).toEqual(tomorrowFixedOffset);
});
it('should work when no document is retrieved', async () => {
await MongooseSchema.findOne({ _id: new ObjectId() });
});
});