-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathfeedConfig.test.ts
More file actions
164 lines (143 loc) · 5.16 KB
/
Copy pathfeedConfig.test.ts
File metadata and controls
164 lines (143 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import {
PREDICT_FEED_IDS,
PREDICT_FEED_REGISTRY,
isPredictFeedId,
resolvePredictFeedConfig,
resolvePredictFeedDefaultFilter,
resolvePredictFeedDefaultTab,
resolvePredictFeedDynamicFilterConfig,
type PredictDynamicFilterConfig,
type PredictFeedFilterConfig,
} from './feedConfig';
const getAllStaticFilters = (): PredictFeedFilterConfig[] =>
Object.values(PREDICT_FEED_REGISTRY).flatMap((feed) =>
feed.tabs.flatMap((tab) => tab.filters.static),
);
const getAllDynamicFilters = (): PredictDynamicFilterConfig[] =>
Object.values(PREDICT_FEED_REGISTRY)
.flatMap((feed) => feed.tabs.map((tab) => tab.filters.dynamic))
.filter((filter): filter is PredictDynamicFilterConfig => Boolean(filter));
describe('feedConfig', () => {
it('centralizes the exact v1 feed ids and excludes World Cup', () => {
expect(Object.keys(PREDICT_FEED_REGISTRY)).toEqual([...PREDICT_FEED_IDS]);
expect(PREDICT_FEED_IDS).toEqual([
'sports',
'politics',
'crypto',
'live',
'trending',
]);
expect(isPredictFeedId('world-cup')).toBe(false);
expect(isPredictFeedId('popular-today')).toBe(false);
});
it.each(PREDICT_FEED_IDS)('resolves known feed id %s', (feedId) => {
expect(isPredictFeedId(feedId)).toBe(true);
expect(resolvePredictFeedConfig(feedId)?.id).toBe(feedId);
});
it('returns undefined for unknown feed ids', () => {
expect(isPredictFeedId('unknown')).toBe(false);
expect(resolvePredictFeedConfig('unknown')).toBeUndefined();
expect(resolvePredictFeedConfig()).toBeUndefined();
});
it('defines at least one tab for every feed', () => {
Object.values(PREDICT_FEED_REGISTRY).forEach((feed) => {
expect(feed.tabs.length).toBeGreaterThan(0);
});
});
it.each(PREDICT_FEED_IDS)(
'uses the first tab as the default tab for %s',
(feedId) => {
expect(resolvePredictFeedDefaultTab(feedId)).toBe(
PREDICT_FEED_REGISTRY[feedId].tabs[0],
);
},
);
it('resolves every tab default filter from static filters', () => {
Object.values(PREDICT_FEED_REGISTRY).forEach((feed) => {
feed.tabs.forEach((tab) => {
const defaultFilter = tab.filters.static.find(
(filter) => filter.id === tab.defaultFilterId,
);
expect(defaultFilter).toBeDefined();
expect(resolvePredictFeedDefaultFilter(feed.id, tab.id)).toBe(
defaultFilter,
);
});
});
});
it.each(PREDICT_FEED_IDS)(
'falls back to the first tab when resolving the default filter for %s without a tabId',
(feedId) => {
const firstTab = PREDICT_FEED_REGISTRY[feedId].tabs[0];
const expected = firstTab.filters.static.find(
(filter) => filter.id === firstTab.defaultFilterId,
);
expect(resolvePredictFeedDefaultFilter(feedId)).toBe(expected);
},
);
it('returns undefined when resolving the default filter for unknown feeds or tabs', () => {
expect(resolvePredictFeedDefaultFilter('unknown')).toBeUndefined();
expect(
resolvePredictFeedDefaultFilter('sports', 'unknown-tab'),
).toBeUndefined();
});
it('represents hidden-tab feeds with exactly one tab', () => {
(['politics', 'crypto', 'live', 'trending'] as const).forEach((feedId) => {
expect(PREDICT_FEED_REGISTRY[feedId].tabs).toHaveLength(1);
});
});
it('defines the v1 sports tabs from the Figma shell', () => {
expect(PREDICT_FEED_REGISTRY.sports.tabs.map((tab) => tab.id)).toEqual([
'basketball',
'tennis',
'soccer',
'baseball',
'football',
]);
});
it('uses related-tags dynamic filters with supported base slugs', () => {
const dynamicFilters = getAllDynamicFilters();
const sources = new Set(dynamicFilters.map((filter) => filter.source));
const baseTagSlugs = new Set(
dynamicFilters
.map((filter) => filter.baseTagSlug)
.filter((baseTagSlug): baseTagSlug is string => Boolean(baseTagSlug)),
);
expect(sources).toEqual(new Set(['related-tags']));
expect(baseTagSlugs).toEqual(
new Set(['sports', 'politics', 'crypto', 'all']),
);
});
it('resolves the dynamic filter config for a feed from the registry', () => {
expect(resolvePredictFeedDynamicFilterConfig('trending')).toBe(
PREDICT_FEED_REGISTRY.trending.tabs[0].filters.dynamic,
);
});
it('returns undefined dynamic config for feeds without one, unknown feeds, or unknown tabs', () => {
expect(resolvePredictFeedDynamicFilterConfig('live')).toBeUndefined();
expect(resolvePredictFeedDynamicFilterConfig('unknown')).toBeUndefined();
expect(
resolvePredictFeedDynamicFilterConfig('sports', 'unknown-tab'),
).toBeUndefined();
});
it('keeps static market params category-free', () => {
const allowedParamKeys = new Set([
'tags',
'tagSlugs',
'series',
'order',
'status',
'live',
'search',
'limit',
'afterCursor',
]);
getAllStaticFilters().forEach((filter) => {
const params = filter.params as Record<string, unknown>;
expect('category' in params).toBe(false);
expect(
Object.keys(params).every((key) => allowedParamKeys.has(key)),
).toBe(true);
});
});
});