|
1 | 1 | /* eslint-disable @typescript-eslint/no-var-requires */
|
2 | 2 | import chai, { expect } from 'chai'
|
3 | 3 | import sinonChai from 'sinon-chai'
|
| 4 | +import { AvailableTestModels } from './utils/available-test-models' |
4 | 5 |
|
5 | 6 | import Property from './property'
|
6 | 7 |
|
7 | 8 | chai.use(sinonChai)
|
8 | 9 |
|
9 | 10 | const db = require('../models/index.js')
|
10 | 11 |
|
11 |
| -describe('Property', () => { |
12 |
| - before(function () { |
13 |
| - this.SequelizeModel = db.sequelize.models.User |
14 |
| - this.rawAttributes = this.SequelizeModel.attributes || this.SequelizeModel.rawAttributes |
15 |
| - }) |
| 12 | +const getRawProperty = (modelName: AvailableTestModels, propertyName) => { |
| 13 | + const model = db.sequelize.models[modelName] |
| 14 | + const propertyAttrs = model.attributes || model.rawAttributes |
| 15 | + return propertyAttrs[propertyName] |
| 16 | +} |
16 | 17 |
|
| 18 | +describe('Property', () => { |
17 | 19 | describe('#isArray', () => {
|
18 |
| - it('returns false for regular (not arrayed) property', function () { |
19 |
| - const property = new Property(this.rawAttributes.email) |
| 20 | + it('returns false for regular (not arrayed) property', () => { |
| 21 | + const property = new Property(getRawProperty('User', 'email')) |
20 | 22 | expect(property.isArray()).to.equal(false)
|
21 | 23 | })
|
22 | 24 |
|
23 |
| - it('returns true for array property', function () { |
24 |
| - const property = new Property(this.rawAttributes.arrayed) |
| 25 | + it('returns true for array property', () => { |
| 26 | + const property = new Property(getRawProperty('User', 'arrayed')) |
25 | 27 | expect(property.isArray()).to.equal(true)
|
26 | 28 | })
|
27 | 29 | })
|
28 | 30 |
|
29 | 31 | describe('#type', () => {
|
30 |
| - it('returns correct string type', function () { |
31 |
| - const property = new Property(this.rawAttributes.firstName) |
| 32 | + it('returns correct string type', () => { |
| 33 | + const property = new Property(getRawProperty('User', 'firstName')) |
32 | 34 | expect(property.type()).to.equal('string')
|
33 | 35 | })
|
34 | 36 |
|
35 |
| - it('returns correct integer type', function () { |
36 |
| - const property = new Property(this.rawAttributes.id) |
| 37 | + it('returns correct integer type', () => { |
| 38 | + const property = new Property(getRawProperty('User', 'id')) |
37 | 39 | expect(property.type()).to.equal('number')
|
38 | 40 | })
|
39 | 41 |
|
40 |
| - it('returns correct date type', function () { |
41 |
| - const property = new Property(this.rawAttributes.createdAt) |
| 42 | + it('returns correct date type', () => { |
| 43 | + const property = new Property(getRawProperty('User', 'createdAt')) |
42 | 44 | expect(property.type()).to.equal('datetime')
|
43 | 45 | })
|
44 | 46 |
|
45 |
| - it('returns string when property is an array of strings', function () { |
46 |
| - const property = new Property(this.rawAttributes.arrayed) |
| 47 | + it('returns string when property is an array of strings', () => { |
| 48 | + const property = new Property(getRawProperty('User', 'arrayed')) |
47 | 49 | expect(property.type()).to.equal('string')
|
48 | 50 | })
|
49 | 51 | })
|
50 | 52 |
|
51 | 53 | describe('#availableValues', () => {
|
52 |
| - it('returns null for all standard (Non enum) values', function () { |
53 |
| - const property = new Property(this.rawAttributes.email) |
| 54 | + it('returns null for all standard (Non enum) values', () => { |
| 55 | + const property = new Property(getRawProperty('User', 'email')) |
54 | 56 | expect(property.availableValues()).to.equal(null)
|
55 | 57 | })
|
56 | 58 |
|
57 |
| - it('returns array of values for the enum field', function () { |
58 |
| - const property = new Property(this.rawAttributes.gender) |
| 59 | + it('returns array of values for the enum field', () => { |
| 60 | + const property = new Property(getRawProperty('User', 'gender')) |
59 | 61 | expect(property.availableValues()).to.deep.equal(['male', 'female'])
|
60 | 62 | })
|
61 | 63 | })
|
62 | 64 |
|
| 65 | + describe('#isEditable', () => { |
| 66 | + it('returns false for UUID property', () => { |
| 67 | + const property = new Property(getRawProperty('Comment', 'id')) |
| 68 | + expect(property.isEditable()).to.equal(false) |
| 69 | + }) |
| 70 | + }) |
| 71 | + |
| 72 | + describe('#isId', () => { |
| 73 | + it('returns true for id when its default', () => { |
| 74 | + const property = new Property(getRawProperty('User', 'id')) |
| 75 | + expect(property.isId()).to.eq(true) |
| 76 | + }) |
| 77 | + |
| 78 | + it('returns true for id when its uuid', () => { |
| 79 | + const property = new Property(getRawProperty('Comment', 'id')) |
| 80 | + expect(property.isId()).to.eq(true) |
| 81 | + }) |
| 82 | + }) |
| 83 | + |
63 | 84 | describe('isRequired', () => {
|
64 |
| - it('returns true for required fields', function () { |
65 |
| - const property = new Property(this.rawAttributes.email) |
| 85 | + it('returns true for required fields', () => { |
| 86 | + const property = new Property(getRawProperty('User', 'email')) |
66 | 87 | expect(property.isRequired()).to.equal(true)
|
67 | 88 | })
|
68 | 89 |
|
69 |
| - it('returns false for not required fields', function () { |
70 |
| - const property = new Property(this.rawAttributes.gender) |
| 90 | + it('returns false for not required fields', () => { |
| 91 | + const property = new Property(getRawProperty('User', 'gender')) |
71 | 92 | expect(property.isRequired()).to.eq(false)
|
72 | 93 | })
|
73 | 94 | })
|
|
0 commit comments