reload.test.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. 'use strict';
  2. const { expect } = require('chai');
  3. const { sequelize } = require('../../support');
  4. const { DataTypes } = require('@sequelize/core');
  5. const sinon = require('sinon');
  6. describe('Model#reload', () => {
  7. it('is not allowed if the instance does not have a primary key defined', async () => {
  8. const User = sequelize.define('User', {});
  9. const instance = User.build({});
  10. await expect(instance.reload()).to.be.rejectedWith(
  11. 'but this model instance is missing the value of its primary key',
  12. );
  13. });
  14. describe('options tests', () => {
  15. let stub;
  16. before(() => {
  17. stub = sinon.stub(sequelize, 'queryRaw').resolves({
  18. _previousDataValues: { id: 1 },
  19. dataValues: { id: 2 },
  20. });
  21. });
  22. after(() => {
  23. stub.restore();
  24. });
  25. it('should allow reloads even if options are not given', async () => {
  26. const User = sequelize.define(
  27. 'User',
  28. {
  29. id: {
  30. type: DataTypes.INTEGER,
  31. primaryKey: true,
  32. autoIncrement: true,
  33. },
  34. deletedAt: {},
  35. },
  36. {
  37. paranoid: true,
  38. },
  39. );
  40. const instance = User.build({ id: 1 }, { isNewRecord: false });
  41. await expect(instance.reload()).to.be.fulfilled;
  42. });
  43. });
  44. });