# HG changeset patch # User Michael Granger # Date 1582313304 28800 # Fri Feb 21 11:28:24 2020 -0800 # Node ID d966c34002adbd866f6e74ff422c747317f35110 # Parent e1f73810de4617dc63fa48796d52a82445df4210 Upgrade to Babel 7 and fix deletion diff --git a/.babelrc b/.babelrc --- a/.babelrc +++ b/.babelrc @@ -1,10 +1,11 @@ { "sourceMap": "inline", "presets": [ - "latest", "stage-1" + "@babel/preset-env" ], "plugins": [ - "transform-decorators-legacy" + ["@babel/plugin-proposal-decorators", { "legacy": true }], + "@babel/plugin-proposal-class-properties" ] } diff --git a/package.json b/package.json --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "description": "This is an experimental model toolkit for web applications. It's meant to allow for a model that is closer to the Domain Model pattern.", "homepage": "https://bitbucket.org/ged/bailiwick#readme", "version": "0.2.3", + "browserslist": "> 0.25%, not dead", "scripts": { "build": "babel src --out-dir ./dist", "prepublish": "yarn build", @@ -40,18 +41,17 @@ }, "main": "dist/index", "devDependencies": { - "babel-cli": "^6.24.0", - "babel-core": "^6.24.0", - "babel-eslint": "^7.2.1", - "babel-plugin-transform-decorators-legacy": "^1.3.4", - "babel-plugin-transform-runtime": "^6.23.0", - "babel-polyfill": "^6.23.0", - "babel-preset-es2015": "^6.24.0", - "babel-preset-latest": "^6.24.0", - "babel-preset-stage-1": "^6.22.0", + "@babel/cli": "^7.0.0", + "@babel/core": "^7.8.4", + "@babel/plugin-proposal-class-properties": "^7.8.3", + "@babel/plugin-proposal-decorators": "^7.8.3", + "@babel/preset-env": "^7.8.4", + "@babel/register": "^7.8.3", + "babel-eslint": "^10.0.3", "chai": "^3.5.0", "chai-as-promised": "^6.0.0", - "eslint": "^3.18.0", + "core-js": "^3.6.4", + "eslint": "^6.8.0", "eslint-plugin-babel": "^4.1.1", "isomorphic-fetch": "^2.2.1", "mocha": "^3.2.0", diff --git a/scripts/mocha_runner.js b/scripts/mocha_runner.js --- a/scripts/mocha_runner.js +++ b/scripts/mocha_runner.js @@ -1,8 +1,7 @@ /* -*- javascript -*- */ "use strict"; -require('babel-core/register'); -require('babel-polyfill'); +require('@babel/register'); process.on('unhandledRejection', function (error) { console.error('Unhandled Promise Rejection:'); diff --git a/spec/associations.spec.js b/spec/associations.spec.js --- a/spec/associations.spec.js +++ b/spec/associations.spec.js @@ -33,7 +33,7 @@ sandbox = null; beforeEach( () => { - logger.outputTo( console ); + // logger.outputTo( console ); sandbox = sinon.sandbox.create(); chai.use( sinonChai ); diff --git a/spec/model.spec.js b/spec/model.spec.js --- a/spec/model.spec.js +++ b/spec/model.spec.js @@ -47,6 +47,7 @@ beforeEach( () => { sandbox = sinon.sandbox.create(); + // logger.outputTo( console ); User.datastore = new NullDatastore(); @@ -57,6 +58,7 @@ afterEach( () => { + logger.reset(); sandbox.restore(); }); @@ -271,7 +273,7 @@ sandbox.stub( User.datastore, 'remove' ). resolves( Object.assign(data, {removed_at: removedDate}) ); return expect( user.delete() ).to.be.fulfilled. - then( () => { + then( user => { expect( User.datastore.remove ).to.have.been.calledOnce; expect( user.removed_at ).to.eq( removedDate ); }); @@ -306,6 +308,7 @@ return expect( user.validate() ).to.be.fulfilled; } ); + it( 'returns a Promise that rejects if the object is not valid', () => { user.firstName = 'Nate'; // No Nates allowed user.lastName = null; // missing @@ -337,29 +340,6 @@ } ); } ); - - describe( 'deleting', () => { - - it( "deletes the object if the object has an id", () => { - let data = { id: 1234, firstName: "Morty", email: "morty@rm.co" }; - let user = new User( data ); - - user.email = "mortimer@rm.co"; - - return expect( user.delete() ).to.be.fulfilled.then( () => { - expect( user.email ).to.equal( "mortimer@rm.co" ); - } ); - - }); - - - it( "resolves if the object does not have an id", () => { - let user = new User(); - - expect( user.delete.bind( user ) ).to.throw(/Cannot delete an object with no id/); - }); - - }); } ); diff --git a/spec/null-datastore.spec.js b/spec/null-datastore.spec.js --- a/spec/null-datastore.spec.js +++ b/spec/null-datastore.spec.js @@ -23,10 +23,14 @@ beforeEach( () => { + // logger.outputTo( console ); chai.use( chaiAsPromised ); datastore = new NullDatastore(); }); + afterEach( () => { + logger.reset(); + }); describe( 'get', () => { @@ -199,11 +203,11 @@ it( 'can remove the data for an existing object', () => { let objId = ids[ 1 ]; return expect( datastore.remove(User, objId) ). - to.eventually.be.true. + to.eventually.deep.equal( objects[1] ). then( () => { expect( datastore.get(User, objId) ). to.be.rejectedWith( `No such User ID=${objId}` ); - }); + }); }); @@ -213,7 +217,7 @@ logger.debug( `Non-existent ID = ${nonexistentId}` ); return expect( datastore.remove(User, nonexistentId) ). - to.eventually.be.false; + to.eventually.be.undefined; }); }); diff --git a/src/model.js b/src/model.js --- a/src/model.js +++ b/src/model.js @@ -287,27 +287,24 @@ * result. */ delete() { - let dirtyData = {}; - for ( let field of this[ DIRTY_FIELDS ] ) { - dirtyData[ field ] = this[ DATA ][ field ]; - } - if ( this.id ) { return this.validate(). then( () => { - this[ DATASTORE ].remove( this.constructor, this.id, dirtyData ); + return this[ DATASTORE ].remove( this.constructor, this.id ); }). then( deletedData => { - logger.debug( "Updating ", this, " with results from deletion." ); - Object.assign( this[ DATA ], deletedData ); - this[ NEW_OBJECT ] = true; - this[ DIRTY_FIELDS ].clear(); - this.defineAttributes( this[ DATA ] ); + if ( deletedData ) { + logger.debug( "Updating ", this, " with results from deletion: ", deletedData ); + Object.assign( this[ DATA ], deletedData ); + this[ NEW_OBJECT ] = true; + this[ DIRTY_FIELDS ].clear(); + this.defineAttributes( this[ DATA ] ); + } return this; }); } else { - throw new Error( "Cannot delete an object with no id" ); + return Promise.resolve( null ); } } diff --git a/src/null-datastore.js b/src/null-datastore.js --- a/src/null-datastore.js +++ b/src/null-datastore.js @@ -216,15 +216,12 @@ * @method remove * @param {Class} type the collection to remove the object from * @param {Integer} id the ID of the object to remove - * @param {Object} data the object data to store * * @returns {Promise} the promise that resolves to `true` if the object existed * or `false` if it did not. */ - remove( type, id, data ) { + remove( type, id ) { let collection = this.getCollectionForType( type ); - collection.set( id, data ); - let current = collection.get( id ); collection.delete( id );