11import { Model as ORMModel } from "@vuex-orm/core" ;
2- import Vue from 'vue' ;
32import { createStore , sendWithMockFetch } from "../support/Helpers" ;
4- import fetchMock from 'fetch-mock' ;
53
64let store ;
75let vuexOrmApollo ;
@@ -19,6 +17,23 @@ class User extends ORMModel {
1917 }
2018}
2119
20+ class Video extends ORMModel {
21+ static entity = 'videos' ;
22+ static eagerLoad = [ 'comments' ] ;
23+
24+ static fields ( ) {
25+ return {
26+ id : this . increment ( null ) ,
27+ content : this . string ( '' ) ,
28+ title : this . string ( '' ) ,
29+ userId : this . number ( 0 ) ,
30+ otherId : this . number ( 0 ) , // This is a field which ends with `Id` but doesn't belong to any relation
31+ user : this . belongsTo ( User , 'userId' ) ,
32+ comments : this . morphMany ( Comment , 'subjectId' , 'subjectType' )
33+ } ;
34+ }
35+ }
36+
2237class Post extends ORMModel {
2338 static entity = 'posts' ;
2439 static eagerLoad = [ 'comments' ] ;
@@ -31,7 +46,7 @@ class Post extends ORMModel {
3146 userId : this . number ( 0 ) ,
3247 otherId : this . number ( 0 ) , // This is a field which ends with `Id` but doesn't belong to any relation
3348 user : this . belongsTo ( User , 'userId' ) ,
34- comments : this . hasMany ( Comment , 'userId ' )
49+ comments : this . morphMany ( Comment , 'subjectId' , 'subjectType ')
3550 } ;
3651 }
3752}
@@ -45,23 +60,26 @@ class Comment extends ORMModel {
4560 id : this . increment ( 0 ) ,
4661 content : this . string ( '' ) ,
4762 userId : this . number ( 0 ) ,
48- postId : this . number ( 0 ) ,
4963 user : this . belongsTo ( User , 'userId' ) ,
50- post : this . belongsTo ( Post , 'postId' )
64+
65+ subjectId : this . number ( 0 ) ,
66+ subjectType : this . string ( '' )
5167 } ;
5268 }
5369}
5470
5571describe ( 'VuexORMApollo' , ( ) => {
5672 beforeEach ( ( ) => {
57- [ store , vuexOrmApollo ] = createStore ( [ { model : User } , { model : Post } , { model : Comment } ] ) ;
73+ [ store , vuexOrmApollo ] = createStore ( [ { model : User } , { model : Post } , { model : Video } , { model : Comment } ] ) ;
5874
5975 store . dispatch ( 'entities/users/insert' , { data : { id : 1 , name : 'Charlie Brown' } } ) ;
6076 store . dispatch ( 'entities/users/insert' , { data : { id : 2 , name : 'Peppermint Patty' } } ) ;
6177 store . dispatch ( 'entities/posts/insert' , { data : { id : 1 , userId : 1 , title : 'Example post 1' , content : 'Foo' } } ) ;
6278 store . dispatch ( 'entities/posts/insert' , { data : { id : 1 , userId : 1 , title : 'Example post 2' , content : 'Bar' } } ) ;
63- store . dispatch ( 'entities/comments/insert' , { data : { id : 1 , userId : 1 , postId : 1 , content : 'Example comment 1' } } ) ;
64- store . dispatch ( 'entities/comments/insert' , { data : { id : 1 , userId : 2 , postId : 1 , content : 'Example comment 2' } } ) ;
79+ store . dispatch ( 'entities/videos/insert' , { data : { id : 1 , userId : 1 , title : 'Example video' , content : 'Video' } } ) ;
80+ store . dispatch ( 'entities/comments/insert' , { data : { id : 1 , userId : 1 , subjectId : 1 , subjectType : 'videos' , content : 'Example comment 1' } } ) ;
81+ store . dispatch ( 'entities/comments/insert' , { data : { id : 1 , userId : 2 , subjectId : 1 , subjectType : 'posts' , content : 'Example comment 2' } } ) ;
82+ store . dispatch ( 'entities/comments/insert' , { data : { id : 1 , userId : 2 , subjectId : 2 , subjectType : 'posts' , content : 'Example comment 3' } } ) ;
6583 } ) ;
6684
6785 describe ( 'fetch' , ( ) => {
@@ -70,13 +88,19 @@ describe('VuexORMApollo', () => {
7088 data : {
7189 post : {
7290 __typename : 'post' ,
73- id : 1 ,
91+ id : 42 ,
7492 otherId : 13548 ,
75- title : 'Example Post 1 ' ,
93+ title : 'Example Post 5 ' ,
7694 content : 'Foo' ,
7795 comments : {
7896 __typename : 'comment' ,
79- nodes : [ ]
97+ nodes : [ {
98+ __typename : 'comment' ,
99+ id : 15 ,
100+ content : 'Works!' ,
101+ subjectId : 42 ,
102+ subjectType : 'Post'
103+ } ]
80104 } ,
81105 user : {
82106 __typename : 'user' ,
@@ -88,7 +112,7 @@ describe('VuexORMApollo', () => {
88112 } ;
89113
90114 let request = await sendWithMockFetch ( response , async ( ) => {
91- await store . dispatch ( 'entities/posts/fetch' , { filter : { id : 1 } } ) ;
115+ await store . dispatch ( 'entities/posts/fetch' , { filter : { id : 42 } } ) ;
92116 } ) ;
93117 expect ( request ) . not . toEqual ( null ) ;
94118
@@ -108,6 +132,8 @@ query Post($id: ID!) {
108132 nodes {
109133 id
110134 content
135+ subjectId
136+ subjectType
111137 __typename
112138 }
113139 __typename
@@ -116,6 +142,11 @@ query Post($id: ID!) {
116142 }
117143}
118144 ` . trim ( ) + "\n" ) ;
145+
146+ const post = store . getters [ 'entities/posts/query' ] ( ) . withAll ( ) . where ( 'id' , 42 ) . first ( ) ;
147+ expect ( post . title ) . toEqual ( 'Example Post 5' ) ;
148+ expect ( post . comments . length ) . toEqual ( 1 ) ;
149+ expect ( post . comments [ 0 ] . content ) . toEqual ( 'Works!' ) ;
119150 } ) ;
120151
121152
@@ -392,6 +423,8 @@ mutation UpvotePost($post: PostInput!, $captchaToken: String!) {
392423 nodes {
393424 id
394425 content
426+ subjectId
427+ subjectType
395428 __typename
396429 }
397430 __typename
0 commit comments