Skip to content

Commit 91280eb

Browse files
committed
refactor(tests): asserting states
1 parent 73d7a06 commit 91280eb

9 files changed

+74
-110
lines changed

test/feature/Request.spec.ts

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import axios from 'axios'
22
import MockAdapter from 'axios-mock-adapter'
3-
import { createStore, createState } from 'test/support/Helpers'
4-
import { Model, Fields } from '@vuex-orm/core'
3+
import { createStore, assertState } from 'test/support/Helpers'
4+
import { Model } from '@vuex-orm/core'
55

66
describe('Feature - Request', () => {
77
let mock: MockAdapter
88

99
class User extends Model {
1010
static entity = 'users'
1111

12-
static fields(): Fields {
12+
static fields() {
1313
return {
1414
id: this.attr(null),
1515
name: this.attr('')
@@ -34,14 +34,12 @@ describe('Feature - Request', () => {
3434

3535
await User.api().get('/api/users')
3636

37-
const expected = createState({
37+
assertState(store, {
3838
users: {
3939
1: { $id: '1', id: 1, name: 'John Doe' },
4040
2: { $id: '2', id: 2, name: 'Jane Doe' }
4141
}
4242
})
43-
44-
expect(store.state.entities).toEqual(expected)
4543
})
4644

4745
it('`get` can perform a get request with additional config', async () => {
@@ -56,14 +54,12 @@ describe('Feature - Request', () => {
5654

5755
await User.api().get('/api/users', { dataKey: 'data' })
5856

59-
const expected = createState({
57+
assertState(store, {
6058
users: {
6159
1: { $id: '1', id: 1, name: 'John Doe' },
6260
2: { $id: '2', id: 2, name: 'Jane Doe' }
6361
}
6462
})
65-
66-
expect(store.state.entities).toEqual(expected)
6763
})
6864

6965
it('`post` can perform a post request', async () => {
@@ -76,14 +72,12 @@ describe('Feature - Request', () => {
7672

7773
await User.api().post('/api/users')
7874

79-
const expected = createState({
75+
assertState(store, {
8076
users: {
8177
1: { $id: '1', id: 1, name: 'John Doe' },
8278
2: { $id: '2', id: 2, name: 'Jane Doe' }
8379
}
8480
})
85-
86-
expect(store.state.entities).toEqual(expected)
8781
})
8882

8983
it('`post` can perform a post request with additional config', async () => {
@@ -98,14 +92,12 @@ describe('Feature - Request', () => {
9892

9993
await User.api().post('/api/users', {}, { dataKey: 'data' })
10094

101-
const expected = createState({
95+
assertState(store, {
10296
users: {
10397
1: { $id: '1', id: 1, name: 'John Doe' },
10498
2: { $id: '2', id: 2, name: 'Jane Doe' }
10599
}
106100
})
107-
108-
expect(store.state.entities).toEqual(expected)
109101
})
110102

111103
it('`put` can perform a put request', async () => {
@@ -118,14 +110,12 @@ describe('Feature - Request', () => {
118110

119111
await User.api().put('/api/users')
120112

121-
const expected = createState({
113+
assertState(store, {
122114
users: {
123115
1: { $id: '1', id: 1, name: 'John Doe' },
124116
2: { $id: '2', id: 2, name: 'Jane Doe' }
125117
}
126118
})
127-
128-
expect(store.state.entities).toEqual(expected)
129119
})
130120

131121
it('`put` can perform a put request with additional config', async () => {
@@ -140,14 +130,12 @@ describe('Feature - Request', () => {
140130

141131
await User.api().put('/api/users', {}, { dataKey: 'data' })
142132

143-
const expected = createState({
133+
assertState(store, {
144134
users: {
145135
1: { $id: '1', id: 1, name: 'John Doe' },
146136
2: { $id: '2', id: 2, name: 'Jane Doe' }
147137
}
148138
})
149-
150-
expect(store.state.entities).toEqual(expected)
151139
})
152140

153141
it('`patch` can perform a patch request', async () => {
@@ -160,14 +148,12 @@ describe('Feature - Request', () => {
160148

161149
await User.api().patch('/api/users')
162150

163-
const expected = createState({
151+
assertState(store, {
164152
users: {
165153
1: { $id: '1', id: 1, name: 'John Doe' },
166154
2: { $id: '2', id: 2, name: 'Jane Doe' }
167155
}
168156
})
169-
170-
expect(store.state.entities).toEqual(expected)
171157
})
172158

173159
it('`patch` can perform a patch request with additional config', async () => {
@@ -182,14 +168,12 @@ describe('Feature - Request', () => {
182168

183169
await User.api().patch('/api/users', {}, { dataKey: 'data' })
184170

185-
const expected = createState({
171+
assertState(store, {
186172
users: {
187173
1: { $id: '1', id: 1, name: 'John Doe' },
188174
2: { $id: '2', id: 2, name: 'Jane Doe' }
189175
}
190176
})
191-
192-
expect(store.state.entities).toEqual(expected)
193177
})
194178

195179
it('`delete` can perform a delete request', async () => {
@@ -202,14 +186,12 @@ describe('Feature - Request', () => {
202186

203187
await User.api().delete('/api/users')
204188

205-
const expected = createState({
189+
assertState(store, {
206190
users: {
207191
1: { $id: '1', id: 1, name: 'John Doe' },
208192
2: { $id: '2', id: 2, name: 'Jane Doe' }
209193
}
210194
})
211-
212-
expect(store.state.entities).toEqual(expected)
213195
})
214196

215197
it('`delete` can perform a delete request with additional config', async () => {
@@ -224,13 +206,11 @@ describe('Feature - Request', () => {
224206

225207
await User.api().delete('/api/users', { dataKey: 'data' })
226208

227-
const expected = createState({
209+
assertState(store, {
228210
users: {
229211
1: { $id: '1', id: 1, name: 'John Doe' },
230212
2: { $id: '2', id: 2, name: 'Jane Doe' }
231213
}
232214
})
233-
234-
expect(store.state.entities).toEqual(expected)
235215
})
236216
})

test/feature/Request_Actions.spec.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import axios from 'axios'
22
import MockAdapter from 'axios-mock-adapter'
3-
import { createStore, createState } from 'test/support/Helpers'
4-
import { Model, Fields } from '@vuex-orm/core'
5-
import Request from '@/api/Request'
6-
import Response from '@/api/Response'
3+
import { createStore, assertState } from 'test/support/Helpers'
4+
import { Model } from '@vuex-orm/core'
5+
import { Request, Response } from '@/index'
76

87
describe('Feature - Request - Actions', () => {
98
let mock: MockAdapter
@@ -19,7 +18,7 @@ describe('Feature - Request - Actions', () => {
1918
class User extends Model {
2019
static entity = 'users'
2120

22-
static fields(): Fields {
21+
static fields() {
2322
return {
2423
id: this.attr(null),
2524
name: this.attr('')
@@ -39,20 +38,18 @@ describe('Feature - Request - Actions', () => {
3938

4039
await User.api().fetch()
4140

42-
const expected = createState({
41+
assertState(store, {
4342
users: {
4443
1: { $id: '1', id: 1, name: 'John' }
4544
}
4645
})
47-
48-
expect(store.state.entities).toEqual(expected)
4946
})
5047

5148
it('can define a custom action as a function', async () => {
5249
class User extends Model {
5350
static entity = 'users'
5451

55-
static fields(): Fields {
52+
static fields() {
5653
return {
5754
id: this.attr(null),
5855
name: this.attr('')
@@ -74,12 +71,10 @@ describe('Feature - Request - Actions', () => {
7471

7572
await User.api().fetch('/users')
7673

77-
const expected = createState({
74+
assertState(store, {
7875
users: {
7976
1: { $id: '1', id: 1, name: 'John' }
8077
}
8178
})
82-
83-
expect(store.state.entities).toEqual(expected)
8479
})
8580
})

test/feature/Request_DataKey.spec.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import axios from 'axios'
22
import MockAdapter from 'axios-mock-adapter'
3-
import { createStore, createState } from 'test/support/Helpers'
4-
import { Model, Fields } from '@vuex-orm/core'
3+
import { createStore, assertState } from 'test/support/Helpers'
4+
import { Model } from '@vuex-orm/core'
55

66
describe('Feature - Request - Data Key', () => {
77
let mock: MockAdapter
88

99
class User extends Model {
1010
static entity = 'users'
1111

12-
static fields(): Fields {
12+
static fields() {
1313
return {
1414
id: this.attr(null),
1515
name: this.attr('')
@@ -24,7 +24,7 @@ describe('Feature - Request - Data Key', () => {
2424
mock.reset()
2525
})
2626

27-
it('can specify which key to look for the data', async () => {
27+
it('can specify which resource key to extract data from', async () => {
2828
mock.onGet('/users').reply(200, {
2929
data: { id: 1, name: 'John Doe' }
3030
})
@@ -36,12 +36,10 @@ describe('Feature - Request - Data Key', () => {
3636
dataKey: 'data'
3737
})
3838

39-
const expected = createState({
39+
assertState(store, {
4040
users: {
4141
1: { $id: '1', id: 1, name: 'John Doe' }
4242
}
4343
})
44-
45-
expect(store.state.entities).toEqual(expected)
4644
})
4745
})

test/feature/Request_DataTransformer.spec.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import axios from 'axios'
22
import MockAdapter from 'axios-mock-adapter'
3-
import { createStore, createState } from 'test/support/Helpers'
4-
import { Model, Fields } from '@vuex-orm/core'
3+
import { createStore, assertState } from 'test/support/Helpers'
4+
import { Model } from '@vuex-orm/core'
55

66
describe('Feature - Request - Data Transformer', () => {
77
let mock: MockAdapter
88

99
class User extends Model {
1010
static entity = 'users'
1111

12-
static fields(): Fields {
12+
static fields() {
1313
return {
1414
id: this.attr(null),
1515
name: this.attr('')
@@ -24,7 +24,7 @@ describe('Feature - Request - Data Transformer', () => {
2424
mock.reset()
2525
})
2626

27-
it('can specify callback to transform the response', async () => {
27+
it('can specify a callback to transform the response', async () => {
2828
mock.onGet('/users').reply(200, {
2929
data: { id: 1, name: 'John Doe' }
3030
})
@@ -36,12 +36,10 @@ describe('Feature - Request - Data Transformer', () => {
3636
dataTransformer: ({ data }) => data.data
3737
})
3838

39-
const expected = createState({
39+
assertState(store, {
4040
users: {
4141
1: { $id: '1', id: 1, name: 'John Doe' }
4242
}
4343
})
44-
45-
expect(store.state.entities).toEqual(expected)
4644
})
4745
})

test/feature/Request_Delete.spec.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import axios from 'axios'
22
import MockAdapter from 'axios-mock-adapter'
3-
import { createStore, createState } from 'test/support/Helpers'
4-
import { Model, Fields } from '@vuex-orm/core'
3+
import { createStore, assertState } from 'test/support/Helpers'
4+
import { Model } from '@vuex-orm/core'
55

66
describe('Feature - Request - Delete', () => {
77
let mock: MockAdapter
88

99
class User extends Model {
1010
static entity = 'users'
1111

12-
static fields(): Fields {
12+
static fields() {
1313
return {
1414
id: this.attr(null),
1515
name: this.attr('')
@@ -24,7 +24,7 @@ describe('Feature - Request - Delete', () => {
2424
mock.reset()
2525
})
2626

27-
it('can delete record after the api call', async () => {
27+
it('can delete a record after the api call', async () => {
2828
mock.onDelete('/users/1').reply(200, { ok: true })
2929

3030
const store = createStore([User])
@@ -42,12 +42,10 @@ describe('Feature - Request - Delete', () => {
4242
delete: 1
4343
})
4444

45-
const expected = createState({
45+
assertState(store, {
4646
users: {
4747
2: { $id: '2', id: 2, name: 'Jane' }
4848
}
4949
})
50-
51-
expect(store.state.entities).toEqual(expected)
5250
})
5351
})

0 commit comments

Comments
 (0)