11import makeServer from './drivers/default/server' ;
22import Post from '../stubs/models/post' ;
33import { Batch } from '../../src/batch' ;
4+ import { Orion } from '../../src/orion' ;
45
56let server : any ;
67
78beforeEach ( ( ) => {
89 server = makeServer ( ) ;
10+ Orion . setBaseUrl ( "https://api-mock.test/api" )
911} ) ;
1012
1113afterEach ( ( ) => {
@@ -14,25 +16,98 @@ afterEach(() => {
1416
1517describe ( 'Batch tests' , ( ) => {
1618 test ( 'saving a couple of models' , async ( ) => {
17- await Batch . store (
18- [
19- new Post ( { title : "item1" , id : 10 , created_at : "" , updated_at : "" } ) ,
20- new Post ( { title : "item2" , id : 12 , created_at : "" , updated_at : "" } ) ,
21- ]
22- )
19+ const posts = [
20+ new Post ( ) ,
21+ new Post ( ) ,
22+ ]
23+ posts [ 0 ] . $attributes . title = "First" ;
24+ posts [ 1 ] . $attributes . title = "Second" ;
25+
26+ const res = await Batch . store ( posts ) ;
27+
28+ expect ( server . schema . posts . all ( ) ) . toHaveLength ( 2 ) ;
29+ expect ( server . schema . posts . find ( '1' ) . attrs . title ) . toBe ( "First" )
30+ expect ( server . schema . posts . find ( '2' ) . attrs . title ) . toBe ( "Second" )
31+ expect ( server . schema . posts . find ( '1' ) . attrs . title ) . toEqual ( res [ 0 ] . $attributes . title )
32+ expect ( server . schema . posts . find ( '1' ) . attrs . created_at ) . toEqual ( res [ 0 ] . $attributes . created_at )
33+ expect ( server . schema . posts . find ( '2' ) . attrs . title ) . toEqual ( res [ 1 ] . $attributes . title )
34+ expect ( server . schema . posts . find ( '2' ) . attrs . created_at ) . toEqual ( res [ 1 ] . $attributes . created_at )
2335 } ) ;
2436
2537 test ( 'updating a couple of models' , async ( ) => {
26- const post1 = await Post . $query ( ) . find ( 1 ) ;
27- const post2 = await Post . $query ( ) . find ( 2 ) ;
38+ const posts = [
39+ new Post ( ) ,
40+ new Post ( ) ,
41+ new Post ( ) ,
42+ ]
43+ posts [ 0 ] . $attributes . title = "First" ;
44+ posts [ 1 ] . $attributes . title = "Second" ;
45+ posts [ 2 ] . $attributes . title = "Third" ;
46+
47+ let res = await Batch . store ( posts ) ;
48+
49+ res [ 0 ] . $attributes . title = "NewFirst" ;
50+ res [ 1 ] . $attributes . title = "NewSecond" ;
51+
52+ res = await Batch . update ( [ res [ 0 ] , res [ 1 ] ] ) ;
53+
54+ expect ( res ) . toHaveLength ( 2 ) ;
55+ expect ( server . schema . posts . find ( '1' ) . attrs . title ) . toBe ( "NewFirst" )
56+ expect ( server . schema . posts . find ( '2' ) . attrs . title ) . toBe ( "NewSecond" )
57+ expect ( server . schema . posts . find ( '1' ) . attrs . title ) . toEqual ( res [ 0 ] . $attributes . title )
58+ expect ( server . schema . posts . find ( '2' ) . attrs . title ) . toEqual ( res [ 1 ] . $attributes . title )
59+ expect ( server . schema . posts . find ( '3' ) . attrs . title ) . toEqual ( "Third" ) ;
60+
61+ } ) ;
62+
63+ test ( 'deleting a couple of models' , async ( ) => {
64+ const posts = [
65+ new Post ( ) ,
66+ new Post ( ) ,
67+ new Post ( ) ,
68+ ]
69+ posts [ 0 ] . $attributes . title = "First" ;
70+ posts [ 1 ] . $attributes . title = "Second" ;
71+ posts [ 2 ] . $attributes . title = "Third" ;
72+
73+ let res = await Batch . store ( posts ) ;
74+
75+ let ModelDelete = await Batch . delete ( [ res [ 1 ] ] ) ;
76+ let idDelete = await Batch . delete ( [ 3 ] , new Post ) ;
77+
78+ expect ( server . schema . posts . find ( '1' ) . attrs . deleted_at ) . toBeUndefined ( ) ;
79+ expect ( server . schema . posts . find ( '2' ) . attrs . deleted_at ) . toBeDefined ( ) ;
80+ expect ( server . schema . posts . find ( '3' ) . attrs . deleted_at ) . toBeDefined ( ) ;
81+ expect ( server . schema . posts . find ( '2' ) . attrs . title ) . toEqual ( ModelDelete [ 0 ] . $attributes . title )
82+ expect ( server . schema . posts . find ( '3' ) . attrs . title ) . toEqual ( idDelete [ 0 ] . $attributes . title )
83+
84+
85+ } ) ;
86+
87+ test ( 'restoring a couple of models' , async ( ) => {
88+ const posts = [
89+ new Post ( ) ,
90+ new Post ( ) ,
91+ new Post ( ) ,
92+ ]
93+ posts [ 0 ] . $attributes . title = "First" ;
94+ posts [ 1 ] . $attributes . title = "Second" ;
95+ posts [ 2 ] . $attributes . title = "Third" ;
96+
97+ let res = await Batch . store ( posts ) ;
98+
99+ // delete ID 2 & 3
100+ let ModelDelete = await Batch . delete ( [ res [ 1 ] ] ) ;
101+ let idDelete = await Batch . delete ( [ 3 ] , new Post ) ;
102+
103+ res = await Batch . restore ( [ ...ModelDelete , ...idDelete ] ) ;
104+
105+ expect ( server . schema . posts . find ( '1' ) . attrs . deleted_at ) . toBeFalsy ( ) ;
106+ expect ( server . schema . posts . find ( '2' ) . attrs . deleted_at ) . toBeFalsy ( ) ;
107+ expect ( server . schema . posts . find ( '3' ) . attrs . deleted_at ) . toBeFalsy ( ) ;
108+ expect ( server . schema . posts . find ( '2' ) . attrs . title ) . toEqual ( res [ 0 ] . $attributes . title ) ;
109+ expect ( server . schema . posts . find ( '3' ) . attrs . title ) . toEqual ( res [ 1 ] . $attributes . title ) ;
28110
29- post1 . $attributes . title = "new title" ;
30- post2 . $attributes . title = "newer title" ;
31111
32- await Batch . update (
33- [
34- post1 , post2
35- ]
36- )
37112 } ) ;
38113} ) ;
0 commit comments