Skip to content

Commit 1180f16

Browse files
Unit test linking and unlinking attributions
Signed-off-by: Markus Obendrauf <markusobendrauf@fb.com>
1 parent 635fb82 commit 1180f16

File tree

1 file changed

+191
-18
lines changed

1 file changed

+191
-18
lines changed

src/Frontend/state/helpers/__tests__/save-action-helpers.test.ts

+191-18
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {
1919
createManualAttribution,
2020
deleteManualAttribution,
2121
updateManualAttribution,
22+
unlinkResourceFromAttributionId,
23+
linkToAttributionManualData,
2224
} from '../save-action-helpers';
2325

2426
const testUuid: string = uuidNil;
@@ -91,24 +93,19 @@ describe('The deleteManualAttribution function', () => {
9193
});
9294

9395
it('correctly maintains childrenFromAttributedResources', () => {
94-
const testAnotherUuid = '000';
9596
const testManualData: AttributionData = {
9697
attributions: {
97-
[testAnotherUuid]: {
98-
packageName: 'another testpackage',
99-
},
10098
[testUuid]: {
10199
packageName: 'testpackage',
102100
packageVersion: '2.0',
103101
licenseText: 'Permission is hereby granted',
104102
},
105103
},
106104
resourcesToAttributions: {
107-
'/first/': [testUuid, testAnotherUuid],
105+
'/first/': [testUuid],
108106
},
109107
attributionsToResources: {
110108
[testUuid]: ['/first/'],
111-
[testAnotherUuid]: ['/first/'],
112109
},
113110
resourcesWithAttributedChildren: {
114111
attributedChildren: {
@@ -126,19 +123,11 @@ describe('The deleteManualAttribution function', () => {
126123
testUuid,
127124
() => false,
128125
);
129-
expect(newManualData.attributions).toEqual({
130-
'000': { packageName: 'another testpackage' },
131-
});
132-
expect(newManualData.resourcesToAttributions).toEqual({
133-
'/first/': ['000'],
134-
});
135-
expect(newManualData.attributionsToResources).toEqual({
136-
'000': ['/first/'],
137-
});
126+
expect(newManualData.attributions).toEqual({});
127+
expect(newManualData.resourcesToAttributions).toEqual({});
128+
expect(newManualData.attributionsToResources).toEqual({});
138129
expect(newManualData.resourcesWithAttributedChildren).toEqual({
139-
attributedChildren: {
140-
'0': new Set<number>().add(1),
141-
},
130+
attributedChildren: {},
142131
pathsToIndices: {
143132
'/': 0,
144133
'/first/': 1,
@@ -193,6 +182,190 @@ describe('The updateManualAttribution function', () => {
193182
});
194183
});
195184

185+
describe('The linkToAttributionManualData function', () => {
186+
it('links a manual attribution', () => {
187+
const testAnotherUuid = '000';
188+
const testManualData: AttributionData = {
189+
attributions: {
190+
[testAnotherUuid]: {
191+
packageName: 'another testpackage',
192+
},
193+
[testUuid]: {
194+
packageName: 'testpackage',
195+
packageVersion: '2.0',
196+
licenseText: 'Permission is hereby granted',
197+
},
198+
},
199+
resourcesToAttributions: {
200+
'/first/': [testAnotherUuid],
201+
},
202+
attributionsToResources: {
203+
[testAnotherUuid]: ['/first/'],
204+
},
205+
resourcesWithAttributedChildren: {
206+
attributedChildren: {
207+
'0': new Set<number>().add(1),
208+
},
209+
pathsToIndices: {
210+
'/': 0,
211+
'/first/': 1,
212+
},
213+
paths: ['/', '/first/'],
214+
},
215+
};
216+
217+
const newManualData = linkToAttributionManualData(
218+
testManualData,
219+
'/first/',
220+
testUuid,
221+
() => false,
222+
);
223+
224+
expect(newManualData.attributions).toEqual(testManualData.attributions);
225+
expect(newManualData.resourcesToAttributions).toEqual({
226+
'/first/': [testAnotherUuid, testUuid],
227+
});
228+
expect(newManualData.attributionsToResources).toEqual({
229+
[testUuid]: ['/first/'],
230+
[testAnotherUuid]: ['/first/'],
231+
});
232+
expect(newManualData.resourcesWithAttributedChildren).toEqual(
233+
testManualData.resourcesWithAttributedChildren,
234+
);
235+
});
236+
237+
it('correctly maintains childrenFromAttributedResources', () => {
238+
const testManualData: AttributionData = {
239+
attributions: {
240+
[testUuid]: {
241+
packageName: 'testpackage',
242+
packageVersion: '2.0',
243+
licenseText: 'Permission is hereby granted',
244+
},
245+
},
246+
resourcesToAttributions: {},
247+
attributionsToResources: {},
248+
resourcesWithAttributedChildren: {
249+
attributedChildren: {},
250+
pathsToIndices: {
251+
'/': 0,
252+
'/first/': 1,
253+
},
254+
paths: ['/', '/first/'],
255+
},
256+
};
257+
258+
const newManualData = linkToAttributionManualData(
259+
testManualData,
260+
'/first/',
261+
testUuid,
262+
() => false,
263+
);
264+
265+
expect(newManualData.resourcesWithAttributedChildren).toEqual({
266+
attributedChildren: {
267+
'0': new Set<number>().add(1),
268+
},
269+
pathsToIndices: {
270+
'/': 0,
271+
'/first/': 1,
272+
},
273+
paths: ['/', '/first/'],
274+
});
275+
});
276+
});
277+
278+
describe('The unlinkResourceFromAttributionId function', () => {
279+
it('unlinks a manual attribution', () => {
280+
const testManualData: AttributionData = {
281+
attributions: {
282+
[testUuid]: {
283+
packageName: 'testpackage',
284+
packageVersion: '2.0',
285+
licenseText: 'Permission is hereby granted',
286+
},
287+
},
288+
resourcesToAttributions: {
289+
'/first/': [testUuid],
290+
},
291+
attributionsToResources: {
292+
[testUuid]: ['/first/'],
293+
},
294+
resourcesWithAttributedChildren: {
295+
attributedChildren: {
296+
'0': new Set<number>().add(1),
297+
},
298+
pathsToIndices: {
299+
'/': 0,
300+
'/first/': 1,
301+
},
302+
paths: ['/', '/first/'],
303+
},
304+
};
305+
306+
const newManualData: AttributionData = unlinkResourceFromAttributionId(
307+
testManualData,
308+
'/first/',
309+
testUuid,
310+
);
311+
312+
expect(newManualData.attributions).toEqual(testManualData.attributions);
313+
expect(isEmpty(newManualData.resourcesToAttributions)).toBe(true);
314+
expect(isEmpty(newManualData.attributionsToResources)).toBe(true);
315+
expect(newManualData.resourcesWithAttributedChildren).toEqual({
316+
attributedChildren: {},
317+
pathsToIndices: {
318+
'/': 0,
319+
'/first/': 1,
320+
},
321+
paths: ['/', '/first/'],
322+
});
323+
});
324+
325+
it('correctly maintains childrenFromAttributedResources', () => {
326+
const testManualData: AttributionData = {
327+
attributions: {
328+
[testUuid]: {
329+
packageName: 'testpackage',
330+
packageVersion: '2.0',
331+
licenseText: 'Permission is hereby granted',
332+
},
333+
},
334+
resourcesToAttributions: {
335+
'/first/': [testUuid],
336+
},
337+
attributionsToResources: {
338+
[testUuid]: ['/first/'],
339+
},
340+
resourcesWithAttributedChildren: {
341+
attributedChildren: {
342+
'0': new Set<number>().add(1),
343+
},
344+
pathsToIndices: {
345+
'/': 0,
346+
'/first/': 1,
347+
},
348+
paths: ['/', '/first/'],
349+
},
350+
};
351+
352+
const newManualData: AttributionData = unlinkResourceFromAttributionId(
353+
testManualData,
354+
'/first/',
355+
testUuid,
356+
);
357+
358+
expect(newManualData.resourcesWithAttributedChildren).toEqual({
359+
attributedChildren: {},
360+
pathsToIndices: {
361+
'/': 0,
362+
'/first/': 1,
363+
},
364+
paths: ['/', '/first/'],
365+
});
366+
});
367+
});
368+
196369
describe('_removeManualAttributionFromChildrenIfAllAreIdentical', () => {
197370
it('three matching', () => {
198371
const testManualData: AttributionData = {

0 commit comments

Comments
 (0)