-
-
Notifications
You must be signed in to change notification settings - Fork 11.3k
/
Copy pathselectors.test.ts
172 lines (140 loc) · 5.92 KB
/
selectors.test.ts
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
165
166
167
168
169
170
171
172
import { t } from 'i18next';
import { describe, expect, it } from 'vitest';
import { ChatStore } from '@/store/chat';
import { initialState } from '@/store/chat/initialState';
import { merge } from '@/utils/merge';
import { topicSelectors } from './selectors';
// Mock i18next
vi.mock('i18next', () => ({
t: vi.fn().mockImplementation((key) => key),
}));
const initialStore = initialState as ChatStore;
const topicMaps = {
test: [
{ id: 'topic1', name: 'Topic 1', favorite: true },
{ id: 'topic2', name: 'Topic 2' },
],
};
describe('topicSelectors', () => {
describe('currentTopics', () => {
it('should return undefined if there are no topics with activeId', () => {
const topics = topicSelectors.currentTopics(initialStore);
expect(topics).toBeUndefined();
});
it('should return all current topics from the store', () => {
const state = merge(initialStore, { topicMaps, activeId: 'test' });
const topics = topicSelectors.currentTopics(state);
expect(topics).toEqual(topicMaps.test);
});
});
describe('currentTopicLength', () => {
it('should return 0 if there are no topics', () => {
const length = topicSelectors.currentTopicLength(initialStore);
expect(length).toBe(0);
});
it('should return the number of current topics', () => {
const state = merge(initialStore, { topicMaps, activeId: 'test' });
const length = topicSelectors.currentTopicLength(state);
expect(length).toBe(topicMaps.test.length);
});
});
describe('currentActiveTopic', () => {
it('should return undefined if there is no active topic', () => {
const topic = topicSelectors.currentActiveTopic(initialStore);
expect(topic).toBeUndefined();
});
it('should return the current active topic', () => {
const state = merge(initialStore, { topicMaps, activeId: 'test', activeTopicId: 'topic1' });
const topic = topicSelectors.currentActiveTopic(state);
expect(topic).toEqual(topicMaps.test[0]);
});
});
describe('currentUnFavTopics', () => {
it('should return all unfavorited topics', () => {
const state = merge(initialStore, { topicMaps, activeId: 'test' });
const topics = topicSelectors.currentUnFavTopics(state);
expect(topics).toEqual([topicMaps.test[1]]);
});
});
describe('displayTopics', () => {
it('should return current topics if not searching', () => {
const state = merge(initialStore, { topicMaps, activeId: 'test' });
const topics = topicSelectors.displayTopics(state);
expect(topics).toEqual(topicMaps.test);
});
it('should return search topics if searching', () => {
const searchTopics = [{ id: 'search1', name: 'Search 1' }];
const state = merge(initialStore, { isSearchingTopic: true, searchTopics });
const topics = topicSelectors.displayTopics(state);
expect(topics).toEqual(searchTopics);
});
});
describe('getTopicById', () => {
it('should return undefined if topic is not found', () => {
const state = merge(initialStore, { topicMaps, activeId: 'test' });
const topic = topicSelectors.getTopicById('notfound')(state);
expect(topic).toBeUndefined();
});
it('should return the topic with the given id', () => {
const state = merge(initialStore, { topicMaps, activeId: 'test' });
const topic = topicSelectors.getTopicById('topic1')(state);
expect(topic).toEqual(topicMaps.test[0]);
});
});
describe('groupedTopicsSelector', () => {
it('should return empty array if there are no topics', () => {
const state = merge(initialStore, { activeId: 'test' });
const grouped = topicSelectors.groupedTopicsSelector(state);
expect(grouped).toEqual([]);
});
it('should return grouped topics by time when no favorites exist', () => {
const topics = [
{ id: 'topic1', name: 'Topic 1', favorite: false, createAt: '2023-01-01' },
{ id: 'topic2', name: 'Topic 2', favorite: false, createAt: '2023-01-01' },
];
const state = merge(initialStore, {
topicMaps: { test: topics },
activeId: 'test',
});
const grouped = topicSelectors.groupedTopicsSelector(state);
expect(grouped).toHaveLength(1); // One time-based group
expect(grouped[0].children).toEqual(topics);
});
it('should separate favorite and unfavorite topics into different groups', () => {
const topics = [
{ id: 'topic1', name: 'Topic 1', favorite: true, createAt: '2023-01-01' },
{ id: 'topic2', name: 'Topic 2', favorite: false, createAt: '2023-01-01' },
{ id: 'topic3', name: 'Topic 3', favorite: true, createAt: '2023-01-01' },
];
const state = merge(initialStore, {
topicMaps: { test: topics },
activeId: 'test',
});
const grouped = topicSelectors.groupedTopicsSelector(state);
expect(grouped).toHaveLength(2); // Favorite group + one time-based group
// Check favorite group
expect(grouped[0]).toEqual({
id: 'favorite',
title: 'favorite', // This matches the mocked t function return
children: topics.filter((t) => t.favorite),
});
// Check unfavorite group
expect(grouped[1].children).toEqual(topics.filter((t) => !t.favorite));
});
it('should only create time-based groups when there are no favorites', () => {
const topics = [
{ id: 'topic1', name: 'Topic 1', favorite: false, createAt: '2023-01-01' },
{ id: 'topic2', name: 'Topic 2', favorite: false, createAt: '2023-02-01' },
];
const state = merge(initialStore, {
topicMaps: { test: topics },
activeId: 'test',
});
const grouped = topicSelectors.groupedTopicsSelector(state);
// Should not have a favorites group
expect(grouped.find((g) => g.id === 'favorite')).toBeUndefined();
// Should have time-based groups
expect(grouped.every((g) => g.id !== 'favorite')).toBeTruthy();
});
});
});