-
Notifications
You must be signed in to change notification settings - Fork 6
/
user-type-proxy.test.js
117 lines (104 loc) · 3.89 KB
/
user-type-proxy.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
import db, { UserType } from '../'
import GetProxy from '../proxies'
const { assert } = require('chai')
const { UserTypeProxy, Events } = GetProxy({ UserType })
const { isDataProxy } = require('./helpers')
const USER_TYPE_NAME = 'student'
const USER_TYPE_NAME2 = 'staff'
describe('UserTypeProxy', () => {
it('should be a valid Data Proxy', () => {
/**
* First, we check that the proxy object provided has all proxy functions
*/
return isDataProxy(UserTypeProxy)
})
describe('Inserts', () => {
/**
* We check that all INSERT constraints are obeyed
*/
it('insert(userType) should work', () => {
return UserTypeProxy.insert({
name: USER_TYPE_NAME
}).then(user => {
assert.isNotNull(user)
}).catch(err => {
assert.fail()
})
})
describe('Selects and Updates', () => {
const getLastUserType = () => {
/**
* retrieve the last added action
*/
return UserTypeProxy.getAll({
limit: 1,
order: [ [ 'createdAt', 'DESC' ]]
}).then(users => {
const lastUserType = users[0]
assert.isDefined(lastUserType)
return lastUserType
})
}
/**
* We check that all SELECT and UPDATE operations work
*/
it('getById(-1) should return NULL', () => {
return UserTypeProxy.getById(-1).then((user) => {
assert.isNull(user)
}).catch(err => {
assert.isNull(err)
})
})
it(`getById(id) should return an userType "${USER_TYPE_NAME}"`, () => {
return getLastUserType().then(lastUserType => {
return UserTypeProxy.getById(lastUserType.id).then(savedUserType => {
if (savedUserType) {
assert.equal(savedUserType.name, USER_TYPE_NAME)
}
}).catch(err => {
assert.isNull(err)
})
})
})
it('getAll() should return items', () => {
return UserTypeProxy.getAll({}).then((users) => {
assert.isArray(users)
users.forEach(user => {
assert.isDefined(user)
assert.isDefined(user.name)
})
}).catch(err => {
assert.isNull(err)
})
})
it('update(userType) should work', () => {
return getLastUserType().then(lastUserType => {
return UserTypeProxy.update({
id: lastUserType.id,
name: `${USER_TYPE_NAME2}`
}).then(rows => {
assert.isArray(rows)
}).catch(err => {
assert.fail()
})
}).catch(err => {
assert.isNull(err, 'update(userType)')
})
})
describe('Deletes', () => {
/**
* Last, we delete the items created
*/
it(`destroy(id) should return a boolean`, () => {
return getLastUserType().then(lastUserType => {
return UserTypeProxy.destroy(lastUserType.id).then((success) => {
assert.equal(success, 1)
}).catch(err => {
assert.isNull(err)
})
})
})
})
})
})
})