-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvariant-aliases.test.ts
More file actions
136 lines (118 loc) · 5.16 KB
/
variant-aliases.test.ts
File metadata and controls
136 lines (118 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { getVariantAliases, getVariantMetadataTags } from '../src/variant-aliases';
import { variantEntrySingle, variantEntries } from './mock/variant-fixtures';
function sortAliases(aliases: string[]): string[] {
return [...aliases].sort((a, b) => a.localeCompare(b));
}
describe('getVariantAliases', () => {
const contentTypeUid = 'movie';
it('extracts variant aliases for a single entry with explicit contentTypeUid', () => {
const result = getVariantAliases(variantEntrySingle, contentTypeUid);
expect(result.entry_uid).toBe('entry_uid_single');
expect(result.contenttype_uid).toBe(contentTypeUid);
expect(sortAliases(result.variants)).toEqual(sortAliases(['cs_personalize_0_0', 'cs_personalize_0_3']));
});
it('uses _content_type_uid from entry when present', () => {
const entry = {
...variantEntrySingle,
_content_type_uid: 'from_entry',
};
const result = getVariantAliases(entry, 'ignored');
expect(result.contenttype_uid).toBe('from_entry');
});
it('returns empty contenttype_uid when missing from entry and not passed', () => {
const result = getVariantAliases(variantEntrySingle);
expect(result.contenttype_uid).toBe('');
});
it('maps multiple entries in order', () => {
const results = getVariantAliases(variantEntries, contentTypeUid);
expect(results).toHaveLength(3);
expect(results[0].entry_uid).toBe('entry_uid_1');
expect(sortAliases(results[0].variants)).toEqual(sortAliases(['cs_personalize_0_0', 'cs_personalize_0_3']));
expect(results[1].entry_uid).toBe('entry_uid_2');
expect(results[1].variants).toEqual(['cs_personalize_0_0']);
expect(results[2].entry_uid).toBe('entry_uid_3');
expect(results[2].variants).toEqual([]);
});
it('returns empty variants when publish_details or variants is absent', () => {
const entry = { uid: 'u1', _content_type_uid: 'ct' };
expect(getVariantAliases(entry).variants).toEqual([]);
const entry2 = { uid: 'u1', publish_details: {} };
expect(getVariantAliases(entry2).variants).toEqual([]);
const entry3 = { uid: 'u1', publish_details: { variants: {} } };
expect(getVariantAliases(entry3).variants).toEqual([]);
});
it('skips variant objects with missing or empty alias', () => {
const entry = {
uid: 'u1',
publish_details: {
variants: {
a: { alias: 'keep_me' },
b: { alias: '' },
c: {},
d: { alias: 'also_keep' },
},
},
};
const result = getVariantAliases(entry);
expect(sortAliases(result.variants)).toEqual(sortAliases(['keep_me', 'also_keep']));
});
it('skips variant entries that are null, non-objects, or arrays', () => {
const variants: Record<string, unknown> = {
skip_null: null,
skip_string: 'not-an-object',
skip_array: [1, 2],
keep: { alias: 'only_valid' },
};
const entry = {
uid: 'u1',
publish_details: {
variants,
},
};
const result = getVariantAliases(entry);
expect(result.variants).toEqual(['only_valid']);
});
it('throws when entry is null or undefined', () => {
expect(() => getVariantAliases(null as unknown as Record<string, unknown>)).toThrow();
expect(() => getVariantAliases(undefined as unknown as Record<string, unknown>)).toThrow();
});
it('throws TypeError when single entry is a non-object (e.g. primitive)', () => {
expect(() => getVariantAliases(42 as unknown as Record<string, unknown>)).toThrow(TypeError);
expect(() => getVariantAliases('entry' as unknown as Record<string, unknown>)).toThrow(TypeError);
});
it('throws TypeError when an array item is not a plain object', () => {
expect(() =>
getVariantAliases([variantEntrySingle, [] as unknown as Record<string, unknown>])
).toThrow(TypeError);
});
it('throws when entry uid is missing or empty', () => {
expect(() => getVariantAliases({})).toThrow(/uid/i);
expect(() => getVariantAliases({ uid: '' })).toThrow(/uid/i);
});
it('throws when entries array contains a non-object', () => {
expect(() => getVariantAliases([variantEntrySingle, null as unknown as Record<string, unknown>])).toThrow();
});
});
describe('getVariantMetadataTags', () => {
const contentTypeUid = 'movie';
it('serialises array results as JSON in data-csvariants', () => {
const tag = getVariantMetadataTags(variantEntries, contentTypeUid);
expect(tag).toHaveProperty('data-csvariants');
const parsed = JSON.parse(tag['data-csvariants']) as Array<{
entry_uid: string;
contenttype_uid: string;
variants: string[];
}>;
expect(parsed).toHaveLength(3);
expect(parsed[0].entry_uid).toBe('entry_uid_1');
expect(sortAliases(parsed[0].variants)).toEqual(sortAliases(['cs_personalize_0_0', 'cs_personalize_0_3']));
});
it('returns empty JSON array string for empty entries', () => {
const tag = getVariantMetadataTags([]);
expect(tag['data-csvariants']).toBe('[]');
});
it('throws when entries is null or not an array', () => {
expect(() => getVariantMetadataTags(null as unknown as Record<string, unknown>[])).toThrow();
expect(() => getVariantMetadataTags({} as unknown as Record<string, unknown>[])).toThrow();
});
});