不要怂,就是干,撸起袖子干!

is-soft-deleted.test.js 2.21 KB
'use strict';

const chai = require('chai'),
  expect = chai.expect,
  Support   = require(__dirname + '/../support'),
  current   = Support.sequelize,
  DataTypes = require(__dirname + '/../../../lib/data-types'),
  Sequelize = Support.Sequelize,
  moment    = require('moment');

describe(Support.getTestDialectTeaser('Instance'), () => {
  describe('isSoftDeleted', () => {
    beforeEach(function() {
      const User = current.define('User', {
        name: DataTypes.STRING,
        birthdate: DataTypes.DATE,
        meta: DataTypes.JSON,
        deletedAt: {
          type: Sequelize.DATE
        }
      });

      const ParanoidUser = current.define('User', {
        name: DataTypes.STRING,
        birthdate: DataTypes.DATE,
        meta: DataTypes.JSON,
        deletedAt: {
          type: Sequelize.DATE
        }
      }, {
        paranoid: true
      });

      this.paranoidUser = ParanoidUser.build({
        name: 'a'
      }, {
        isNewRecord: false,
        raw: true
      });

      this.user = User.build({
        name: 'a'
      }, {
        isNewRecord: false,
        raw: true
      });
    });

    it('should not throw if paranoid is set to true', function() {
      expect(() => {
        this.paranoidUser.isSoftDeleted();
      }).to.not.throw();
    });

    it('should throw if paranoid is set to false', function() {
      expect(() => {
        this.user.isSoftDeleted();
      }).to.throw('Model is not paranoid');
    });

    it('should return false if the soft-delete property is the same as ' +
      'the default value', function() {
      this.paranoidUser.setDataValue('deletedAt', null);
      expect(this.paranoidUser.isSoftDeleted()).to.be.false;
    });

    it('should return false if the soft-delete property is set to a date in ' +
      'the future', function() {
      this.paranoidUser.setDataValue('deletedAt', moment().add(5, 'days').format());
      expect(this.paranoidUser.isSoftDeleted()).to.be.false;
    });

    it('should return true if the soft-delete property is set to a date ' +
      'before now', function() {
      this.paranoidUser.setDataValue('deletedAt', moment().subtract(5, 'days').format());
      expect(this.paranoidUser.isSoftDeleted()).to.be.true;
    });
  });
});