This repository was archived by the owner on Aug 28, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathSchemaEntry.ts
177 lines (146 loc) · 4.56 KB
/
SchemaEntry.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
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
176
177
/* eslint-disable @typescript-eslint/ban-ts-comment, dot-notation */
import ava from 'ava';
import { createClient } from './lib/SettingsClient';
import {
Schema,
SchemaEntry
} from '../src';
ava('SchemaEntry Properties', (test): void => {
test.plan(13);
const schema = new Schema();
const schemaEntry = new SchemaEntry(schema, 'test', 'textchannel');
test.is(schemaEntry.client, null);
test.is(schemaEntry.key, 'test');
test.is(schemaEntry.type, 'textchannel');
test.is(schemaEntry.parent, schema);
test.is(schemaEntry.array, false);
test.is(schemaEntry.default, null);
test.is(schemaEntry.filter, null);
test.is(schemaEntry.inclusive, false);
test.is(schemaEntry.maximum, null);
test.is(schemaEntry.minimum, null);
test.is(schemaEntry.shouldResolve, true);
test.throws(() => schemaEntry.serializer, { instanceOf: Error });
test.deepEqual(schemaEntry.toJSON(), {
array: false,
default: null,
inclusive: false,
maximum: null,
minimum: null,
resolve: true,
type: 'textchannel'
});
});
ava('SchemaEntry#edit', (test): void => {
test.plan(7);
const schema = new Schema();
const schemaEntry = new SchemaEntry(schema, 'test', 'textchannel', {
array: false,
default: 1,
filter: (): boolean => true,
inclusive: false,
maximum: 100,
minimum: 98,
resolve: false
});
schemaEntry.edit({
type: 'guild',
array: true,
default: [1],
filter: null,
inclusive: true,
maximum: 200,
minimum: 100,
resolve: true
});
test.is(schemaEntry.type, 'guild');
test.is(schemaEntry.array, true);
test.is(schemaEntry.filter, null);
test.is(schemaEntry.shouldResolve, true);
test.is(schemaEntry.maximum, 200);
test.is(schemaEntry.minimum, 100);
test.deepEqual(schemaEntry.default, [1]);
});
ava('SchemaEntry#check', (test): void => {
test.plan(10);
const client = createClient();
const schema = new Schema();
const schemaEntry = new SchemaEntry(schema, 'test', 'textchannel');
const throwsCheck = (): void => schemaEntry._check();
// #region Client
// No client
test.throws(throwsCheck, { message: /Cannot retrieve serializers/i });
schemaEntry.client = client;
// #endregion
// #region Type
// @ts-ignore
schemaEntry.type = null;
test.throws(throwsCheck, { message: /Parameter 'type' must be a string/i });
schemaEntry.type = 'totallyaserializerpleasebelieveme';
test.throws(throwsCheck, { message: /is not a valid type/i });
// Reset to a valid type
schemaEntry.type = 'string';
// #endregion
// #region Booleans
// @ts-expect-error
schemaEntry.array = 'true';
test.throws(throwsCheck, { message: /Parameter 'array' must be a boolean/i });
schemaEntry.array = false;
// @ts-expect-error
schemaEntry.minimum = '123';
test.throws(throwsCheck, { message: /Parameter 'minimum' must be a number or null/i });
schemaEntry.minimum = 123;
// @ts-expect-error
schemaEntry.maximum = '100';
test.throws(throwsCheck, { message: /Parameter 'maximum' must be a number or null/i });
schemaEntry.maximum = 100;
test.throws(throwsCheck, { message: /Parameter 'minimum' must contain a value lower than the parameter 'maximum'/i });
schemaEntry.maximum = 200;
// #endregion
// @ts-expect-error
schemaEntry.filter = 'true';
test.throws(throwsCheck, { message: /Parameter 'filter' must be a function/i });
schemaEntry.filter = null;
// Checking if the default is an array and the type is an array
schemaEntry.array = true;
schemaEntry.default = null;
test.throws(throwsCheck, { message: /Default key must be an array if the key stores an array/i });
// Checking if the type is a string, but the default isn't
schemaEntry.array = false;
schemaEntry.type = 'string';
schemaEntry.default = true;
test.throws(throwsCheck, { message: /Default key must be a/i });
});
ava('SchemaEntry#toJSON', (test): void => {
test.plan(1);
const schema = new Schema();
const schemaEntry = new SchemaEntry(schema, 'test', 'textchannel', {
array: true,
default: [],
inclusive: true,
maximum: 1000,
minimum: 100,
resolve: true
});
const json = schemaEntry.toJSON();
test.deepEqual(json, {
type: 'textchannel',
array: true,
default: [],
inclusive: true,
maximum: 1000,
minimum: 100,
resolve: true
});
});
ava('SchemaEntry#default (Automatic)', (test): void => {
test.plan(3);
const schema = new Schema();
const schemaEntry = new SchemaEntry(schema, 'test', 'textchannel');
const generateDefault = (): unknown => schemaEntry['_generateDefaultValue']();
test.is(generateDefault(), null);
schemaEntry.edit({ array: true });
test.deepEqual(generateDefault(), []);
schemaEntry.edit({ array: false, type: 'boolean' });
test.is(generateDefault(), false);
});