1
- import { expect } from 'chai' ;
1
+ import { expect , use } from 'chai' ;
2
+ import * as chaiAsPromised from 'chai-as-promised' ;
2
3
import { createSequelize } from "../utils/sequelize" ;
3
4
import { getScopeOptions } from "../../lib/services/models" ;
4
5
import { ShoeWithScopes , SHOE_DEFAULT_SCOPE , SHOE_SCOPES } from "../models/ShoeWithScopes" ;
5
6
import { Manufacturer } from "../models/Manufacturer" ;
7
+ import { Person } from "../models/Person" ;
8
+
9
+ use ( chaiAsPromised ) ;
6
10
7
11
describe ( 'scopes' , ( ) => {
8
12
@@ -28,6 +32,7 @@ describe('scopes', () => {
28
32
describe ( 'find' , ( ) => {
29
33
30
34
const BRAND = 'adiwas' ;
35
+ const OWNER = 'bob' ;
31
36
32
37
beforeEach ( ( ) => ShoeWithScopes
33
38
. create < ShoeWithScopes > ( {
@@ -37,8 +42,11 @@ describe('scopes', () => {
37
42
producedAt : new Date ( ) ,
38
43
manufacturer : {
39
44
brand : BRAND
45
+ } ,
46
+ owner : {
47
+ name : OWNER
40
48
}
41
- } , { include : [ Manufacturer ] } ) ) ;
49
+ } , { include : [ Manufacturer , Person ] } ) ) ;
42
50
43
51
it ( 'should consider default scope' , ( ) =>
44
52
@@ -64,6 +72,76 @@ describe('scopes', () => {
64
72
} )
65
73
) ;
66
74
75
+ it ( 'should not consider default scope due to unscoped call' , ( ) =>
76
+
77
+ ShoeWithScopes
78
+ . unscoped ( )
79
+ . findOne ( )
80
+ . then ( shoe => {
81
+ expect ( shoe ) . to . have . property ( 'secretKey' ) . which . is . not . null ;
82
+ } )
83
+ ) ;
84
+
85
+ describe ( 'with include options' , ( ) => {
86
+
87
+ it ( 'should consider scopes and additional included model (object)' , ( ) =>
88
+ expect ( ShoeWithScopes
89
+ . scope ( 'full' )
90
+ . findOne < ShoeWithScopes > ( {
91
+ include : [ {
92
+ model : Person ,
93
+ } ]
94
+ } )
95
+ . then ( shoe => {
96
+ expect ( shoe ) . to . have . property ( 'manufacturer' ) . which . is . not . null ;
97
+ expect ( shoe ) . to . have . property ( 'manufacturer' ) . which . have . property ( 'brand' , BRAND ) ;
98
+ expect ( shoe ) . to . have . property ( 'owner' ) . which . is . not . null ;
99
+ } )
100
+ ) . not . to . be . rejected
101
+ ) ;
102
+
103
+ it ( 'should consider scopes and additional included model (model)' , ( ) =>
104
+ expect ( ShoeWithScopes
105
+ . scope ( 'full' )
106
+ . findOne ( {
107
+ include : [ Person ]
108
+ } )
109
+ . then ( shoe => {
110
+ expect ( shoe ) . to . have . property ( 'manufacturer' ) . which . is . not . null ;
111
+ expect ( shoe ) . to . have . property ( 'manufacturer' ) . which . have . property ( 'brand' , BRAND ) ;
112
+ expect ( shoe ) . to . have . property ( 'owner' ) . which . is . not . null ;
113
+ } )
114
+ ) . not . to . be . rejected
115
+ ) ;
116
+
117
+ it ( 'should not consider default scope due to unscoped call, but additonal includes (object)' , ( ) =>
118
+
119
+ ShoeWithScopes
120
+ . unscoped ( )
121
+ . findOne ( {
122
+ include : [ { model : Person } ]
123
+ } )
124
+ . then ( shoe => {
125
+ expect ( shoe ) . to . have . property ( 'secretKey' ) . which . is . not . null ;
126
+ expect ( shoe ) . to . have . property ( 'owner' ) . which . is . not . null ;
127
+ } )
128
+ ) ;
129
+
130
+ it ( 'should not consider default scope due to unscoped call, but additonal includes (model)' , ( ) =>
131
+
132
+ ShoeWithScopes
133
+ . unscoped ( )
134
+ . findOne ( {
135
+ include : [ Person ]
136
+ } )
137
+ . then ( shoe => {
138
+ expect ( shoe ) . to . have . property ( 'secretKey' ) . which . is . not . null ;
139
+ expect ( shoe ) . to . have . property ( 'owner' ) . which . is . not . null ;
140
+ } )
141
+ ) ;
142
+
143
+ } ) ;
144
+
67
145
} ) ;
68
146
69
147
} ) ;
0 commit comments