File tree Expand file tree Collapse file tree 3 files changed +28
-1
lines changed Expand file tree Collapse file tree 3 files changed +28
-1
lines changed Original file line number Diff line number Diff line change 1
1
import { Store } from 'vuex'
2
- import { isArray } from '../support/Utils'
2
+ import { isArray , assert } from '../support/Utils'
3
3
import { Element , Item , Collection } from '../data/Data'
4
4
import { Attribute } from './attributes/Attribute'
5
5
import { Attr } from './attributes/types/Attr'
@@ -194,6 +194,12 @@ export class Model {
194
194
* Get the store instance.
195
195
*/
196
196
get $store ( ) : Store < any > {
197
+ assert ( this . _store !== undefined , [
198
+ 'A Vuex Store instance is not injected into the model instance.' ,
199
+ 'You might be trying to instantiate the model directly. Please use' ,
200
+ '`repository.make` method to create a new model instance.'
201
+ ] )
202
+
197
203
return this . _store
198
204
}
199
205
Original file line number Diff line number Diff line change @@ -198,3 +198,13 @@ export function cloneDeep<T extends object>(target: T): T {
198
198
199
199
return target
200
200
}
201
+
202
+ /**
203
+ * Check for the given condition, and if it's `false`, it will throw a new
204
+ * error with the given message.
205
+ */
206
+ export function assert ( condition : boolean , message : string [ ] ) : void {
207
+ if ( ! condition ) {
208
+ throw new Error ( [ '[Vuex ORM]' ] . concat ( message ) . join ( ' ' ) )
209
+ }
210
+ }
Original file line number Diff line number Diff line change
1
+ import { Model } from '@/model/Model'
2
+
3
+ describe ( 'unit/model/Model' , ( ) => {
4
+ class User extends Model {
5
+ static entity = 'users'
6
+ }
7
+
8
+ it ( 'throws when accessing the store but it is not injected' , ( ) => {
9
+ expect ( ( ) => new User ( ) . $store ) . toThrow ( )
10
+ } )
11
+ } )
You can’t perform that action at this time.
0 commit comments