@@ -5,7 +5,15 @@ import {
5
5
syncClasses ,
6
6
} from '../src/datastore/datastore' ;
7
7
import { PersistentModelConstructor , SortDirection } from '../src/types' ;
8
- import { pause , Model , User , Profile , testSchema } from './helpers' ;
8
+ import {
9
+ Model ,
10
+ User ,
11
+ Profile ,
12
+ Post ,
13
+ Comment ,
14
+ testSchema ,
15
+ pause ,
16
+ } from './helpers' ;
9
17
import { Predicates } from '../src/predicates' ;
10
18
import { addCommonQueryTests } from './commonAdapterTests' ;
11
19
@@ -41,7 +49,7 @@ describe('AsyncStorageAdapter tests', () => {
41
49
describe ( 'Query' , ( ) => {
42
50
let Model : PersistentModelConstructor < Model > ;
43
51
let model1Id : string ;
44
- const spyOnGetOne = jest . spyOn ( ASAdapter , 'getById ' ) ;
52
+ const spyOnGetOne = jest . spyOn ( ASAdapter , 'getByKey ' ) ;
45
53
const spyOnGetAll = jest . spyOn ( ASAdapter , 'getAll' ) ;
46
54
const spyOnMemory = jest . spyOn ( ASAdapter , 'inMemoryPagination' ) ;
47
55
@@ -92,9 +100,8 @@ describe('AsyncStorageAdapter tests', () => {
92
100
await DataStore . clear ( ) ;
93
101
} ) ;
94
102
95
- it ( 'Should call getById for query by id ' , async ( ) => {
103
+ it ( 'Should call getById for query by key ' , async ( ) => {
96
104
const result = await DataStore . query ( Model , model1Id ) ;
97
-
98
105
expect ( result . field1 ) . toEqual ( 'Some value' ) ;
99
106
expect ( spyOnGetOne ) . toHaveBeenCalled ( ) ;
100
107
expect ( spyOnGetAll ) . not . toHaveBeenCalled ( ) ;
@@ -155,11 +162,16 @@ describe('AsyncStorageAdapter tests', () => {
155
162
expect ( spyOnMemory ) . not . toHaveBeenCalled ( ) ;
156
163
} ) ;
157
164
} ) ;
165
+
158
166
describe ( 'Delete' , ( ) => {
159
167
let User : PersistentModelConstructor < User > ;
160
168
let Profile : PersistentModelConstructor < Profile > ;
161
169
let profile1Id : string ;
162
170
let user1Id : string ;
171
+ let Post : PersistentModelConstructor < Post > ;
172
+ let Comment : PersistentModelConstructor < Comment > ;
173
+ let post1Id : string ;
174
+ let comment1Id : string ;
163
175
164
176
beforeAll ( async ( ) => {
165
177
( { initSchema, DataStore } = require ( '../src/datastore/datastore' ) ) ;
@@ -183,6 +195,25 @@ describe('AsyncStorageAdapter tests', () => {
183
195
) ) ;
184
196
} ) ;
185
197
198
+ beforeEach ( async ( ) => {
199
+ const classes = initSchema ( testSchema ( ) ) ;
200
+
201
+ ( { Post } = classes as {
202
+ Post : PersistentModelConstructor < Post > ;
203
+ } ) ;
204
+
205
+ ( { Comment } = classes as {
206
+ Comment : PersistentModelConstructor < Comment > ;
207
+ } ) ;
208
+
209
+ const post = await DataStore . save ( new Post ( { title : 'Test' } ) ) ;
210
+ ( { id : post1Id } = post ) ;
211
+
212
+ ( { id : comment1Id } = await DataStore . save (
213
+ new Comment ( { content : 'Test Content' , post } )
214
+ ) ) ;
215
+ } ) ;
216
+
186
217
it ( 'Should perform a cascading delete on a record with a Has One relationship' , async ( ) => {
187
218
let user = await DataStore . query ( User , user1Id ) ;
188
219
let profile = await DataStore . query ( Profile , profile1Id ) ;
@@ -197,8 +228,26 @@ describe('AsyncStorageAdapter tests', () => {
197
228
profile = await DataStore . query ( Profile , profile1Id ) ;
198
229
199
230
// both should be undefined, even though we only explicitly deleted the user
200
- expect ( user ) . toBeUndefined ;
201
- expect ( profile ) . toBeUndefined ;
231
+ expect ( user ) . toBeUndefined ( ) ;
232
+ expect ( profile ) . toBeUndefined ( ) ;
233
+ } ) ;
234
+
235
+ it ( 'Should perform a cascading delete on a record with a Has Many relationship' , async ( ) => {
236
+ let post = await DataStore . query ( Post , post1Id ) ;
237
+ let comment = await DataStore . query ( Comment , comment1Id ) ;
238
+
239
+ // double-checking that both of the records exist at first
240
+ expect ( post . id ) . toEqual ( post1Id ) ;
241
+ expect ( comment . id ) . toEqual ( comment1Id ) ;
242
+
243
+ await DataStore . delete ( Post , post . id ) ;
244
+
245
+ post = await DataStore . query ( Post , post1Id ) ;
246
+ comment = await DataStore . query ( Comment , comment1Id ) ;
247
+
248
+ // both should be undefined, even though we only explicitly deleted the post
249
+ expect ( post ) . toBeUndefined ( ) ;
250
+ expect ( comment ) . toBeUndefined ( ) ;
202
251
} ) ;
203
252
} ) ;
204
253
0 commit comments