-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.test.js
154 lines (140 loc) · 3.46 KB
/
tasks.test.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
const tasks = require('./tasks');
describe('Report tests', () => {
test('should handle incorrect payload', () => {
const output = tasks.report([], {});
expect(output).toEqual({
ok: false,
message: 'No email specified'
});
});
test('should validate email', () => {
const payload = {
email: 'baran'
};
const output = tasks.report([], payload);
expect(output).toEqual({
ok: false,
message: 'invalid email'
});
});
test('should send an email', () => {
const payload = {
email: '[email protected]'
};
const output = tasks.report([], payload);
expect(output).toMatchObject({
ok: true,
data: []
});
});
});
describe('Rename Tests', () => {
test('should require payload', () => {
const output = tasks.rename([]);
expect(output).toMatchObject({
ok: false,
message: 'payload required',
});
});
test('should handle malformed payload', () => {
const payload = {};
const output = tasks.rename([], payload);
expect(output).toMatchObject({
ok: false,
message: 'malformed payload',
});
});
test('should rename field correctly', () => {
const payload = {
from: 'foo',
to: 'bar'
};
const data = [{
foo: 'test me',
id: 1
}, {
name: '45',
id: 2
}];
const expectedData = [{
bar: 'test me',
id: 1
}, {
name: '45',
id: 2
}];
const output = tasks.rename(data, payload);
expect(output.data).toEqual(expectedData);
});
});
describe('Filter tests', () => {
test('should require payload', () => {
const output = tasks.filter([]);
expect(output).toMatchObject({
ok: false,
message: 'payload required',
});
});
test('should handle malformed payload', () => {
const payload = {};
const output = tasks.filter([], payload);
expect(output).toMatchObject({
ok: false,
message: 'malformed payload',
});
});
test('should filter fields correctly', () => {
const payload = ['foo'];
const data = [{
foo: 'test me',
id: 1
}, {
name: '45',
id: 2
}];
const expectedData = [{
foo: 'test me',
}, {
}];
const output = tasks.filter(data, payload);
expect(output.ok).toBe(true);
expect(output.data).toEqual(expectedData);
});
});
describe('Median tests', () => {
test('should require payload', () => {
const output = tasks.median([]);
expect(output).toMatchObject({
ok: false,
message: 'payload required',
});
});
test('should handle malformed payload', () => {
const payload = '45';
const output = tasks.median([], payload);
expect(output).toMatchObject({
ok: false,
message: 'malformed payload',
});
});
test('should find median', () => {
const payload = {
field: 'age'
};
const data = [{ age: 45 }, { age: 35 }, { age: 25 }, { age: 10 }];
const output = tasks.median(data, payload);
const expectedData = [{age: 28.75}];
expect(output.ok).toBe(true);
expect(output.data).toEqual(expectedData);
});
test('should find median with malformed data', () => {
const payload = {
field: 'age'
};
const data = [{ age: 45 }, { age: 'foo', name: 'baran' }, { age: 25 }, { age: 10 }];
const output = tasks.median(data, payload);
const expectedData = [{age: 26.67}];
expect(output.ok).toBe(true);
expect(output.data).toEqual(expectedData);
});
});