-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from vuex-orm/response-save-method
Add the save method to the response object
- Loading branch information
Showing
9 changed files
with
286 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--- | ||
sidebarDepth: 2 | ||
--- | ||
|
||
# Response | ||
|
||
The Response object is what gets returned when you make API call via Request object. | ||
|
||
## Instance Properties | ||
|
||
### response | ||
|
||
- **`response: AxiosResponse`** | ||
|
||
Please refer to the [Axios documentation](https://github.com/axios/axios#response-schema) for more details. | ||
|
||
### entities | ||
|
||
- **`entities: Collections | null`** | ||
|
||
The result of Vuex ORM persistent method. | ||
|
||
### isSaved | ||
|
||
- **`isSaved: boolean`** | ||
|
||
Whether the response data is persisted to the store or not. | ||
|
||
### model | ||
|
||
- **`model: typeof Model`** | ||
|
||
The Model class that was attached to the Request instance when making an API call. | ||
|
||
### config | ||
|
||
- **`config: Config`** | ||
|
||
The configuration which was passed to the Request instance. | ||
|
||
## Instance Methods | ||
|
||
### save | ||
|
||
- **`save (): Promise<void>`** | ||
|
||
Save response data to the store. | ||
|
||
### delete | ||
|
||
- **`delete (): Promise<void>`** | ||
|
||
Delete store record depending on `delete` option. If the `delete` option is not specified at the config, it will throw an error. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import axios from 'axios' | ||
import MockAdapter from 'axios-mock-adapter' | ||
import { createStore, createState } from 'test/support/Helpers' | ||
import { Model, Fields } from '@vuex-orm/core' | ||
|
||
describe('Feature - Response - Save', () => { | ||
let mock: MockAdapter | ||
|
||
class User extends Model { | ||
static entity = 'users' | ||
|
||
static fields (): Fields { | ||
return { | ||
id: this.attr(null), | ||
name: this.attr('') | ||
} | ||
} | ||
} | ||
|
||
beforeEach(() => { mock = new MockAdapter(axios) }) | ||
afterEach(() => { mock.reset() }) | ||
|
||
it('can save response data afterword', async () => { | ||
mock.onGet('/api/users').reply(200, { id: 1, name: 'John Doe' }) | ||
|
||
const store = createStore([User]) | ||
|
||
const response = await User.api().get('/api/users') | ||
|
||
const expected1 = createState({ | ||
users: { | ||
1: { $id: 1, id: 1, name: 'John Doe' } | ||
} | ||
}) | ||
|
||
expect(store.state.entities).toEqual(expected1) | ||
|
||
response.config.delete = 1 | ||
|
||
await response.delete() | ||
|
||
const expected2 = createState({ | ||
users: {} | ||
}) | ||
|
||
expect(store.state.entities).toEqual(expected2) | ||
}) | ||
|
||
it('throws error if `delete` option is not set', async () => { | ||
mock.onGet('/api/users').reply(200, { id: 1, name: 'John Doe' }) | ||
|
||
createStore([User]) | ||
|
||
const response = await User.api().get('/api/users') | ||
|
||
try { | ||
await response.delete() | ||
} catch (e) { | ||
expect(e.message).toBe('[Vuex ORM Axios] Could not delete records because the `delete` option is not set.') | ||
|
||
return | ||
} | ||
|
||
throw new Error('Error was not thrown') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import axios from 'axios' | ||
import MockAdapter from 'axios-mock-adapter' | ||
import { createStore, createState } from 'test/support/Helpers' | ||
import { Model, Fields } from '@vuex-orm/core' | ||
|
||
describe('Feature - Response - Save', () => { | ||
let mock: MockAdapter | ||
|
||
class User extends Model { | ||
static entity = 'users' | ||
|
||
static fields (): Fields { | ||
return { | ||
id: this.attr(null), | ||
name: this.attr('') | ||
} | ||
} | ||
} | ||
|
||
beforeEach(() => { mock = new MockAdapter(axios) }) | ||
afterEach(() => { mock.reset() }) | ||
|
||
it('can save response data afterword', async () => { | ||
mock.onGet('/api/users').reply(200, { id: 1, name: 'John Doe' }) | ||
|
||
const store = createStore([User]) | ||
|
||
const response = await User.api().get('/api/users', { save: false }) | ||
|
||
const expected1 = createState({ | ||
users: {} | ||
}) | ||
|
||
expect(store.state.entities).toEqual(expected1) | ||
|
||
await response.save() | ||
|
||
const expected2 = createState({ | ||
users: { | ||
1: { $id: 1, id: 1, name: 'John Doe' } | ||
} | ||
}) | ||
|
||
expect(store.state.entities).toEqual(expected2) | ||
}) | ||
|
||
it('sets `isSaved` flag', async () => { | ||
mock.onGet('/api/users').reply(200, { id: 1, name: 'John Doe' }) | ||
|
||
createStore([User]) | ||
|
||
const response = await User.api().get('/api/users', { save: false }) | ||
|
||
expect(response.isSaved).toEqual(false) | ||
|
||
await response.save() | ||
|
||
expect(response.isSaved).toEqual(true) | ||
}) | ||
}) |