set.test.js
4.96 KB
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
'use strict';
const chai = require('chai'),
expect = chai.expect,
Support = require(__dirname + '/../support'),
DataTypes = require(__dirname + '/../../../lib/data-types'),
current = Support.sequelize,
Promise = current.Promise,
sinon = require('sinon');
describe(Support.getTestDialectTeaser('Instance'), () => {
describe('set', () => {
it('sets nested keys in JSON objects', () => {
const User = current.define('User', {
meta: DataTypes.JSONB
});
const user = User.build({
meta: {
location: 'Stockhollm'
}
}, {
isNewRecord: false,
raw: true
});
const meta = user.get('meta');
user.set('meta.location', 'Copenhagen');
expect(user.dataValues['meta.location']).not.to.be.ok;
expect(user.get('meta').location).to.equal('Copenhagen');
expect(user.get('meta') === meta).to.equal(true);
expect(user.get('meta') === meta).to.equal(true);
});
it('doesnt mutate the JSONB defaultValue', () => {
const User = current.define('User', {
meta: {
type: DataTypes.JSONB,
allowNull: false,
defaultValue: {}
}
});
const user1 = User.build({});
user1.set('meta.location', 'Stockhollm');
const user2 = User.build({});
expect(user2.get('meta')).to.deep.equal({});
});
it('sets the date "1970-01-01" to previously null field', () => {
const User = current.define('User', {
date: {
type: DataTypes.DATE,
allowNull: true
}
});
const user1 = User.build({
date: null
});
user1.set('date', '1970-01-01');
expect(user1.get('date')).to.be.ok;
expect(user1.get('date').getTime()).to.equal(new Date('1970-01-01').getTime());
});
it('overwrites non-date originalValue with date', () => {
const User = current.define('User', {
date: DataTypes.DATE
});
const user = User.build({
date: ' '
}, {
isNewRecord: false,
raw: true
});
user.set('date', new Date());
expect(user.get('date')).to.be.an.instanceof(Date);
expect(user.get('date')).not.to.be.NaN;
});
describe('custom setter', () => {
before(function() {
this.stubCreate = sinon.stub(current.getQueryInterface(), 'insert', instance => {
return Promise.resolve([instance, 1]);
});
});
after(function() {
this.stubCreate.restore();
});
const User = current.define('User', {
phoneNumber: {
type: DataTypes.STRING,
set (val) {
if (typeof val === 'object' && val !== null) {
val = `00${val.country}${val.area}${val.local}`;
}
if (typeof val === 'string') {
// Canonicalize phone number
val = val.replace(/^\+/, '00').replace(/\(0\)|[\s+\/.\-\(\)]/g, '');
}
this.setDataValue('phoneNumber', val);
}
}
});
it('does not set field to changed if field is set to the same value with custom setter using primitive value', () => {
const user = User.build({
phoneNumber: '+1 234 567'
});
return user.save().then(() => {
expect(user.changed('phoneNumber')).to.be.false;
user.set('phoneNumber', '+1 (0) 234567'); // Canonical equivalent of existing phone number
expect(user.changed('phoneNumber')).to.be.false;
});
});
it('sets field to changed if field is set to the another value with custom setter using primitive value', () => {
const user = User.build({
phoneNumber: '+1 234 567'
});
return user.save().then(() => {
expect(user.changed('phoneNumber')).to.be.false;
user.set('phoneNumber', '+1 (0) 765432'); // Canonical non-equivalent of existing phone number
expect(user.changed('phoneNumber')).to.be.true;
});
});
it('does not set field to changed if field is set to the same value with custom setter using object', () => {
const user = User.build({
phoneNumber: '+1 234 567'
});
return user.save().then(() => {
expect(user.changed('phoneNumber')).to.be.false;
user.set('phoneNumber', { country: '1', area: '234', local: '567' }); // Canonical equivalent of existing phone number
expect(user.changed('phoneNumber')).to.be.false;
});
});
it('sets field to changed if field is set to the another value with custom setter using object', () => {
const user = User.build({
phoneNumber: '+1 234 567'
});
return user.save().then(() => {
expect(user.changed('phoneNumber')).to.be.false;
user.set('phoneNumber', { country: '1', area: '765', local: '432' }); // Canonical non-equivalent of existing phone number
expect(user.changed('phoneNumber')).to.be.true;
});
});
});
});
});