belongs-to.test.js
4.05 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
'use strict';
const chai = require('chai'),
expect = chai.expect,
sinon = require('sinon'),
_ = require('lodash'),
DataTypes = require('../../../lib/data-types'),
Support = require('../support'),
current = Support.sequelize;
describe(Support.getTestDialectTeaser('belongsTo'), () => {
it('throws when invalid model is passed', () => {
const User = current.define('User');
expect(() => {
User.belongsTo();
}).to.throw('User.belongsTo called with something that\'s not a subclass of Sequelize.Model');
});
it('warn on invalid options', () => {
const User = current.define('User', {});
const Task = current.define('Task', {});
expect(() => {
User.belongsTo(Task, { targetKey: 'wowow' });
}).to.throw('Unknown attribute "wowow" passed as targetKey, define this attribute on model "Task" first');
});
it('should not override custom methods with association mixin', () => {
const methods = {
getTask: 'get',
setTask: 'set',
createTask: 'create'
};
const User = current.define('User');
const Task = current.define('Task');
_.each(methods, (alias, method) => {
User.prototype[method] = function() {
const realMethod = this.constructor.associations.task[alias];
expect(realMethod).to.be.a('function');
return realMethod;
};
});
User.belongsTo(Task, { as: 'task' });
const user = User.build();
_.each(methods, (alias, method) => {
expect(user[method]()).to.be.a('function');
});
});
describe('association hooks', () => {
beforeEach(function() {
this.Projects = this.sequelize.define('Project', { title: DataTypes.STRING });
this.Tasks = this.sequelize.define('Task', { title: DataTypes.STRING });
});
describe('beforeBelongsToAssociate', () => {
it('should trigger', function() {
const beforeAssociate = sinon.spy();
this.Projects.beforeAssociate(beforeAssociate);
this.Projects.belongsTo(this.Tasks, { hooks: true });
const beforeAssociateArgs = beforeAssociate.getCall(0).args;
expect(beforeAssociate).to.have.been.called;
expect(beforeAssociateArgs.length).to.equal(2);
const firstArg = beforeAssociateArgs[0];
expect(Object.keys(firstArg).join()).to.equal('source,target,type');
expect(firstArg.source).to.equal(this.Projects);
expect(firstArg.target).to.equal(this.Tasks);
expect(firstArg.type.name).to.equal('BelongsTo');
expect(beforeAssociateArgs[1].sequelize.constructor.name).to.equal('Sequelize');
});
it('should not trigger association hooks', function() {
const beforeAssociate = sinon.spy();
this.Projects.beforeAssociate(beforeAssociate);
this.Projects.belongsTo(this.Tasks, { hooks: false });
expect(beforeAssociate).to.not.have.been.called;
});
});
describe('afterBelongsToAssociate', () => {
it('should trigger', function() {
const afterAssociate = sinon.spy();
this.Projects.afterAssociate(afterAssociate);
this.Projects.belongsTo(this.Tasks, { hooks: true });
const afterAssociateArgs = afterAssociate.getCall(0).args;
expect(afterAssociate).to.have.been.called;
expect(afterAssociateArgs.length).to.equal(2);
const firstArg = afterAssociateArgs[0];
expect(Object.keys(firstArg).join()).to.equal('source,target,type,association');
expect(firstArg.source).to.equal(this.Projects);
expect(firstArg.target).to.equal(this.Tasks);
expect(firstArg.type.name).to.equal('BelongsTo');
expect(firstArg.association.constructor.name).to.equal('BelongsTo');
expect(afterAssociateArgs[1].sequelize.constructor.name).to.equal('Sequelize');
});
it('should not trigger association hooks', function() {
const afterAssociate = sinon.spy();
this.Projects.afterAssociate(afterAssociate);
this.Projects.belongsTo(this.Tasks, { hooks: false });
expect(afterAssociate).to.not.have.been.called;
});
});
});
});