Blame view
node_modules/loopback-datasource-juggler/test/mixins.test.js
4.07 KB
f7563de62
|
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 |
// Copyright IBM Corp. 2014,2016. All Rights Reserved. // Node module: loopback-datasource-juggler // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT 'use strict'; // This test written in mocha+should.js var should = require('./init.js'); var jdb = require('../'); var ModelBuilder = jdb.ModelBuilder; var DataSource = jdb.DataSource; var Memory = require('../lib/connectors/memory'); var modelBuilder = new ModelBuilder(); var mixins = modelBuilder.mixins; function timestamps(Model, options) { Model.defineProperty('createdAt', {type: Date}); Model.defineProperty('updatedAt', {type: Date}); var originalBeforeSave = Model.beforeSave; Model.beforeSave = function(next, data) { Model.applyTimestamps(data, this.isNewRecord()); if (data.createdAt) { this.createdAt = data.createdAt; } if (data.updatedAt) { this.updatedAt = data.updatedAt; } if (originalBeforeSave) { originalBeforeSave.apply(this, arguments); } else { next(); } }; Model.applyTimestamps = function(data, creation) { data.updatedAt = new Date(); if (creation) { data.createdAt = data.updatedAt; } }; } mixins.define('TimeStamp', timestamps); describe('Model class', function() { it('should define mixins', function() { mixins.define('Example', function(Model, options) { Model.prototype.example = function() { return options; }; }); mixins.define('Demo', function(Model, options) { Model.demoMixin = options.value; }); mixins.define('Multi', function(Model, options) { Model.multiMixin = Model.multiMixin || {}; Model.multiMixin[options.key] = options.value; }); }); it('should apply a mixin class', function() { var Address = modelBuilder.define('Address', { street: {type: 'string', required: true}, city: {type: 'string', required: true}, }); var memory = new DataSource('mem', {connector: Memory}, modelBuilder); var Item = memory.createModel('Item', {name: 'string'}, { mixins: {Address: true}, }); var properties = Item.definition.properties; properties.street.should.eql({type: String, required: true}); properties.city.should.eql({type: String, required: true}); }); it('should apply mixins', function(done) { var memory = new DataSource('mem', {connector: Memory}, modelBuilder); var Item = memory.createModel('Item', {name: 'string'}, { mixins: { TimeStamp: true, Demo: {value: true}, Multi: [ {key: 'foo', value: 'bar'}, {key: 'fox', value: 'baz'}, ], }, }); Item.mixin('Example', {foo: 'bar'}); Item.demoMixin.should.be.true; Item.multiMixin.foo.should.equal('bar'); Item.multiMixin.fox.should.equal('baz'); var properties = Item.definition.properties; properties.createdAt.should.eql({type: Date}); properties.updatedAt.should.eql({type: Date}); Item.create({name: 'Item 1'}, function(err, inst) { inst.createdAt.should.be.a.date; inst.updatedAt.should.be.a.date; inst.example().should.eql({foo: 'bar'}); done(); }); }); describe('#mixin()', function() { var Person, Author, Address; beforeEach(function() { Address = modelBuilder.define('Address', { street: {type: 'string', required: true}, city: {type: 'string', required: true}, }); var memory = new DataSource('mem', {connector: Memory}, modelBuilder); Person = memory.createModel('Person', {name: 'string'}); Author = memory.createModel('Author', {name: 'string'}); }); it('should register mixin class into _mixins', function() { Person.mixin(Address); Person._mixins.should.containEql(Address); }); it('should NOT share mixins registry', function() { Person.mixin(Address); Author._mixins.should.not.containEql(Address); }); it('should able to mixin same class', function() { Person.mixin(Address); Author.mixin(Address); Author._mixins.should.containEql(Address); }); }); }); |