set.test.js
2.14 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
'use strict';
const chai = require('chai'),
expect = chai.expect,
Support = require(__dirname + '/../support'),
DataTypes = require(__dirname + '/../../../lib/data-types'),
current = Support.sequelize;
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;
});
});
});