@@ -3,67 +3,76 @@ import ResourcesRemover from '../../src/services/resources-remover';
3
3
import { InvalidParameterError } from '../../src/services/errors' ;
4
4
5
5
describe ( 'services > resources-remover' , ( ) => {
6
- describe ( 'perform' , ( ) => {
7
- it ( 'should throw error if ids is not an array or empty' , async ( ) => {
8
- expect . assertions ( 3 ) ;
9
- function Actor ( ) {
10
- this . unscoped = ( ) => this ;
11
- this . sequelize = { constructor : Sequelize } ;
12
- }
6
+ const buildModelMock = ( dialect ) => {
7
+ // Sequelize is created here without connection to a database
8
+ const sequelize = new Sequelize ( { dialect } ) ;
13
9
14
- await expect ( new ResourcesRemover ( new Actor ( ) , [ ] ) . perform ( ) )
15
- . rejects
16
- . toBeInstanceOf ( InvalidParameterError ) ;
10
+ const Actor = sequelize . define ( 'actor' , { } ) ;
11
+ const Film = sequelize . define ( 'film' , { } ) ;
12
+ const ActorFilm = sequelize . define ( 'ActorFilem' , {
13
+ actorId : {
14
+ type : Sequelize . DataTypes . INTEGER ,
15
+ primaryKey : true ,
16
+ } ,
17
+ filmId : {
18
+ type : Sequelize . DataTypes . INTEGER ,
19
+ primaryKey : true ,
20
+ } ,
21
+ } ) ;
17
22
18
- await expect ( new ResourcesRemover ( new Actor ( ) , 'foo' ) . perform ( ) )
19
- . rejects
20
- . toBeInstanceOf ( InvalidParameterError ) ;
23
+ ActorFilm . belongsTo ( Actor ) ;
24
+ ActorFilm . belongsTo ( Film ) ;
21
25
22
- await expect ( new ResourcesRemover ( new Actor ( ) , { } ) . perform ( ) )
23
- . rejects
24
- . toBeInstanceOf ( InvalidParameterError ) ;
25
- } ) ;
26
+ return { Actor, Film, ActorFilm } ;
27
+ } ;
28
+
29
+ [ 'mysql' , 'mssql' , 'postgres' ] . forEach ( ( dialect ) => {
30
+ describe ( `perform with ${ dialect } ` , ( ) => {
31
+ it ( 'should throw error if ids is not an array or empty' , async ( ) => {
32
+ expect . assertions ( 3 ) ;
33
+
34
+ const { Actor } = buildModelMock ( dialect ) ;
35
+
36
+ await expect ( new ResourcesRemover ( Actor , [ ] ) . perform ( ) )
37
+ . rejects
38
+ . toBeInstanceOf ( InvalidParameterError ) ;
26
39
27
- it ( 'should remove resources with a single primary key' , async ( ) => {
28
- expect . assertions ( 1 ) ;
40
+ await expect ( new ResourcesRemover ( Actor , 'foo' ) . perform ( ) )
41
+ . rejects
42
+ . toBeInstanceOf ( InvalidParameterError ) ;
29
43
30
- function Actor ( ) {
31
- this . sequelize = { constructor : Sequelize } ;
32
- this . name = 'actor' ;
33
- this . primaryKeys = { id : { } } ;
34
- this . unscoped = ( ) => this ;
35
- this . sequelize = { constructor : Sequelize } ;
36
- this . associations = { } ;
37
- this . destroy = ( condition ) => {
44
+ await expect ( new ResourcesRemover ( Actor , { } ) . perform ( ) )
45
+ . rejects
46
+ . toBeInstanceOf ( InvalidParameterError ) ;
47
+ } ) ;
48
+
49
+ it ( 'should remove resources with a single primary key' , async ( ) => {
50
+ expect . assertions ( 1 ) ;
51
+
52
+ const { Actor } = buildModelMock ( dialect ) ;
53
+ jest . spyOn ( Actor , 'destroy' ) . mockImplementation ( ( condition ) => {
38
54
expect ( condition ) . toStrictEqual ( { where : { id : [ '1' , '2' ] } } ) ;
39
- } ;
40
- }
55
+ } ) ;
41
56
42
- await new ResourcesRemover ( new Actor ( ) , [ '1' , '2' ] ) . perform ( ) ;
43
- } ) ;
57
+ await new ResourcesRemover ( Actor , [ '1' , '2' ] ) . perform ( ) ;
58
+ } ) ;
44
59
45
- it ( 'should remove resources with composite keys' , async ( ) => {
46
- expect . assertions ( 1 ) ;
47
- function ActorFilm ( ) {
48
- this . sequelize = { constructor : Sequelize } ;
49
- this . name = 'actorFilm' ;
50
- this . primaryKeys = { actorId : { } , filmId : { } } ;
51
- this . unscoped = ( ) => this ;
52
- this . sequelize = { constructor : Sequelize } ;
53
- this . associations = { } ;
54
- this . destroy = ( condition ) => {
55
- expect ( condition ) . toStrictEqual ( {
56
- where : {
57
- [ Op . or ] : [
58
- { actorId : '1' , filmId : '2' } ,
59
- { actorId : '3' , filmId : '4' } ,
60
- ] ,
61
- } ,
60
+ it ( 'should remove resources with composite keys' , async ( ) => {
61
+ expect . assertions ( 1 ) ;
62
+
63
+ const { ActorFilm } = buildModelMock ( dialect ) ;
64
+ jest . spyOn ( ActorFilm , 'destroy' ) . mockImplementation ( ( condition ) => {
65
+ expect ( condition . where ) . toStrictEqual ( {
66
+ [ Op . or ] : [
67
+ { actorId : '1' , filmId : '2' } ,
68
+ { actorId : '3' , filmId : '4' } ,
69
+ ] ,
62
70
} ) ;
63
- } ;
64
- }
65
- const sequelizeOptions = { Sequelize } ;
66
- await new ResourcesRemover ( new ActorFilm ( ) , [ '1|2' , '3|4' ] , sequelizeOptions ) . perform ( ) ;
71
+ } ) ;
72
+
73
+ const sequelizeOptions = { Sequelize } ;
74
+ await new ResourcesRemover ( ActorFilm , [ '1|2' , '3|4' ] , sequelizeOptions ) . perform ( ) ;
75
+ } ) ;
67
76
} ) ;
68
77
} ) ;
69
78
} ) ;
0 commit comments