Skip to content

Commit cdb004e

Browse files
committed
massive test refactors, improve test coverage
1 parent 3fc7e49 commit cdb004e

File tree

3 files changed

+180
-133
lines changed

3 files changed

+180
-133
lines changed

test/env/factory.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as R from 'ramda';
2+
3+
const generateRandomString = (): string =>
4+
(Math.random() + 1).toString(36).substring(7).toString();
5+
6+
export const getSignedUrl = async (key: string) => (key ? `SIGNED_${key}` : '');
7+
8+
export const getSampleObject = async (): Promise<[any, object, object]> => {
9+
const data = {
10+
type: 'png',
11+
image: generateRandomString(),
12+
};
13+
return [
14+
R.lensProp<string, any>('image'),
15+
data,
16+
{
17+
...data,
18+
image: await getSignedUrl(data.image),
19+
},
20+
];
21+
};
22+
23+
export const getAnotherObject = async (): Promise<[any, object, object]> => {
24+
const data = {
25+
mime: 'png',
26+
file: generateRandomString(),
27+
};
28+
return [
29+
R.lensProp<string, any>('file'),
30+
data,
31+
{
32+
...data,
33+
file: await getSignedUrl(data.file),
34+
},
35+
];
36+
};

test/env/server.ts

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import * as hapi from '@hapi/hapi';
22
import { signedUrl } from '../../src/index';
3+
import { RouteOptions } from '../../src/lib/types';
34
import { fileSignFunction } from './types';
4-
import * as R from 'ramda';
55

