channel_store_tests.rs

 1use util::http::FakeHttpClient;
 2
 3use super::*;
 4
 5#[gpui::test]
 6fn test_update_channels(cx: &mut AppContext) {
 7    let http = FakeHttpClient::with_404_response();
 8    let client = Client::new(http.clone(), cx);
 9    let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http, cx));
10
11    let channel_store = cx.add_model(|cx| ChannelStore::new(client, user_store, cx));
12
13    update_channels(
14        &channel_store,
15        proto::UpdateChannels {
16            channels: vec![
17                proto::Channel {
18                    id: 1,
19                    name: "b".to_string(),
20                    parent_id: None,
21                },
22                proto::Channel {
23                    id: 2,
24                    name: "a".to_string(),
25                    parent_id: None,
26                },
27            ],
28            ..Default::default()
29        },
30        cx,
31    );
32    assert_channels(
33        &channel_store,
34        &[
35            //
36            (0, "a"),
37            (0, "b"),
38        ],
39        cx,
40    );
41
42    update_channels(
43        &channel_store,
44        proto::UpdateChannels {
45            channels: vec![
46                proto::Channel {
47                    id: 3,
48                    name: "x".to_string(),
49                    parent_id: Some(1),
50                },
51                proto::Channel {
52                    id: 4,
53                    name: "y".to_string(),
54                    parent_id: Some(2),
55                },
56            ],
57            ..Default::default()
58        },
59        cx,
60    );
61    assert_channels(
62        &channel_store,
63        &[
64            //
65            (0, "a"),
66            (1, "y"),
67            (0, "b"),
68            (1, "x"),
69        ],
70        cx,
71    );
72}
73
74fn update_channels(
75    channel_store: &ModelHandle<ChannelStore>,
76    message: proto::UpdateChannels,
77    cx: &mut AppContext,
78) {
79    channel_store.update(cx, |store, cx| store.update_channels(message, cx));
80}
81
82fn assert_channels(
83    channel_store: &ModelHandle<ChannelStore>,
84    expected_channels: &[(usize, &str)],
85    cx: &AppContext,
86) {
87    channel_store.read_with(cx, |store, _| {
88        let actual = store
89            .channels()
90            .iter()
91            .map(|c| (c.depth, c.name.as_str()))
92            .collect::<Vec<_>>();
93        assert_eq!(actual, expected_channels);
94    });
95}