-
Notifications
You must be signed in to change notification settings - Fork 15
/
plugins.spec.ts
129 lines (108 loc) · 3.03 KB
/
plugins.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import Post from './helpers/Post'
import AxiosMock from './helpers/AxiosMock'
import ApiResource from '../src/classes/ApiResource'
import ApiEndpoint from '../src/classes/ApiEndpoint'
import { resource, remap, data } from '../src/functions/plugins'
// ---------------------------------------------------------------------------------------------------------------------
// mocks
const dummy = {
id: 1,
userId: 2,
title: 'the title',
body: 'the body'
}
const axios = new AxiosMock()
axios.on('get', 'posts', [dummy, dummy, dummy])
axios.on('get', 'posts/1', dummy)
axios.on('post', 'posts/1', dummy)
axios.on('patch', 'posts/1', dummy)
axios.on('delete', 'posts/1', { success: 1})
// ---------------------------------------------------------------------------------------------------------------------
// tests
describe('axios mock', () => {
it('should return the mocked data', () => {
expect.assertions(1)
axios.get('posts/1').then(res => {
expect(res.data).toEqual(dummy)
})
})
})
describe('resource class', () => {
const posts = new ApiResource(axios, 'posts/:id', Post)
it('should transform inbound data to Post classes', () => {
expect.assertions(1)
return posts
.read(1)
.then(post => {
expect(post).toBeInstanceOf(Post)
})
})
it('should transform outbound Post classes to data', () => {
expect.assertions(1)
const post = new Post({ id: 1, title: 'the title' })
return posts
.update(post)
.then(post => {
expect(post.title).toBe('the title')
})
})
})
describe('resource plugin', () => {
const posts = new ApiEndpoint(axios, 'posts/:id')
resource(posts, Post)
it('should transform inbound data to Post classes', () => {
expect.assertions(1)
return posts
.read(1)
.then(post => {
expect(post).toBeInstanceOf(Post)
})
})
it('should transform outbound Post classes to data', () => {
expect.assertions(1)
const post = new Post({ id: 1, title: 'the title' })
return posts
.update(post)
.then(post => {
expect(post.title).toBe('the title')
})
})
})
describe('remap plugin', () => {
const data = {
id: 1,
the_userId: 2,
the_title: 'the title',
the_body: 'the body'
}
const map = {
the_id: 'id',
the_userId: 'userId',
the_title: 'title',
the_body: 'body',
}
const posts = new ApiEndpoint(axios, 'posts/:id')
remap(posts, map)
it('should remap keys in read operations', () => {
expect.assertions(1)
posts.update(data).then(res => {
expect(res.data.the_title).toBe('the title')
})
})
it('should remap keys in browse operations', () => {
expect.assertions(1)
posts.index().then(res => {
expect(res.data[0].the_title).toBe('the title')
})
})
})
describe('data plugin', () => {
const posts = new ApiEndpoint(axios, 'posts/:id')
data(posts)
it('should return the data object directly', () => {
expect.assertions(1)
posts.read(1).then(data => {
expect(data).toEqual(dummy)
})
})
})