channel_store_tests.rs

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