|
| 1 | +import * as R from 'ramda'; |
1 | 2 | import { expect } from 'chai'; |
| 3 | +import { getAnotherObject, getSampleObject, getSignedUrl } from './env/factory'; |
2 | 4 | import { init } from './env/server'; |
3 | | - |
4 | | -const getSignedUrl = async (key: string) => (key ? `SIGNED_${key}` : ''); |
| 5 | +import { execPath } from 'process'; |
5 | 6 |
|
6 | 7 | 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 | + }); |
33 | 20 |
|
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, |
56 | 33 | }, |
57 | | - }, |
58 | | - }; |
| 34 | + }); |
| 35 | + expect(multiple.result).to.eql({ ...anotherUpdated, ...updated }); |
| 36 | + }); |
59 | 37 |
|
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({ |
62 | 46 | method: 'POST', |
63 | | - url: '/lenses', |
64 | | - payload: sampleImage, |
| 47 | + url: '/test', |
| 48 | + payload: [original, anotherOriginal], |
65 | 49 | }); |
| 50 | + expect(response.result).to.eql([updated, anotherUpdated]); |
| 51 | + }); |
66 | 52 |
|
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 | + }); |
68 | 60 |
|
69 | | - const fileImage = await server.inject({ |
| 61 | + const payload = { |
| 62 | + name: 'test name', |
| 63 | + documents: original, |
| 64 | + }; |
| 65 | + const response = await server.inject({ |
70 | 66 | method: 'POST', |
71 | | - url: '/lenses', |
72 | | - payload: { |
73 | | - ...sampleFile, |
74 | | - ...sampleImage, |
75 | | - }, |
| 67 | + url: '/test', |
| 68 | + payload: payload, |
76 | 69 | }); |
| 70 | + expect(response.result).to.eql({ ...payload, documents: updated }); |
| 71 | + }); |
77 | 72 |
|
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, |
81 | 80 | }); |
82 | | - }); |
83 | 81 |
|
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 | + }; |
85 | 86 | const response = await server.inject({ |
86 | 87 | method: 'POST', |
87 | | - url: '/lenses', |
88 | | - payload: [sampleImage, sampleFile, sampleImage, sampleFile], |
| 88 | + url: '/test', |
| 89 | + payload: payload, |
89 | 90 | }); |
90 | | - const result = response.result as any[]; |
| 91 | + expect(response.result).to.eql({ ...payload, documents: [updated, anotherUpdated] }); |
| 92 | + }); |
91 | 93 |
|
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 | + }, |
98 | 107 | ]); |
| 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 }); |
99 | 120 | }); |
100 | 121 |
|
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 | + }; |
102 | 143 | const response = await server.inject({ |
103 | 144 | 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], |
106 | 152 | }); |
107 | | - const result = response.result; |
108 | | - expect(result).to.eql(updatedNestedLevelOne); |
109 | 153 | }); |
110 | 154 |
|
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 | + |
112 | 162 | const response = await server.inject({ |
113 | 163 | method: 'POST', |
114 | | - url: '/nested/level-two', |
115 | | - payload: nestedLevelTwo, |
| 164 | + url: '/test', |
| 165 | + payload: original, |
116 | 166 | }); |
117 | | - const result = response.result; |
118 | | - expect(result).to.eql(updatedNestedLevelTwo); |
| 167 | + expect(response.result).to.eql(updated); |
119 | 168 | }); |
120 | 169 | }); |
0 commit comments