|
| 1 | +# Testing |
| 2 | + |
| 3 | +[[toc]] |
| 4 | + |
| 5 | +## Unit Testing |
| 6 | + |
| 7 | +To unit test vue components which use the persistence actions of this plugin, you need to mock |
| 8 | +the results of the GraphQL queries. The GraphQL plugin offers some utils to do this. |
| 9 | + |
| 10 | +First we have to import the mock method from the test utils via |
| 11 | + |
| 12 | +```js |
| 13 | +import VuexORMGraphQL from '@vuex-orm/plugin-graphql'; |
| 14 | +import { setupTestUtils, mock } from '@vuex-orm/plugin-graphql/lib/test-utils'; |
| 15 | +``` |
| 16 | + |
| 17 | +After that we have to setup the test utils, this is very easy, just pass the imported VuexORMGraphQL |
| 18 | +plugin like this: |
| 19 | + |
| 20 | +``` |
| 21 | +setupTestUtils(VuexORMGraphQL); |
| 22 | +``` |
| 23 | + |
| 24 | +Now we're ready to go. In the next step we can setup mocks via |
| 25 | + |
| 26 | +```js |
| 27 | +mock('fetch').for(User).andReturn({ id: 1, name: 'Charlie Brown' }); |
| 28 | +``` |
| 29 | + |
| 30 | +This means: Whenever `User.fetch()` is called, insert `{ id: 1, name: 'Charlie Brown' }` in the Vuex-ORM |
| 31 | +store. |
| 32 | + |
| 33 | +The mock method also accepts a second param which allows to match specific calls. Only those |
| 34 | +properties which are within the given object are considered while matching. Example: |
| 35 | + |
| 36 | +```js |
| 37 | +// Will only trigger when `User.fetch(17)` is called |
| 38 | +mock('fetch', { filter: { id: 17 } }).for(User).andReturn({ id: 17, name: 'Charlie Brown' }); |
| 39 | + |
| 40 | +// Will only trigger when `User.fetch({ filter: { active: true }})` is called |
| 41 | +mock('fetch', { filter: { active: true } }).for(User).andReturn([ |
| 42 | + { id: 17, name: 'Charlie Brown' }, |
| 43 | + { id: 18, name: 'Snoopy' } |
| 44 | +]); |
| 45 | +``` |
| 46 | + |
| 47 | +Additionally the argument of `andReturn` can be a function, which will be called each time the mock |
| 48 | +is triggered. |
| 49 | + |
| 50 | +The following examples describe how each action type can be mocked. |
| 51 | + |
| 52 | + |
| 53 | +### Fetch |
| 54 | + |
| 55 | +```js |
| 56 | +// This mock call |
| 57 | +mock('fetch', { filter: { id: 42 }}).for(User).andReturn(userData); |
| 58 | + |
| 59 | +// will be triggerd by |
| 60 | +User.fetch(42); |
| 61 | +``` |
| 62 | + |
| 63 | +### Persist |
| 64 | + |
| 65 | +```js |
| 66 | +// This mock call |
| 67 | +mock('persist', { id: 17 }).for(User).andReturn({ id: 17, name: 'Charlie Brown' }); |
| 68 | + |
| 69 | +// will be triggerd by |
| 70 | +user.$persist(); |
| 71 | +``` |
| 72 | + |
| 73 | +### Push |
| 74 | + |
| 75 | +```js |
| 76 | +// This mock call |
| 77 | +mock('push', { data: { ... } }).for(User).andReturn({ id: 17, name: 'Charlie Brown' }); |
| 78 | + |
| 79 | +// will be triggerd by |
| 80 | +user.$push(); |
| 81 | +``` |
| 82 | + |
| 83 | +### Destroy |
| 84 | + |
| 85 | +```js |
| 86 | +// This mock call |
| 87 | +mock('destroy', { id: 17 }).for(User).andReturn({ id: 17, name: 'Charlie Brown' }); |
| 88 | + |
| 89 | +// will be triggerd by |
| 90 | +user.$destroy(); |
| 91 | +``` |
| 92 | + |
| 93 | +### Query |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +### Mutate |
| 98 | + |
| 99 | +```js |
| 100 | +// This mock call |
| 101 | +mock('mutate', { name: 'upvote', args: { id: 4 }}).for(Post).andReturn({ ... }); |
| 102 | + |
| 103 | +// will be triggerd by |
| 104 | +post.$mutate({ name: 'upvote' }); |
| 105 | +``` |
| 106 | + |
| 107 | +### Simple Query |
| 108 | + |
| 109 | +Mocking simple queries works slightly different then the other actions, because these are not model |
| 110 | +related. Thus we have to mock these globally by omiting the model (`for`): |
| 111 | + |
| 112 | +```js |
| 113 | +// This mock call |
| 114 | +mock('simpleQuery', { name: 'example' }).andReturn({ success: true }); |
| 115 | + |
| 116 | +// will be triggered by |
| 117 | +store.dispatch('entities/simpleQuery', { query: 'query example { success }' }); |
| 118 | +``` |
| 119 | + |
| 120 | +### Simple Mutation |
| 121 | + |
| 122 | +Works just like the simple queries: |
| 123 | + |
| 124 | +```js |
| 125 | +// This mock call |
| 126 | +mock('simpleMutation', { |
| 127 | + name: 'SendSms', |
| 128 | + variables: { to: '+4912345678', text: 'GraphQL is awesome!' } |
| 129 | +}).andReturn({ sendSms: { delivered: true }}); |
| 130 | + |
| 131 | +// will be triggered by |
| 132 | +const query = ` |
| 133 | +mutation SendSms($to: string!, $text: string!) { |
| 134 | + sendSms(to: $to, text: $text) { |
| 135 | + delivered |
| 136 | + } |
| 137 | +}`; |
| 138 | + |
| 139 | +const result = await store.dispatch('entities/simpleMutation', { |
| 140 | + query, |
| 141 | + variables: { to: '+4912345678', text: 'GraphQL is awesome!' } |
| 142 | +}); |
| 143 | +``` |
| 144 | + |
| 145 | + |
| 146 | +### Resetting a mock |
| 147 | + |
| 148 | +::: warning |
| 149 | +Support for resetting a mock is currently work in progress and will be added soon. |
| 150 | + |
| 151 | +See https://github.com/vuex-orm/plugin-graphql/issues/61 |
| 152 | +::: |
| 153 | + |
| 154 | + |
| 155 | +## Misc |
| 156 | + |
| 157 | +The testing utils also provide some other useful functions, which are listed here: |
| 158 | + |
| 159 | +- `async clearORMStore()`: Will remove all records from the Vuex-ORM store to clean up while testing. |
| 160 | + |
| 161 | + |
| 162 | +## Integration Testing |
| 163 | + |
| 164 | +::: warning |
| 165 | +Support for integration testing is currently work in progress and will be added soon. |
| 166 | + |
| 167 | +See https://github.com/vuex-orm/plugin-graphql/issues/59 |
| 168 | +::: |
0 commit comments