is-soft-deleted.test.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. 'use strict';
  2. const chai = require('chai');
  3. const expect = chai.expect;
  4. const Support = require('../../support');
  5. const current = Support.sequelize;
  6. const { DataTypes } = require('@sequelize/core');
  7. const dayjs = require('dayjs');
  8. describe(Support.getTestDialectTeaser('Instance'), () => {
  9. describe('isSoftDeleted', () => {
  10. beforeEach(function () {
  11. const User = current.define('User', {
  12. name: DataTypes.STRING,
  13. birthdate: DataTypes.DATE,
  14. meta: DataTypes.TEXT,
  15. deletedAt: {
  16. type: DataTypes.DATE,
  17. },
  18. });
  19. const ParanoidUser = current.define(
  20. 'User',
  21. {
  22. name: DataTypes.STRING,
  23. birthdate: DataTypes.DATE,
  24. meta: DataTypes.TEXT,
  25. deletedAt: {
  26. type: DataTypes.DATE,
  27. },
  28. },
  29. {
  30. paranoid: true,
  31. },
  32. );
  33. this.paranoidUser = ParanoidUser.build(
  34. {
  35. name: 'a',
  36. },
  37. {
  38. isNewRecord: false,
  39. raw: true,
  40. },
  41. );
  42. this.user = User.build(
  43. {
  44. name: 'a',
  45. },
  46. {
  47. isNewRecord: false,
  48. raw: true,
  49. },
  50. );
  51. });
  52. it('should not throw if paranoid is set to true', function () {
  53. expect(() => {
  54. this.paranoidUser.isSoftDeleted();
  55. }).to.not.throw();
  56. });
  57. it('should throw if paranoid is set to false', function () {
  58. expect(() => {
  59. this.user.isSoftDeleted();
  60. }).to.throw('Model is not paranoid');
  61. });
  62. it('should return false if the soft-delete property is the same as the default value', function () {
  63. this.paranoidUser.setDataValue('deletedAt', null);
  64. expect(this.paranoidUser.isSoftDeleted()).to.be.false;
  65. });
  66. it('should return true if the soft-delete property is set', function () {
  67. this.paranoidUser.setDataValue('deletedAt', dayjs().subtract(5, 'days').format());
  68. expect(this.paranoidUser.isSoftDeleted()).to.be.true;
  69. });
  70. });
  71. });