-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
getters-setters.test.js
226 lines (193 loc) · 6.59 KB
/
getters-setters.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
'use strict';
const assert = require('assert');
const start = require('../common');
const mongoose = new start.mongoose.Mongoose();
const Schema = mongoose.Schema;
// This file is in `es-next` because it uses async/await for convenience
describe('getters/setters', function() {
before(async function() {
await mongoose.connect(start.uri);
});
beforeEach(function() {
mongoose.deleteModel(/.*/);
});
describe('getters', function() {
it('basic example', async function() {
const userSchema = new Schema({
email: {
type: String,
get: obfuscate
}
});
// Mongoose passes the raw value in MongoDB `email` to the getter
function obfuscate(email) {
const separatorIndex = email.indexOf('@');
if (separatorIndex < 3) {
// 'ab@gmail.com' -> '**@gmail.com'
return email.slice(0, separatorIndex).replace(/./g, '*') +
email.slice(separatorIndex);
}
// 'test42@gmail.com' -> 'te****@gmail.com'
return email.slice(0, 2) +
email.slice(2, separatorIndex).replace(/./g, '*') +
email.slice(separatorIndex);
}
const User = mongoose.model('User', userSchema);
const user = new User({ email: 'ab@gmail.com' });
user.email; // **@gmail.com
// acquit:ignore:start
assert.equal(user.email, '**@gmail.com');
user.email = 'test42@gmail.com';
assert.equal(user.email, 'te****@gmail.com');
// acquit:ignore:end
});
it('skip', function() {
// acquit:ignore:start
const userSchema = new Schema({
email: {
type: String,
get: obfuscate
}
});
// Mongoose passes the raw value in MongoDB `email` to the getter
function obfuscate(email) {
const separatorIndex = email.indexOf('@');
if (separatorIndex < 3) {
// 'ab@gmail.com' -> '**@gmail.com'
return email.slice(0, separatorIndex).replace(/./g, '*') +
email.slice(separatorIndex);
}
// 'test42@gmail.com' -> 'te****@gmail.com'
return email.slice(0, 2) +
email.slice(2, separatorIndex).replace(/./g, '*') +
email.slice(separatorIndex);
}
const User = mongoose.model('User', userSchema);
const user = new User({ email: 'ab@gmail.com' });
assert.equal(user.get('email', null, { getters: false }), 'ab@gmail.com');
// acquit:ignore:end
user.get('email', null, { getters: false }); // 'ab@gmail.com'
});
});
describe('setters', function() {
it('basic', function() {
const userSchema = new Schema({
email: {
type: String,
set: v => v.toLowerCase()
}
});
const User = mongoose.model('User', userSchema);
const user = new User({ email: 'TEST@gmail.com' });
user.email; // 'test@gmail.com'
// The raw value of `email` is lowercased
user.get('email', null, { getters: false }); // 'test@gmail.com'
// acquit:ignore:start
assert.equal(user.email, 'test@gmail.com');
assert.equal(user.get('email', null, { getters: false }), 'test@gmail.com');
// acquit:ignore:end
user.set({ email: 'NEW@gmail.com' });
user.email; // 'new@gmail.com'
// acquit:ignore:start
assert.equal(user.email, 'new@gmail.com');
// acquit:ignore:end
});
it('updates', async function() {
// acquit:ignore:start
const userSchema = new Schema({
email: {
type: String,
set: v => v.toLowerCase()
}
});
const User = mongoose.model('User', userSchema);
// acquit:ignore:end
await User.updateOne({}, { email: 'TEST@gmail.com' }, { upsert: true });
const doc = await User.findOne();
doc.email; // 'test@gmail.com'
// acquit:ignore:start
assert.equal(doc.email, 'test@gmail.com');
// acquit:ignore:end
});
it('update skip', async function() {
const userSchema = new Schema({
email: {
type: String,
set: toLower
}
});
function toLower(email) {
// Don't transform `email` if using `updateOne()` or `updateMany()`
if (!(this instanceof mongoose.Document)) {
return email;
}
return email.toLowerCase();
}
const User = mongoose.model('User', userSchema);
await User.updateOne({}, { email: 'TEST@gmail.com' }, { upsert: true });
const doc = await User.findOne();
doc.email; // 'TEST@gmail.com'
// acquit:ignore:start
assert.equal(doc.email, 'TEST@gmail.com');
// acquit:ignore:end
});
it('vs ES6', function() {
class User {
// This won't convert the email to lowercase! That's because `email`
// is just a setter, the actual `email` property doesn't store any data.
set email(v) {
return v.toLowerCase();
}
}
const user = new User();
user.email = 'TEST@gmail.com';
user.email; // undefined
// acquit:ignore:start
assert.strictEqual(user.email, undefined);
// acquit:ignore:end
});
});
describe('localization', function() {
it('locale', async function() {
const internationalizedStringSchema = new Schema({
en: String,
es: String
});
const ingredientSchema = new Schema({
// Instead of setting `name` to just a string, set `name` to a map
// of language codes to strings.
name: {
type: internationalizedStringSchema,
// When you access `name`, pull the document's locale
get: function(value) {
return value[this.$locals.language || 'en'];
}
}
});
const recipeSchema = new Schema({
ingredients: [{ type: mongoose.ObjectId, ref: 'Ingredient' }]
});
const Ingredient = mongoose.model('Ingredient', ingredientSchema);
const Recipe = mongoose.model('Recipe', recipeSchema);
// Create some sample data
const { _id } = await Ingredient.create({
name: {
en: 'Eggs',
es: 'Huevos'
}
});
await Recipe.create({ ingredients: [_id] });
// Populate with setting `$locals.language` for internationalization
const language = 'es';
const recipes = await Recipe.find().populate({
path: 'ingredients',
transform: function(doc) {
doc.$locals.language = language;
return doc;
}
});
// Gets the ingredient's name in Spanish `name.es`
assert.equal(recipes[0].ingredients[0].name, 'Huevos'); // 'Huevos'
});
})
});