@@ -2,6 +2,7 @@ import { module, test } from 'qunit';
2
2
3
3
import { setupTest } from 'ember-qunit' ;
4
4
5
+ import { PromiseBelongsTo , PromiseManyArray } from '@ember-data/model/-private' ;
5
6
import {
6
7
registerDerivations as registerLegacyDerivations ,
7
8
withDefaults as withLegacy ,
@@ -129,4 +130,132 @@ module('Legacy | Create | relationships', function (hooks) {
129
130
assert . strictEqual ( Rey . friends [ 0 ] , Matt , 'Rey has Matt as bestFriend' ) ;
130
131
assert . strictEqual ( Matt . friends [ 0 ] , Rey , 'Matt has Rey as bestFriend' ) ;
131
132
} ) ;
133
+
134
+ test ( 'we can create with an async belongsTo' , async function ( assert ) {
135
+ type User = {
136
+ id : string | null ;
137
+ $type : 'user' ;
138
+ name : string ;
139
+ bestFriend : User | null ;
140
+ friends : User [ ] ;
141
+ [ Type ] : 'user' ;
142
+ } ;
143
+ const store = this . owner . lookup ( 'service:store' ) as Store ;
144
+ const { schema } = store ;
145
+ registerLegacyDerivations ( schema ) ;
146
+
147
+ schema . registerResource (
148
+ withLegacy ( {
149
+ type : 'user' ,
150
+ fields : [
151
+ {
152
+ name : 'name' ,
153
+ type : null ,
154
+ kind : 'attribute' ,
155
+ } ,
156
+ {
157
+ name : 'bestFriend' ,
158
+ type : 'user' ,
159
+ kind : 'belongsTo' ,
160
+ options : { async : true , inverse : 'bestFriend' } ,
161
+ } ,
162
+ ] ,
163
+ } )
164
+ ) ;
165
+
166
+ const Matt = store . push < User > ( {
167
+ data : {
168
+ type : 'user' ,
169
+ id : '2' ,
170
+ attributes : {
171
+ name : 'Matt Seidel' ,
172
+ } ,
173
+ relationships : {
174
+ bestFriend : {
175
+ data : null ,
176
+ } ,
177
+ } ,
178
+ } ,
179
+ } ) ;
180
+
181
+ const Rey = store . createRecord < User > ( 'user' , {
182
+ name : 'Rey Skybarker' ,
183
+ bestFriend : Matt ,
184
+ } ) ;
185
+
186
+ assert . strictEqual ( Rey . id , null , 'id is accessible' ) ;
187
+ assert . strictEqual ( Rey . name , 'Rey Skybarker' , 'name is accessible' ) ;
188
+ assert . true ( Rey . bestFriend instanceof PromiseBelongsTo , 'Rey has an async bestFriend' ) ;
189
+
190
+ const ReyBestFriend = await Rey . bestFriend ;
191
+ assert . strictEqual ( ReyBestFriend , Matt , 'Rey has Matt as bestFriend' ) ;
192
+
193
+ const MattBestFriend = await Matt . bestFriend ;
194
+ assert . strictEqual ( MattBestFriend , Rey , 'Matt has Rey as bestFriend' ) ;
195
+ } ) ;
196
+
197
+ test ( 'we can create with an async hasMany' , async function ( assert ) {
198
+ type User = {
199
+ id : string | null ;
200
+ $type : 'user' ;
201
+ name : string ;
202
+ bestFriend : User | null ;
203
+ friends : User [ ] ;
204
+ [ Type ] : 'user' ;
205
+ } ;
206
+ const store = this . owner . lookup ( 'service:store' ) as Store ;
207
+ const { schema } = store ;
208
+ registerLegacyDerivations ( schema ) ;
209
+
210
+ schema . registerResource (
211
+ withLegacy ( {
212
+ type : 'user' ,
213
+ fields : [
214
+ {
215
+ name : 'name' ,
216
+ type : null ,
217
+ kind : 'attribute' ,
218
+ } ,
219
+ {
220
+ name : 'friends' ,
221
+ type : 'user' ,
222
+ kind : 'hasMany' ,
223
+ options : { async : true , inverse : 'friends' } ,
224
+ } ,
225
+ ] ,
226
+ } )
227
+ ) ;
228
+
229
+ const Matt = store . push < User > ( {
230
+ data : {
231
+ type : 'user' ,
232
+ id : '2' ,
233
+ attributes : {
234
+ name : 'Matt Seidel' ,
235
+ } ,
236
+ relationships : {
237
+ friends : {
238
+ data : [ ] ,
239
+ } ,
240
+ } ,
241
+ } ,
242
+ } ) ;
243
+
244
+ const Rey = store . createRecord < User > ( 'user' , {
245
+ name : 'Rey Skybarker' ,
246
+ friends : [ Matt ] ,
247
+ } ) ;
248
+
249
+ assert . strictEqual ( Rey . id , null , 'id is accessible' ) ;
250
+ assert . strictEqual ( Rey . name , 'Rey Skybarker' , 'name is accessible' ) ;
251
+ assert . true ( Rey . friends instanceof PromiseManyArray , 'Rey has async friends' ) ;
252
+
253
+ const ReyFriends = await Rey . friends ;
254
+ assert . strictEqual ( ReyFriends . length , 1 , 'Rey has only one friend :(' ) ;
255
+ assert . strictEqual ( ReyFriends [ 0 ] , Matt , 'Rey has Matt as friend' ) ;
256
+
257
+ const MattFriends = await Matt . friends ;
258
+ assert . strictEqual ( MattFriends . length , 1 , 'Matt has only one friend :(' ) ;
259
+ assert . strictEqual ( MattFriends [ 0 ] , Rey , 'Matt has Rey as friend' ) ;
260
+ } ) ;
132
261
} ) ;
0 commit comments