6-
export const init = async (getSignedUrl: fileSignFunction) => {
6+
export const init = async (
7+
getSignedUrl: fileSignFunction,
8+
options: RouteOptions | RouteOptions[],
9+
) => {
710
const PORT = 4000;
811
const server = hapi.server({
912
port: PORT,
@@ -20,60 +23,19 @@ export const init = async (getSignedUrl: fileSignFunction) => {
2023
},
2124
]);
2225

23-
// defining custom lenses
24-
const fileLens = R.lensProp<string, any>('file');
25-
const imageLens = R.lensProp<string, any>('image');
26-
27-
const nestedLevelOnePath = R.lensPath(['data']);
28-
const nestedLevelTwoPath = R.lensPath(['data', 'images']);
29-
30-
server.route({
31-
method: 'POST',
32-
path: '/lenses',
33-
options: {
34-
handler: (request: hapi.Request, h: hapi.ResponseToolkit) => {
35-
return h.response(request.payload).code(200);
36-
},
37-
plugins: {
38-
signedUrl: {
39-
lenses: [fileLens, imageLens],
40-
},
41-
},
42-
},
43-
});
44-
26+
// creating a sample route to test
4527
server.route({
4628
method: 'POST',
47-
path: '/nested/level-one',
29+
path: '/test',
4830
options: {
4931
handler: (request: hapi.Request, h: hapi.ResponseToolkit) => {
5032
return h.response(request.payload).code(200);
5133
},
5234
plugins: {
53-
signedUrl: {
54-
lenses: [fileLens, imageLens],
55-
pathToSource: nestedLevelOnePath,
56-
},
35+
signedUrl: options,
5736
},
5837
},
5938
});
60-
61-
server.route({
62-
method: 'POST',
63-
path: '/nested/level-two',
64-
options: {
65-
handler: (request: hapi.Request, h: hapi.ResponseToolkit) => {
66-
return h.response(request.payload).code(200);
67-
},
68-
plugins: {
69-
signedUrl: {
70-
lenses: [fileLens, imageLens],
71-
pathToSource: nestedLevelTwoPath,
72-
},
73-
},
74-
},
75-
});
76-
7739
await server.initialize();
7840
return server;
7941
};

test/response.spec.ts

Lines changed: 136 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,169 @@
1+
import * as R from 'ramda';
12
import { expect } from 'chai';
3+
import { getAnotherObject, getSampleObject, getSignedUrl } from './env/factory';
24
import { init } from './env/server';
3-
4-
const getSignedUrl = async (key: string) => (key ? `SIGNED_${key}` : '');
5+
import { execPath } from 'process';
56

67
describe('Single object', async () => {
7-
const server = await init(getSignedUrl);
8-
const sampleImage = {
9-
type: 'png',
10-
image: 'image_id',
11-
};
12-
const sampleFile = {
13-
what: 'pdf',
14-
name: 'sample.pdf',
15-
file: 'file_id',
16-
};
17-
const nestedLevelOne = {
18-
name: 'some name',
19-
data: {
20-
...sampleImage,
21-
...sampleFile,
22-
},
23-
};
24-
const nestedLevelTwo = {
25-
name: 'some name',
26-
data: {
27-
images: {
28-
...sampleImage,
29-
...sampleFile,
30-
},
31-
},
32-
};
8+
it('should update the keys for a single value response', async () => {
9+
const [lens, original, updated] = await getSampleObject();
10+
let server = await init(getSignedUrl, {
11+
lenses: [lens],
12+
});
13+
const single = await server.inject({
14+
method: 'POST',
15+
url: '/test',
16+
payload: original,
17+
});
18+
expect(single.result).to.eql(updated);
19+
});
3320

34-
const updatedSampleImage = {
35-
type: 'png',
36-
image: await getSignedUrl(sampleImage.image),
37-
};
38-
const updatedSampleFile = {
39-
what: 'pdf',
40-
name: 'sample.pdf',
41-
file: await getSignedUrl(sampleFile.file),
42-
};
43-
const updatedNestedLevelOne = {
44-
name: 'some name',
45-
data: {
46-
...updatedSampleFile,
47-
...updatedSampleImage,
48-
},
49-
};
50-
const updatedNestedLevelTwo = {
51-
name: 'some name',
52-
data: {
53-
images: {
54-
...updatedSampleFile,
55-
...updatedSampleImage,
21+
it('should update the keys for a multiple value response', async () => {
22+
const [lens, original, updated] = await getSampleObject();
23+
const [anotherLens, anotherOriginal, anotherUpdated] = await getAnotherObject();
24+
const server = await init(getSignedUrl, {
25+
lenses: [lens, anotherLens],
26+
});
27+
const multiple = await server.inject({
28+
method: 'POSt',
29+
url: '/test',
30+
payload: {
31+
...original,
32+
...anotherOriginal,
5633
},
57-
},
58-
};
34+
});
35+
expect(multiple.result).to.eql({ ...anotherUpdated, ...updated });
36+
});
5937

60-
it('should update the keys for a single value response', async () => {
61-
const empty = await server.inject({
38+
it('should update the keys for a array of values in response', async () => {
39+
const [lens, original, updated] = await getSampleObject();
40+
const [anotherLens, anotherOriginal, anotherUpdated] = await getAnotherObject();
41+
const server = await init(getSignedUrl, {
42+
lenses: [lens, anotherLens],
43+
});
44+
45+
const response = await server.inject({
6246
method: 'POST',
63-
url: '/lenses',
64-
payload: sampleImage,
47+
url: '/test',
48+
payload: [original, anotherOriginal],
6549
});
50+
expect(response.result).to.eql([updated, anotherUpdated]);
51+
});
6652

67-
expect(empty.result).to.eql(updatedSampleImage);
53+
it('should update the keys for a nested single object', async () => {
54+
const [lens, original, updated] = await getSampleObject();
55+
const path = R.lensPath(['documents']);
56+
const server = await init(getSignedUrl, {
57+
lenses: [lens],
58+
pathToSource: path,
59+
});
6860

69-
const fileImage = await server.inject({
61+
const payload = {
62+
name: 'test name',
63+
documents: original,
64+
};
65+
const response = await server.inject({
7066
method: 'POST',
71-
url: '/lenses',
72-
payload: {
73-
...sampleFile,
74-
...sampleImage,
75-
},
67+
url: '/test',
68+
payload: payload,
7669
});
70+
expect(response.result).to.eql({ ...payload, documents: updated });
71+
});
7772

78-
expect(fileImage.result).to.eql({
79-
...updatedSampleImage,
80-
...updatedSampleFile,
73+
it('should update the keys for a nested multiple objects', async () => {
74+
const [lens, original, updated] = await getSampleObject();
75+
const [anotherLens, anotherOriginal, anotherUpdated] = await getAnotherObject();
76+
const path = R.lensPath(['documents']);
77+
const server = await init(getSignedUrl, {
78+
lenses: [lens, anotherLens],
79+
pathToSource: path,
8180
});
82-
});
8381

84-
it('should update the keys for a array of values in response', async () => {
82+
const payload = {
83+
name: 'test name',
84+
documents: [original, anotherOriginal],
85+
};
8586
const response = await server.inject({
8687
method: 'POST',
87-
url: '/lenses',
88-
payload: [sampleImage, sampleFile, sampleImage, sampleFile],
88+
url: '/test',
89+
payload: payload,
8990
});
90-
const result = response.result as any[];
91+
expect(response.result).to.eql({ ...payload, documents: [updated, anotherUpdated] });
92+
});
9193

92-
expect(result.length).to.eql(4);
93-
expect(result).to.eql([
94-
updatedSampleImage,
95-
updatedSampleFile,
96-
updatedSampleImage,
97-
updatedSampleFile,
94+
it('should update the keys for a nested single object with multiple sources', async () => {
95+
const [lens, original, updated] = await getSampleObject();
96+
const path = R.lensPath(['documents']);
97+
const anotherPath = R.lensPath(['other']);
98+
const server = await init(getSignedUrl, [
99+
{
100+
lenses: [lens],
101+
pathToSource: path,
102+
},
103+
{
104+
lenses: [lens],
105+
pathToSource: anotherPath,
106+
},
98107
]);
108+
109+
const payload = {
110+
name: 'test name',
111+
documents: original,
112+
other: original,
113+
};
114+
const response = await server.inject({
115+
method: 'POST',
116+
url: '/test',
117+
payload: payload,
118+
});
119+
expect(response.result).to.eql({ ...payload, documents: updated, other: updated });
99120
});
100121

101-
it('should update the keys for a nested object in response', async () => {
122+
it('should update the keys for a nested multiple objects with multiple sources', async () => {
123+
const [lens, original, updated] = await getSampleObject();
124+
const [anotherLens, anotherOriginal, anotherUpdated] = await getAnotherObject();
125+
const path = R.lensPath(['documents']);
126+
const anotherPath = R.lensPath(['other']);
127+
const server = await init(getSignedUrl, [
128+
{
129+
lenses: [lens, anotherLens],
130+
pathToSource: path,
131+
},
132+
{
133+
lenses: [lens, anotherLens],
134+
pathToSource: anotherPath,
135+
},
136+
]);
137+
138+
const payload = {
139+
name: 'test name',
140+
documents: [original, anotherOriginal],
141+
other: [original, anotherOriginal],
142+
};
102143
const response = await server.inject({
103144
method: 'POST',
104-
url: '/nested/level-one',
105-
payload: nestedLevelOne,
145+
url: '/test',
146+
payload: payload,
147+
});
148+
expect(response.result).to.eql({
149+
...payload,
150+
documents: [updated, anotherUpdated],
151+
other: [updated, anotherUpdated],
106152
});
107-
const result = response.result;
108-
expect(result).to.eql(updatedNestedLevelOne);
109153
});
110154

111-
it('should update the keys for a nested two layers object in response', async () => {
155+
it('should update the keys for a missing key from the lenses', async () => {
156+
const [lens, original, updated] = await getSampleObject();
157+
const [anotherLens, anotherOriginal, anotherUpdated] = await getAnotherObject();
158+
const server = await init(getSignedUrl, {
159+
lenses: [lens, anotherLens], // adding another lens but not the object
160+
});
161+
112162
const response = await server.inject({
113163
method: 'POST',
114-
url: '/nested/level-two',
115-
payload: nestedLevelTwo,
164+
url: '/test',
165+
payload: original,
116166
});
117-
const result = response.result;
118-
expect(result).to.eql(updatedNestedLevelTwo);
167+
expect(response.result).to.eql(updated);
119168
});
120169
});

0 commit comments

Comments
 (0)