File tree Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -94,6 +94,17 @@ export class Repository<M extends Model> {
94
94
return this . query ( ) . find ( ids )
95
95
}
96
96
97
+ /**
98
+ * Create a new model instance. This method will not save the model to the
99
+ * store. It's pretty much the alternative to `new Model()`, but it injects
100
+ * the store instance to support model instance methods in SSR environment.
101
+ */
102
+ make ( attributes ?: Element ) : M {
103
+ return this . model . $newInstance ( attributes , {
104
+ relations : false
105
+ } )
106
+ }
107
+
97
108
/**
98
109
* Insert the given record to the store.
99
110
*/
Original file line number Diff line number Diff line change
1
+ import { createStore , assertModel } from 'test/Helpers'
2
+ import { Model , Attr , Str } from '@/index'
3
+
4
+ describe ( 'unit/repository/Repository' , ( ) => {
5
+ class User extends Model {
6
+ static entity = 'users'
7
+
8
+ @Attr ( ) id ! : any
9
+ @Str ( 'John Doe' ) name ! : string
10
+ }
11
+
12
+ it ( 'creates a new model instance' , ( ) => {
13
+ const store = createStore ( [ User ] )
14
+
15
+ const user = store . $repo ( User ) . make ( )
16
+
17
+ expect ( user ) . toBeInstanceOf ( User )
18
+ expect ( user . $store ) . toBe ( store )
19
+ assertModel ( user , { id : null , name : 'John Doe' } )
20
+ } )
21
+
22
+ it ( 'creates a new model instance with default values' , ( ) => {
23
+ const store = createStore ( [ User ] )
24
+
25
+ const user = store . $repo ( User ) . make ( {
26
+ id : 1 ,
27
+ name : 'Jane Doe'
28
+ } )
29
+
30
+ expect ( user ) . toBeInstanceOf ( User )
31
+ expect ( user . $store ) . toBe ( store )
32
+ assertModel ( user , { id : 1 , name : 'Jane Doe' } )
33
+ } )
34
+ } )
You can’t perform that action at this time.
0 commit comments