Blame view
node_modules/loopback-datasource-juggler/test/operation-hooks.suite/embeds-one-update.suite.js
6.02 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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
// Copyright IBM Corp. 2015,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'; var ValidationError = require('../..').ValidationError; var contextTestHelpers = require('../helpers/context-test-helpers'); var ContextRecorder = contextTestHelpers.ContextRecorder; var aCtxForModel = contextTestHelpers.aCtxForModel; var uid = require('../helpers/uid-generator'); var HookMonitor = require('../helpers/hook-monitor'); module.exports = function(dataSource, should, connectorCapabilities) { describe('EmbedsOne - update', function() { var ctxRecorder, hookMonitor, expectedError; beforeEach(function setupHelpers() { ctxRecorder = new ContextRecorder('hook not called'); hookMonitor = new HookMonitor({includeModelName: true}); expectedError = new Error('test error'); }); var Owner, Embedded; var migrated = false; beforeEach(function setupDatabase() { Embedded = dataSource.createModel('Embedded', { // Set id.generated to false to honor client side values id: {type: String, id: true, generated: false, default: uid.next}, name: {type: String, required: true}, extra: {type: String, required: false}, }); Owner = dataSource.createModel('Owner', {}); Owner.embedsOne(Embedded); hookMonitor.install(Embedded); hookMonitor.install(Owner); if (migrated) { return Owner.deleteAll(); } else { return dataSource.automigrate(Owner.modelName) .then(function() { migrated = true; }); } }); var ownerInstance, existingItem; beforeEach(function setupData() { return Owner.create({}) .then(function(inst) { ownerInstance = inst; }) .then(function() { var item = new Embedded({name: 'created'}); return ownerInstance.embeddedItem.create(item).then(function(it) { existingItem = it; }); }) .then(function() { hookMonitor.resetNames(); }); }); function callUpdate() { return ownerInstance.embeddedItem.update({name: 'updated'}); } it('triggers hooks in the correct order', function() { return callUpdate().then(function(result) { hookMonitor.names.should.eql([ 'Embedded:before save', //TODO 'Embedded:persist', 'Owner:before save', 'Owner:persist', 'Owner:loaded', 'Owner:after save', //TODO 'Embedded:loaded', 'Embedded:after save', ]); }); }); it('trigers `before save` hook on embedded model', function() { Embedded.observe('before save', ctxRecorder.recordAndNext()); return callUpdate().then(function(instance) { ctxRecorder.records.should.eql(aCtxForModel(Embedded, { currentInstance: { id: instance.id, name: 'created', extra: undefined, }, data: { name: 'updated', }, // TODO isNewInstance: true, })); }); }); // TODO it('trigers `before save` hook on owner model'); it('applies updates from `before save` hook', function() { Embedded.observe('before save', function(ctx, next) { ctx.data.extra = 'hook data'; next(); }); return callUpdate().then(function(instance) { instance.should.have.property('extra', 'hook data'); }); }); it('validates model after `before save` hook', function() { Embedded.observe('before save', invalidateEmbeddedModel); return callUpdate().then(throwShouldHaveFailed, function(err) { err.should.be.instanceOf(ValidationError); (err.details.codes || {}).should.eql({name: ['presence']}); }); }); it('aborts when `before save` hook fails', function() { Embedded.observe('before save', nextWithError(expectedError)); return callUpdate().then(throwShouldHaveFailed, function(err) { err.should.eql(expectedError); }); }); // TODO it('triggers `persist` hook on embedded model'); it('triggers `persist` hook on owner model'); it('applies updates from `persist` hook'); it('aborts when `persist` hook fails'); // TODO it('triggers `loaded` hook on embedded model'); it('triggers `loaded` hook on owner model'); it('applies updates from `loaded` hook'); it('aborts when `loaded` hook fails'); it('triggers `after save` hook on embedded model', function() { Embedded.observe('after save', ctxRecorder.recordAndNext()); return callUpdate().then(function(instance) { ctxRecorder.records.should.eql(aCtxForModel(Embedded, { instance: { id: instance.id, name: 'updated', extra: undefined, }, // TODO isNewInstance: true, })); }); }); // TODO it('triggers `after save` hook on owner model'); it('applies updates from `after save` hook', function() { Embedded.observe('after save', function(ctx, next) { ctx.instance.should.be.instanceOf(Embedded); ctx.instance.extra = 'hook data'; next(); }); return callUpdate().then(function(instance) { instance.should.have.property('extra', 'hook data'); }); }); it('aborts when `after save` hook fails', function() { Embedded.observe('after save', nextWithError(expectedError)); return callUpdate().then(throwShouldHaveFailed, function(err) { err.should.eql(expectedError); }); }); function invalidateEmbeddedModel(context, next) { if (context.instance) { context.instance.name = ''; } else { context.data.name = ''; } next(); } function nextWithError(err) { return function(context, next) { next(err); }; } function throwShouldHaveFailed() { throw new Error('operation should have failed'); } }); }; |