changed.test.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import type { InferAttributes, InferCreationAttributes } from '@sequelize/core';
  2. import { DataTypes, Model } from '@sequelize/core';
  3. import { expect } from 'chai';
  4. import { beforeAll2, sequelize } from '../../support';
  5. const dialect = sequelize.dialect;
  6. describe('Model#changed()', () => {
  7. describe('Non-JSON attribute', () => {
  8. const vars = beforeAll2(() => {
  9. class User extends Model<InferAttributes<User>, InferCreationAttributes<User>> {
  10. declare name: string | null;
  11. declare birthday: Date | null;
  12. }
  13. User.init(
  14. {
  15. name: DataTypes.STRING,
  16. birthday: DataTypes.DATE,
  17. },
  18. { sequelize },
  19. );
  20. return { User };
  21. });
  22. it('returns true when an non-fetched value is changed', () => {
  23. const user = vars.User.build(
  24. {
  25. name: 'a',
  26. },
  27. {
  28. isNewRecord: false,
  29. raw: true,
  30. },
  31. );
  32. expect(user.changed('name')).to.equal(false, 'name not be considered changed');
  33. user.set('name', 'b');
  34. expect(user.changed('name')).to.equal(true, 'name should be considered changed');
  35. });
  36. it('returns false when setting an existing value to the same primitive value', () => {
  37. const user = vars.User.build(
  38. {
  39. name: 'a',
  40. birthday: null,
  41. },
  42. {
  43. isNewRecord: false,
  44. raw: true,
  45. },
  46. );
  47. user.set('name', 'a');
  48. user.set('birthday', null);
  49. expect(user.changed('name')).to.equal(false);
  50. expect(user.changed('birthday')).to.equal(false);
  51. });
  52. it('returns false when setting a value to the same object value', () => {
  53. const milliseconds = 1_436_921_941_088;
  54. const firstDate = new Date(milliseconds);
  55. const secondDate = new Date(milliseconds);
  56. const user = vars.User.build(
  57. {
  58. birthday: firstDate,
  59. },
  60. {
  61. isNewRecord: false,
  62. raw: true,
  63. },
  64. );
  65. user.set('birthday', secondDate);
  66. expect(user.changed('birthday')).to.equal(false);
  67. });
  68. it('should return true when a value is modified by setDataValue', () => {
  69. const user = vars.User.build(
  70. {
  71. name: 'a',
  72. },
  73. {
  74. isNewRecord: false,
  75. raw: true,
  76. },
  77. );
  78. user.setDataValue('name', 'b');
  79. expect(user.changed('name')).to.equal(true);
  80. });
  81. it('should return true when a value is modified by direct assignations', () => {
  82. const user = vars.User.build(
  83. {
  84. name: 'a',
  85. },
  86. {
  87. isNewRecord: false,
  88. raw: true,
  89. },
  90. );
  91. user.name = 'b';
  92. expect(user.changed('name')).to.equal(true);
  93. });
  94. });
  95. if (dialect.supports.dataTypes.JSON) {
  96. describe('JSON attribute', () => {
  97. const vars = beforeAll2(() => {
  98. class User extends Model<InferAttributes<User>, InferCreationAttributes<User>> {
  99. declare json: unknown;
  100. }
  101. User.init(
  102. {
  103. json: DataTypes.JSON,
  104. },
  105. { sequelize },
  106. );
  107. return { User };
  108. });
  109. it('returns false when setting a value to the same object value', () => {
  110. for (const value of [null, 1, 'asdf', new Date(), [], {}, Buffer.from('')]) {
  111. const t = vars.User.build(
  112. {
  113. json: value,
  114. },
  115. {
  116. isNewRecord: false,
  117. raw: true,
  118. },
  119. );
  120. t.json = value;
  121. expect(t.changed('json')).to.be.false;
  122. expect(t.changed()).to.be.false;
  123. }
  124. });
  125. it('returns true when setting a value to a different primitive value with Model#set & json.path notation', () => {
  126. const user = vars.User.build(
  127. {
  128. json: {
  129. city: 'Stockholm',
  130. },
  131. },
  132. {
  133. isNewRecord: false,
  134. raw: true,
  135. },
  136. );
  137. // @ts-expect-error -- TODO: fix Model#set typings to support this syntax
  138. user.set('json.city', 'Gothenburg');
  139. expect(user.changed('json')).to.equal(true);
  140. });
  141. it('returns true when setting a value to the same primitive value with Model#set & json.path notation', () => {
  142. const user = vars.User.build(
  143. {
  144. json: {
  145. city: 'Gothenburg',
  146. },
  147. },
  148. {
  149. isNewRecord: false,
  150. raw: true,
  151. },
  152. );
  153. // @ts-expect-error -- TODO: fix Model#set typings to support this syntax
  154. user.set('json.city', 'Gothenburg');
  155. expect(user.changed('json')).to.equal(false);
  156. });
  157. it('returns true when setting a value to a different object value with set & json.path notation', () => {
  158. const user = vars.User.build(
  159. {
  160. json: {
  161. address: { street: 'Main street', number: '40' },
  162. },
  163. },
  164. {
  165. isNewRecord: false,
  166. raw: true,
  167. },
  168. );
  169. // @ts-expect-error -- TODO: fix Model#set typings to support this syntax
  170. user.set('json.address', { street: 'Second street', number: '1' });
  171. expect(user.changed('json')).to.equal(true);
  172. });
  173. it('returns false when setting a value to the same object value with set & json.path notation', () => {
  174. const user = vars.User.build(
  175. {
  176. json: {
  177. address: { street: 'Main street', number: '40' },
  178. },
  179. },
  180. {
  181. isNewRecord: false,
  182. raw: true,
  183. },
  184. );
  185. // @ts-expect-error -- TODO: fix Model#set typings to support this syntax
  186. user.set('json.address', { street: 'Main street', number: '40' });
  187. expect(user.changed('json')).to.equal(false);
  188. });
  189. });
  190. }
  191. });