directus

Форк
0
/
get-relation-type.test.ts 
82 строки · 1.7 Кб
1
import type { Relation } from '@directus/types';
2
import { expect, test } from 'vitest';
3
import { getRelationType } from './get-relation-type.js';
4

5
test('Returns null if no relation object is included', () => {
6
	const result = getRelationType({ relation: null, collection: null, field: 'test' });
7
	expect(result).toBe(null);
8
});
9

10
test('Returns a2o if relation matches and includes one_collection_field and one_allowed_collection', () => {
11
	const relation = {
12
		collection: 'pages',
13
		field: 'item',
14
		related_collection: null,
15
		meta: {
16
			one_collection_field: 'collection',
17
			one_allowed_collections: ['paragraphs', 'headings', 'images'],
18
		},
19
	} as Relation;
20

21
	const result = getRelationType({
22
		relation,
23
		collection: 'pages',
24
		field: 'item',
25
	});
26

27
	expect(result).toBe('a2o');
28
});
29

30
test('Returns m2o', () => {
31
	const relation = {
32
		collection: 'articles',
33
		field: 'author',
34
		related_collection: 'authors',
35
	} as Relation;
36

37
	const result = getRelationType({
38
		relation,
39
		collection: 'articles',
40
		field: 'author',
41
	});
42

43
	expect(result).toBe('m2o');
44
});
45

46
test('Returns o2m', () => {
47
	const relation = {
48
		collection: 'articles',
49
		field: 'author',
50
		related_collection: 'authors',
51
		meta: {
52
			one_field: 'articles',
53
		},
54
	} as Relation;
55

56
	const result = getRelationType({
57
		relation,
58
		collection: 'authors',
59
		field: 'articles',
60
	});
61

62
	expect(result).toBe('o2m');
63
});
64

65
test('Returns null when field/collection does not match the relationship', () => {
66
	const relation = {
67
		collection: 'articles',
68
		field: 'author',
69
		related_collection: 'authors',
70
		meta: {
71
			one_field: 'articles',
72
		},
73
	} as Relation;
74

75
	const result = getRelationType({
76
		relation,
77
		collection: 'unrelated',
78
		field: 'wrong',
79
	});
80

81
	expect(result).toBe(null);
82
});
83

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.