Skip to content

Commit d8f153c

Browse files
committed
test: adds a few more tests to entityContainer and prepares tests for the access functionality
1 parent b739ab6 commit d8f153c

1 file changed

Lines changed: 222 additions & 0 deletions

File tree

frontend_svelte/src/lib/entityContainer.svelte.test.ts

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { flushSync } from 'svelte';
12
import { v4 } from 'uuid';
23
import { beforeEach, describe, expect, test } from 'vitest';
34

@@ -7,6 +8,7 @@ import { EntityContainer } from './entityContainer.svelte';
78
type AnyEntityExtended = {
89
id: string;
910
name: string;
11+
creation_date?: Date;
1012
// other properties...
1113
};
1214

@@ -122,4 +124,224 @@ describe('EntityContainer', () => {
122124
entityContainer.removeSelection('testSelection');
123125
expect(entityContainer.selections).toEqual({});
124126
});
127+
128+
test('should create filtered entity selection', () => {
129+
const entity1 = { id: v4(), name: 'Entity 1' };
130+
const entity2 = { id: v4(), name: 'Entity 2' };
131+
const entity3 = { id: v4(), name: 'Something else 3' };
132+
const entity4 = { id: v4(), name: 'Entity 4' };
133+
entityContainer.entities = [entity1, entity2, entity3, entity4];
134+
$effect.root(() => {
135+
entityContainer.createFilteredEntitySelection(
136+
'filteredSelection',
137+
(e) => e.name.slice(0, 6) === 'Entity'
138+
);
139+
entityContainer.createFilteredEntitySelection(
140+
'invertedFilteredSelection',
141+
(e) => e.name.slice(0, 6) !== 'Entity'
142+
);
143+
});
144+
flushSync();
145+
expect(entityContainer.getSelectedEntities('filteredSelection')).toEqual([
146+
entity1,
147+
entity2,
148+
entity4
149+
]);
150+
expect(entityContainer.getSelectedEntities('invertedFilteredSelection')).toEqual([entity3]);
151+
});
152+
153+
// test('should create all linked selection', () => {
154+
// const entity1 = { id: v4(), name: 'Entity 1' };
155+
// const entity2 = { id: v4(), name: 'Entity 2' };
156+
// const entity3 = { id: v4(), name: 'Entity 3' };
157+
// const entity4 = { id: v4(), name: 'Entity 4' };
158+
// entityContainer.entities = [entity1, entity2, entity3, entity4];
159+
// const hierarchy12 = { child_id: entity2.id, parent_id: entity1.id };
160+
// const hierarchy13 = { child_id: entity3.id, parent_id: entity1.id };
161+
// const hierarchy24 = { child_id: entity4.id, parent_id: entity2.id };
162+
// entityContainer.hierarchies = {
163+
// [entity1.id]: { parents: [hierarchy12, hierarchy13] },
164+
// [entity2.id]: { children: [hierarchy12], parents: [hierarchy24] },
165+
// [entity3.id]: { children: [hierarchy13] },
166+
// [entity4.id]: { children: [hierarchy24] }
167+
// };
168+
// $effect.root(() => {
169+
// entityContainer.createAllLinkedSelection('linkedToEntity1', entity1.id);
170+
// entityContainer.createAllLinkedSelection('linkedToEntity2', entity2.id);
171+
// entityContainer.createAllLinkedSelection('linkedToEntity1Inverted', entity1.id, true);
172+
// });
173+
// flushSync(() => {});
174+
// expect(entityContainer.getSelectedEntities('linkedToEntity1')).toEqual([entity2, entity3]);
175+
// expect(entityContainer.getSelectedEntities('linkedToEntity2')).toEqual([entity1, entity4]);
176+
// expect(entityContainer.getSelectedEntities('linkedToEntity1Inverted')).toEqual([entity4]);
177+
// });
178+
179+
test('should create user has specific access right selection', () => {
180+
const entity1 = { id: v4(), name: 'Entity 1' };
181+
const entity2 = { id: v4(), name: 'Entity 2' };
182+
const entity3 = { id: v4(), name: 'Entity 3' };
183+
const entity4 = { id: v4(), name: 'Entity 4' };
184+
entityContainer.entities = [entity1, entity2, entity3, entity4];
185+
const accessRight1 = { [entity1.id]: Action.OWN };
186+
const accessRight2 = { [entity2.id]: Action.WRITE };
187+
const accessRight3 = { [entity3.id]: Action.CONNECT };
188+
const accessRight4 = { [entity4.id]: Action.READ };
189+
entityContainer.accessRights = {
190+
...accessRight1,
191+
...accessRight2,
192+
...accessRight3,
193+
...accessRight4
194+
};
195+
$effect.root(() => {
196+
entityContainer.createUserHasSpecificAccessRightSelection('ownSelection', Action.OWN);
197+
entityContainer.createUserHasSpecificAccessRightSelection('writeSelection', Action.WRITE);
198+
entityContainer.createUserHasSpecificAccessRightSelection('connectSelection', Action.CONNECT);
199+
entityContainer.createUserHasSpecificAccessRightSelection('readSelection', Action.READ);
200+
});
201+
flushSync();
202+
expect(entityContainer.getSelectedEntities('ownSelection')).toEqual([entity1]);
203+
expect(entityContainer.getSelectedEntities('writeSelection')).toEqual([entity2]);
204+
expect(entityContainer.getSelectedEntities('connectSelection')).toEqual([entity3]);
205+
expect(entityContainer.getSelectedEntities('readSelection')).toEqual([entity4]);
206+
});
207+
208+
// test('should create access policy resource selection', () => {
209+
// const entity1 = { id: v4(), name: 'Entity 1' };
210+
// const entity2 = { id: v4(), name: 'Entity 2' };
211+
// const entity3 = { id: v4(), name: 'Entity 3' };
212+
// const entity4 = { id: v4(), name: 'Entity 4' };
213+
// entityContainer.entities = [entity1, entity2, entity3, entity4];
214+
// const accessPolicy1 = { resource_id: entity1.id, action: Action.READ, public: false };
215+
// const accessPolicy2 = { resource_id: entity2.id, action: Action.WRITE, public: false };
216+
// const accessPolicy3 = { resource_id: entity3.id, action: Action.CONNECT, public: false };
217+
// const accessPolicy4 = { resource_id: entity4.id, action: Action.READ, public: false };
218+
// const accessPolicyKey1 = v4();
219+
// const accessPolicyKey2 = v4();
220+
// const accessPolicyKey3 = v4();
221+
// const accessPolicyKey4 = v4();
222+
// entityContainer.accessPolicies = {
223+
// [accessPolicyKey1]: [accessPolicy1],
224+
// [accessPolicyKey2]: [accessPolicy2],
225+
// [accessPolicyKey3]: [accessPolicy3],
226+
// [accessPolicyKey4]: [accessPolicy4]
227+
// };
228+
// $effect.root(() => {
229+
// entityContainer.createAccessPolicyResourceSelection(
230+
// 'readResources',
231+
// (policy) => policy.action === Action.READ
232+
// );
233+
// entityContainer.createAccessPolicyResourceSelection(
234+
// 'writeResources',
235+
// (policy) => policy.action === Action.WRITE
236+
// );
237+
// });
238+
// flushSync();
239+
// expect(entityContainer.getSelectedEntities('readResources')).toEqual([entity1, entity4]);
240+
// expect(entityContainer.getSelectedEntities('writeResources')).toEqual([entity2]);
241+
// });
242+
243+
// test('should create access policy identity selection', () => {
244+
// const entity1 = { id: v4(), name: 'Entity 1' };
245+
// const entity2 = { id: v4(), name: 'Entity 2' };
246+
// const entity3 = { id: v4(), name: 'Entity 3' };
247+
// const entity4 = { id: v4(), name: 'Entity 4' };
248+
// entityContainer.entities = [entity1, entity2, entity3, entity4];
249+
// const identity1 = { id: v4(), name: 'Identity 1' };
250+
// const identity2 = { id: v4(), name: 'Identity 2' };
251+
// const identity3 = { id: v4(), name: 'Identity 3' };
252+
// const identity4 = { id: v4(), name: 'Identity 4' };
253+
// entityContainer.identities = [identity1, identity2, identity3, identity4];
254+
// const accessPolicy1 = {
255+
// resource_id: entity1.id,
256+
// identity_id: identity1.id,
257+
// action: Action.READ,
258+
// public: false
259+
// };
260+
// const accessPolicy2 = {
261+
// resource_id: entity2.id,
262+
// identity_id: identity2.id,
263+
// action: Action.WRITE,
264+
// public: false
265+
// };
266+
// const accessPolicy3 = {
267+
// resource_id: entity3.id,
268+
// identity_id: identity3.id,
269+
// action: Action.CONNECT,
270+
// public: false
271+
// };
272+
// const accessPolicy4 = {
273+
// resource_id: entity4.id,
274+
// identity_id: identity4.id,
275+
// action: Action.READ,
276+
// public: false
277+
// };
278+
// const accessPolicyKey1 = v4();
279+
// const accessPolicyKey2 = v4();
280+
// const accessPolicyKey3 = v4();
281+
// const accessPolicyKey4 = v4();
282+
// entityContainer.accessPolicies = {
283+
// [accessPolicyKey1]: [accessPolicy1],
284+
// [accessPolicyKey2]: [accessPolicy2],
285+
// [accessPolicyKey3]: [accessPolicy3],
286+
// [accessPolicyKey4]: [accessPolicy4]
287+
// };
288+
// $effect.root(() => {
289+
// entityContainer.createAccessPolicyIdentitySelection(
290+
// 'identity1Resources',
291+
// (policy) => policy.identity_id === identity1.id
292+
// );
293+
// entityContainer.createAccessPolicyIdentitySelection(
294+
// 'identity2Resources',
295+
// (policy) => policy.identity_id === identity2.id
296+
// );
297+
// entityContainer.createAccessPolicyIdentitySelection(
298+
// 'identity3Resources',
299+
// (policy) => policy.identity_id === identity3.id
300+
// );
301+
// entityContainer.createAccessPolicyIdentitySelection(
302+
// 'identity4Resources',
303+
// (policy) => policy.identity_id === identity4.id
304+
// );
305+
// });
306+
// flushSync();
307+
// expect(entityContainer.getSelectedEntities('identity1Resources')).toEqual([entity1]);
308+
// expect(entityContainer.getSelectedEntities('identity2Resources')).toEqual([entity2]);
309+
// expect(entityContainer.getSelectedEntities('identity3Resources')).toEqual([entity3]);
310+
// expect(entityContainer.getSelectedEntities('identity4Resources')).toEqual([entity4]);
311+
// });
312+
313+
test('should create sorted selection', () => {
314+
const entity1 = { id: v4(), name: 'Entity 1', creation_date: '2024-01-01' };
315+
const entity2 = { id: v4(), name: 'Entity 2', creation_date: '2024-03-01' };
316+
const entity3 = { id: v4(), name: 'Entity 3', creation_date: '2024-02-01' };
317+
const entity4 = { id: v4(), name: 'Entity 4' };
318+
entityContainer.entities = [entity1, entity2, entity3, entity4];
319+
$effect.root(() => {
320+
entityContainer.createSortedSelection('sortedByCreationDate', 'creation_date');
321+
entityContainer.createSortedSelection('sortedByCreationDateDesc', 'creation_date', false);
322+
});
323+
flushSync();
324+
expect(entityContainer.getSelectedEntities('sortedByCreationDate')).toEqual([
325+
entity1,
326+
entity3,
327+
entity2,
328+
entity4
329+
]);
330+
expect(entityContainer.getSelectedEntities('sortedByCreationDateDesc')).toEqual([
331+
entity2,
332+
entity3,
333+
entity1,
334+
entity4
335+
]);
336+
});
337+
338+
test('should throw error when creating all linked selection without parentId', () => {
339+
$effect.root(() => {
340+
expect(() =>
341+
entityContainer.createAllLinkedSelection('test', undefined as unknown as string)
342+
).toThrowError(
343+
"Parent ID must be provided either as an argument or as the EntityContainer's parentId property."
344+
);
345+
});
346+
});
125347
});

0 commit comments

Comments
 (0)