belongs-to-many-mixins.test.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import type {
  2. BelongsToManyAddAssociationsMixin,
  3. BelongsToManyCountAssociationsMixin,
  4. BelongsToManyCreateAssociationMixin,
  5. BelongsToManyGetAssociationsMixin,
  6. BelongsToManyHasAssociationsMixin,
  7. BelongsToManyRemoveAssociationsMixin,
  8. BelongsToManySetAssociationsMixin,
  9. CreationOptional,
  10. InferAttributes,
  11. InferCreationAttributes,
  12. } from '@sequelize/core';
  13. import { Model } from '@sequelize/core';
  14. import { BelongsToMany } from '@sequelize/core/decorators-legacy';
  15. import { expect } from 'chai';
  16. import { beforeAll2, sequelize, setResetMode } from '../support';
  17. describe('belongsToMany Mixins', () => {
  18. setResetMode('destroy');
  19. const vars = beforeAll2(async () => {
  20. class Article extends Model<InferAttributes<Article>, InferCreationAttributes<Article>> {
  21. declare id: CreationOptional<number>;
  22. @BelongsToMany(() => User, { through: 'UserArticle' })
  23. declare authors?: User[];
  24. declare getAuthors: BelongsToManyGetAssociationsMixin<User>;
  25. declare setAuthors: BelongsToManySetAssociationsMixin<User, User['id']>;
  26. declare addAuthors: BelongsToManyAddAssociationsMixin<User, User['id']>;
  27. declare removeAuthors: BelongsToManyRemoveAssociationsMixin<User, User['id']>;
  28. declare createAuthor: BelongsToManyCreateAssociationMixin<User>;
  29. declare hasAuthors: BelongsToManyHasAssociationsMixin<User, User['id']>;
  30. declare countAuthors: BelongsToManyCountAssociationsMixin<User>;
  31. }
  32. class User extends Model<InferAttributes<User>, InferCreationAttributes<User>> {
  33. declare id: CreationOptional<number>;
  34. }
  35. sequelize.addModels([Article, User]);
  36. await sequelize.sync({ force: true });
  37. return { Article, User };
  38. });
  39. describe('setAssociations', () => {
  40. it('associates target models to the source model', async () => {
  41. const { User, Article } = vars;
  42. const [article, user] = await Promise.all([Article.create(), User.create()]);
  43. expect(await article.getAuthors()).to.be.empty;
  44. expect(article.id).to.be.a('number');
  45. expect(user.id).to.be.a('number');
  46. await article.setAuthors([user]);
  47. expect(await article.getAuthors()).to.have.length(1);
  48. });
  49. it('supports any iterable', async () => {
  50. const { User, Article } = vars;
  51. const [article, user] = await Promise.all([Article.create(), User.create()]);
  52. expect(await article.getAuthors()).to.be.empty;
  53. await article.setAuthors(new Set([user]));
  54. expect(await article.getAuthors()).to.have.length(1);
  55. });
  56. it('unlinks the previous associations', async () => {
  57. const { User, Article } = vars;
  58. const [article, user1, user2] = await Promise.all([
  59. Article.create(),
  60. User.create(),
  61. User.create(),
  62. ]);
  63. expect(await article.getAuthors()).to.be.empty;
  64. await article.setAuthors([user1]);
  65. expect(await article.getAuthors()).to.have.length(1);
  66. await article.setAuthors([user2]);
  67. expect(await article.getAuthors()).to.have.length(1);
  68. expect(await article.hasAuthors([user2])).to.be.true;
  69. });
  70. it('clears associations when the parameter is null', async () => {
  71. const { User, Article } = vars;
  72. const [article, user] = await Promise.all([Article.create(), User.create()]);
  73. await article.setAuthors([user]);
  74. expect(await article.getAuthors()).to.have.length(1);
  75. await article.setAuthors(null);
  76. expect(await article.getAuthors()).to.be.empty;
  77. });
  78. it('supports passing the primary key instead of an object', async () => {
  79. const { User, Article } = vars;
  80. const [article, user] = await Promise.all([Article.create(), User.create()]);
  81. expect(await article.getAuthors()).to.be.empty;
  82. await article.setAuthors([user.id]);
  83. expect(await article.getAuthors()).to.have.length(1);
  84. });
  85. });
  86. describe('addAssociations', () => {
  87. it('associates target models to the source model', async () => {
  88. const { User, Article } = vars;
  89. const [article, user] = await Promise.all([Article.create(), User.create()]);
  90. expect(await article.getAuthors()).to.be.empty;
  91. await article.addAuthors([user]);
  92. expect(await article.getAuthors()).to.have.length(1);
  93. });
  94. it('supports any iterable', async () => {
  95. const { User, Article } = vars;
  96. const [article, user] = await Promise.all([Article.create(), User.create()]);
  97. expect(await article.getAuthors()).to.be.empty;
  98. await article.addAuthors(new Set([user]));
  99. expect(await article.getAuthors()).to.have.length(1);
  100. });
  101. it('supports passing the primary key instead of an object', async () => {
  102. const { User, Article } = vars;
  103. const [article, user] = await Promise.all([Article.create(), User.create()]);
  104. expect(await article.getAuthors()).to.be.empty;
  105. await article.addAuthors([user.id]);
  106. expect(await article.getAuthors()).to.have.length(1);
  107. });
  108. });
  109. describe('removeAssociations', () => {
  110. it('unlinks the target models from the source model', async () => {
  111. const { User, Article } = vars;
  112. const [article, user1, user2] = await Promise.all([
  113. Article.create(),
  114. User.create(),
  115. User.create(),
  116. ]);
  117. await article.setAuthors([user1, user2]);
  118. expect(await article.getAuthors()).to.have.length(2);
  119. await article.removeAuthors([user1]);
  120. expect(await article.getAuthors()).to.have.length(1);
  121. expect(await article.hasAuthors([user1])).to.be.false;
  122. });
  123. it('supports any iterable', async () => {
  124. const { User, Article } = vars;
  125. const [article, user] = await Promise.all([Article.create(), User.create()]);
  126. await article.setAuthors([user]);
  127. await article.removeAuthors(new Set([user]));
  128. expect(await article.getAuthors()).to.have.length(0);
  129. });
  130. it('supports passing the primary key instead of an object', async () => {
  131. const { User, Article } = vars;
  132. const [article, user] = await Promise.all([Article.create(), User.create()]);
  133. await article.setAuthors([user]);
  134. await article.removeAuthors([user.id]);
  135. expect(await article.getAuthors()).to.have.length(0);
  136. });
  137. });
  138. describe('hasAssociations', () => {
  139. it('returns true if the target model is associated to the source model', async () => {
  140. const { User, Article } = vars;
  141. const [article, user1, user2] = await Promise.all([
  142. Article.create(),
  143. User.create(),
  144. User.create(),
  145. ]);
  146. await article.setAuthors([user1]);
  147. expect(await article.hasAuthors([user1])).to.be.true;
  148. expect(await article.hasAuthors([user2])).to.be.false;
  149. expect(await article.hasAuthors([user1, user2])).to.be.false;
  150. });
  151. it('supports any iterable', async () => {
  152. const { User, Article } = vars;
  153. const [article, user] = await Promise.all([Article.create(), User.create()]);
  154. await article.setAuthors([user]);
  155. expect(await article.hasAuthors(new Set([user]))).to.be.true;
  156. });
  157. it('supports passing the primary key instead of an object', async () => {
  158. const { User, Article } = vars;
  159. const [article, user] = await Promise.all([Article.create(), User.create()]);
  160. await article.setAuthors([user]);
  161. expect(await article.hasAuthors([user.id])).to.be.true;
  162. });
  163. });
  164. });