forked from firebase/FirebaseUI-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFUIIndexTableViewDataSourceTest.m
More file actions
169 lines (132 loc) · 5.84 KB
/
Copy pathFUIIndexTableViewDataSourceTest.m
File metadata and controls
169 lines (132 loc) · 5.84 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
165
166
167
168
169
//
// Copyright (c) 2016 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
@import XCTest;
@import UIKit;
@import FirebaseDatabaseUI;
#import "FUIIndexTableViewDataSource.h"
#import "FUIDatabaseTestUtils.h"
static NSString *const kTestReuseIdentifier = @"FUIIndexTableViewDataSourceTest";
@interface FUIIndexTableViewDataSourceTest : XCTestCase <FUIIndexTableViewDataSourceDelegate>
@property (nonatomic, readwrite) FUIIndexTableViewDataSource *dataSource;
@property (nonatomic, readwrite) UITableView *tableView;
@property (nonatomic, readwrite) FUITestObservable *index;
@property (nonatomic, readwrite) FUITestObservable *data;
@property (nonatomic, readwrite) NSMutableDictionary *dict;
@end
@implementation FUIIndexTableViewDataSourceTest
static inline NSDictionary *database() {
static NSDictionary *dict;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dict = @{
@"index": @{
@"1": @(YES),
@"2": @(YES),
@"3": @(YES),
},
@"data": @{
@"1": @{ @"data": @"1" },
@"2": @{ @"data": @"2" },
@"3": @{ @"data": @"3" },
},
};
});
return dict;
}
- (void)setUp {
[super setUp];
[UIView setAnimationsEnabled:NO];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)
style:UITableViewStylePlain];
[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:kTestReuseIdentifier];
self.index = [[FUITestObservable alloc] initWithDictionary:database()[@"index"]];
self.data = [[FUITestObservable alloc] initWithDictionary:database()[@"data"]];
self.dataSource = [self.tableView bindToIndexedQuery:(FIRDatabaseQuery *)self.index
data:(FIRDatabaseReference *)self.data
delegate:self
populateCell:^UITableViewCell *(UITableView *view,
NSIndexPath *indexPath,
FIRDataSnapshot *snap) {
UITableViewCell *cell = [view dequeueReusableCellWithIdentifier:kTestReuseIdentifier];
cell.accessibilityLabel = snap.key;
cell.accessibilityValue = snap.value;
return cell;
}];
self.dict = [database() mutableCopy];
}
- (void)tearDown {
[UIView setAnimationsEnabled:YES];
[super tearDown];
}
- (void)testItPopulatesCells {
UITableViewCell *cell = [self.dataSource tableView:self.tableView
cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
XCTAssertEqualObjects(cell.accessibilityLabel, @"data");
XCTAssertEqualObjects(cell.accessibilityValue, @"1");
cell = [self.dataSource tableView:self.tableView
cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
XCTAssertEqualObjects(cell.accessibilityLabel, @"data");
XCTAssertEqualObjects(cell.accessibilityValue, @"2");
cell = [self.dataSource tableView:self.tableView
cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
XCTAssertEqualObjects(cell.accessibilityLabel, @"data");
XCTAssertEqualObjects(cell.accessibilityValue, @"3");
}
- (void)testItUpdatesOnInsertion {
// insert item
[self.data addObject:@{ @"data": @"4" } forKey:@"4"];
[self.index addObject:@(YES) forKey:@"4"];
UITableViewCell *cell = [self.dataSource tableView:self.tableView
cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
XCTAssertEqualObjects(cell.accessibilityLabel, @"data");
XCTAssertEqualObjects(cell.accessibilityValue, @"4");
}
- (void)testItUpdatesOnDeletion {
// delete item
[self.data removeObjectForKey:@"2"];
[self.index removeObjectForKey:@"2"];
UITableViewCell *cell = [self.dataSource tableView:self.tableView
cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
XCTAssertEqualObjects(cell.accessibilityLabel, @"data");
XCTAssertEqualObjects(cell.accessibilityValue, @"3");
}
- (void)testItUpdatesOnChange {
// change item
[self.data changeObject:@{ @"data": @"changed" } forKey:@"2"];
[self.index changeObject:@(YES) forKey:@"2"];
UITableViewCell *cell = [self.dataSource tableView:self.tableView
cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
XCTAssertEqualObjects(cell.accessibilityLabel, @"data");
XCTAssertEqualObjects(cell.accessibilityValue, @"changed");
}
- (void)testItUpdatesOnMove {
// move item to back
[self.data moveObjectFromIndex:0 toIndex:2];
[self.index moveObjectFromIndex:0 toIndex:2];
UITableViewCell *cell = [self.dataSource tableView:self.tableView
cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
XCTAssertEqualObjects(cell.accessibilityLabel, @"data");
XCTAssertEqualObjects(cell.accessibilityValue, @"1");
// move item to front
[self.data moveObjectFromIndex:2 toIndex:0];
[self.index moveObjectFromIndex:2 toIndex:0];
cell = [self.dataSource tableView:self.tableView
cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
XCTAssertEqualObjects(cell.accessibilityLabel, @"data");
XCTAssertEqualObjects(cell.accessibilityValue, @"3");
}
@end