1- import { expect } from 'chai' ;
1+ import { expect , use } from 'chai' ;
2+ import * as chaiAsPromised from 'chai-as-promised' ;
23import { createSequelize } from "../utils/sequelize" ;
34import { getScopeOptions } from "../../lib/services/models" ;
45import { ShoeWithScopes , SHOE_DEFAULT_SCOPE , SHOE_SCOPES } from "../models/ShoeWithScopes" ;
56import { Manufacturer } from "../models/Manufacturer" ;
7+ import { Person } from "../models/Person" ;
8+
9+ use ( chaiAsPromised ) ;
610
711describe ( 'scopes' , ( ) => {
812
@@ -28,6 +32,7 @@ describe('scopes', () => {
2832 describe ( 'find' , ( ) => {
2933
3034 const BRAND = 'adiwas' ;
35+ const OWNER = 'bob' ;
3136
3237 beforeEach ( ( ) => ShoeWithScopes
3338 . create < ShoeWithScopes > ( {
@@ -37,8 +42,11 @@ describe('scopes', () => {
3742 producedAt : new Date ( ) ,
3843 manufacturer : {
3944 brand : BRAND
45+ } ,
46+ owner : {
47+ name : OWNER
4048 }
41- } , { include : [ Manufacturer ] } ) ) ;
49+ } , { include : [ Manufacturer , Person ] } ) ) ;
4250
4351 it ( 'should consider default scope' , ( ) =>
4452
@@ -64,6 +72,76 @@ describe('scopes', () => {
6472 } )
6573 ) ;
6674
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+
67145 } ) ;
68146
69147} ) ;
0 commit comments