This repository has been archived by the owner on Feb 4, 2021. It is now read-only.
/
mutation.spec.js
175 lines (149 loc) Β· 4.44 KB
/
mutation.spec.js
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import './setupAndTearDown';
import { mutate, findOneByValue } from './db';
import {
asUser,
removeDynamicData,
removeListDynamicData,
removeId,
} from './testUtils';
import { BoardMember } from './models/BoardMember';
import { Board } from './models/Board';
import { Book } from './models/Book';
describe('mutation', () => {
it('reject mutations without state transitions on stateful entities', async () => {
const payload = {
board: 50,
};
await mutate(BoardMember, 'create', payload, null, asUser(99)).catch(e => {
expect(e).toMatchSnapshot();
});
});
it('perform create mutations', async () => {
const payload = {
board: 47,
};
const result = await mutate(BoardMember, 'join', payload, null, asUser(99));
expect(removeDynamicData(BoardMember, result)).toMatchSnapshot();
});
it('perform create mutations with nested JSON attributes', async () => {
const payload = {
title: 'War and Peace',
author: 'Leo Tolstoy',
reviews: [
{
reviewer: 'John Connor',
reviewText: "Couldn't stop reading",
bookAttributes: [
{
attribute: 'Year of publishing',
value: '1867',
},
{
attribute: 'Pages',
value: '1225',
},
],
},
],
};
const result = await mutate(Book, 'create', payload, null, asUser(99));
expect(removeId(result)).toMatchSnapshot();
});
it('perform update mutations', async () => {
const payload = {
board: 50,
invitee: 80,
};
let result = await mutate(BoardMember, 'invite', payload, null, asUser(84));
const inviteId = result.id;
expect(removeDynamicData(BoardMember, result)).toMatchSnapshot();
result = await mutate(BoardMember, 'accept', {}, inviteId, asUser(80));
expect(removeDynamicData(BoardMember, result)).toMatchSnapshot();
});
it('perform update mutations with nested JSON attributes', async () => {
const book = await findOneByValue(
Book,
{ author: 'Leo Tolstoy' },
asUser(99),
);
const payload = {
reviews: [
{
reviewer: 'John Connor',
reviewText: 'Updated review',
bookAttributes: [
{
attribute: 'Year of publishing',
value: '1867',
},
{
attribute: 'Pages',
value: '1225',
},
{
attribute: 'Pages',
value: '1225',
},
{
attribute: 'Genre',
value: 'Novel',
},
],
},
],
};
const result = await mutate(Book, 'update', payload, book.id, asUser(99));
expect(removeId(result)).toMatchSnapshot();
});
it('handle uniqueness constraints', async () => {
const payload = {
name: 'New Board',
isPrivate: false,
vip: 1,
};
await mutate(Board, 'build', payload, null, asUser(99));
await mutate(Board, 'build', payload, null, asUser(99)).catch(e => {
expect(e).toMatchSnapshot();
});
});
it('perform delete mutations', async () => {
const payload = {
board: 50,
invitee: 81,
};
let result = await mutate(BoardMember, 'invite', payload, null, asUser(84));
const inviteId = result.id;
expect(removeDynamicData(BoardMember, result)).toMatchSnapshot();
result = await mutate(BoardMember, 'remove', {}, inviteId, asUser(84));
result.rows = removeListDynamicData(BoardMember, result.rows);
expect(removeDynamicData(BoardMember, result)).toMatchSnapshot();
});
it('fail mutation if fromState is not a match', async () => {
const payload = {
board: 50,
invitee: 81,
};
const result = await mutate(
BoardMember,
'invite',
payload,
null,
asUser(84),
);
const inviteId = result.id;
await mutate(BoardMember, 'accept', {}, inviteId, asUser(81));
await mutate(BoardMember, 'accept', {}, inviteId, asUser(81)).catch(e => {
expect(e).toMatchSnapshot();
});
});
it('reject mutations if ID not found', async () => {
await mutate(BoardMember, 'remove', {}, 99999, asUser(80)).catch(e => {
expect(e).toMatchSnapshot();
});
});
it('reject unknown mutations', async () => {
await mutate(BoardMember, 'findInnerPeace', {}, 1, asUser(80)).catch(e => {
expect(e).toMatchSnapshot();
});
});
});