Skip to content

Commit e684e88

Browse files
committed
add FirebaseIndexTableViewDataSourceTests
1 parent 6d03513 commit e684e88

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
//
2+
// Copyright (c) 2016 Google Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
@import XCTest;
18+
@import UIKit;
19+
20+
@import FirebaseDatabaseUI;
21+
22+
#import "FirebaseIndexTableViewDataSource.h"
23+
#import "FirebaseArrayTestUtils.h"
24+
25+
static NSString *const kTestReuseIdentifier = @"FirebaseIndexTableViewDataSourceTest";
26+
27+
@interface FirebaseIndexTableViewDataSourceTest : XCTestCase <FirebaseIndexTableViewDataSourceDelegate>
28+
29+
@property (nonatomic, readwrite) FirebaseIndexTableViewDataSource *dataSource;
30+
31+
@property (nonatomic, readwrite) UITableView *tableView;
32+
33+
@property (nonatomic, readwrite) FUITestObservable *index;
34+
@property (nonatomic, readwrite) FUITestObservable *data;
35+
36+
@property (nonatomic, readwrite) NSMutableDictionary *dict;
37+
38+
@end
39+
40+
@implementation FirebaseIndexTableViewDataSourceTest
41+
42+
static inline NSDictionary *database() {
43+
static NSDictionary *dict;
44+
static dispatch_once_t onceToken;
45+
dispatch_once(&onceToken, ^{
46+
dict = @{
47+
@"index": @{
48+
@"1": @(YES),
49+
@"2": @(YES),
50+
@"3": @(YES),
51+
},
52+
@"data": @{
53+
@"1": @{ @"data": @"1" },
54+
@"2": @{ @"data": @"2" },
55+
@"3": @{ @"data": @"3" },
56+
},
57+
};
58+
});
59+
return dict;
60+
}
61+
62+
- (void)setUp {
63+
[super setUp];
64+
[UIView setAnimationsEnabled:NO];
65+
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)
66+
style:UITableViewStylePlain];
67+
[self.tableView registerClass:[UITableViewCell class]
68+
forCellReuseIdentifier:kTestReuseIdentifier];
69+
70+
self.index = [[FUITestObservable alloc] initWithDictionary:database()[@"index"]];
71+
self.data = [[FUITestObservable alloc] initWithDictionary:database()[@"data"]];
72+
self.dataSource = [[FirebaseIndexTableViewDataSource alloc] initWithIndex:(FIRDatabaseQuery *)self.index
73+
data:(FIRDatabaseReference *)self.data
74+
tableView:self.tableView
75+
cellReuseIdentifier:kTestReuseIdentifier
76+
delegate:self
77+
populateCell:^(UITableViewCell *cell,
78+
FIRDataSnapshot *snap) {
79+
cell.accessibilityLabel = snap.key;
80+
cell.accessibilityValue = snap.value;
81+
}];
82+
self.dict = [database() mutableCopy];
83+
}
84+
85+
- (void)tearDown {
86+
[UIView setAnimationsEnabled:YES];
87+
[super tearDown];
88+
}
89+
90+
- (void)testItPopulatesCells {
91+
UITableViewCell *cell = [self.dataSource tableView:self.tableView
92+
cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
93+
94+
XCTAssertEqualObjects(cell.accessibilityLabel, @"data");
95+
XCTAssertEqualObjects(cell.accessibilityValue, @"1");
96+
97+
cell = [self.dataSource tableView:self.tableView
98+
cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
99+
100+
XCTAssertEqualObjects(cell.accessibilityLabel, @"data");
101+
XCTAssertEqualObjects(cell.accessibilityValue, @"2");
102+
103+
cell = [self.dataSource tableView:self.tableView
104+
cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
105+
106+
XCTAssertEqualObjects(cell.accessibilityLabel, @"data");
107+
XCTAssertEqualObjects(cell.accessibilityValue, @"3");
108+
}
109+
110+
- (void)testItUpdatesOnInsertion {
111+
// insert item
112+
[self.data addObject:@{ @"data": @"4" } forKey:@"4"];
113+
[self.index addObject:@(YES) forKey:@"4"];
114+
115+
UITableViewCell *cell = [self.dataSource tableView:self.tableView
116+
cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
117+
118+
XCTAssertEqualObjects(cell.accessibilityLabel, @"data");
119+
XCTAssertEqualObjects(cell.accessibilityValue, @"4");
120+
}
121+
122+
- (void)testItUpdatesOnDeletion {
123+
// delete item
124+
[self.data removeObjectForKey:@"2"];
125+
[self.index removeObjectForKey:@"2"];
126+
127+
UITableViewCell *cell = [self.dataSource tableView:self.tableView
128+
cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
129+
130+
XCTAssertEqualObjects(cell.accessibilityLabel, @"data");
131+
XCTAssertEqualObjects(cell.accessibilityValue, @"3");
132+
}
133+
134+
- (void)testItUpdatesOnChange {
135+
// change item
136+
[self.data changeObject:@{ @"data": @"changed" } forKey:@"2"];
137+
[self.index changeObject:@(YES) forKey:@"2"];
138+
139+
UITableViewCell *cell = [self.dataSource tableView:self.tableView
140+
cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
141+
142+
XCTAssertEqualObjects(cell.accessibilityLabel, @"data");
143+
XCTAssertEqualObjects(cell.accessibilityValue, @"changed");
144+
}
145+
146+
- (void)testItUpdatesOnMove {
147+
// move item to back
148+
[self.data moveObjectFromIndex:0 toIndex:2];
149+
[self.index moveObjectFromIndex:0 toIndex:2];
150+
151+
UITableViewCell *cell = [self.dataSource tableView:self.tableView
152+
cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
153+
154+
XCTAssertEqualObjects(cell.accessibilityLabel, @"data");
155+
XCTAssertEqualObjects(cell.accessibilityValue, @"1");
156+
157+
// move item to front
158+
[self.data moveObjectFromIndex:2 toIndex:0];
159+
[self.index moveObjectFromIndex:2 toIndex:0];
160+
161+
cell = [self.dataSource tableView:self.tableView
162+
cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
163+
164+
XCTAssertEqualObjects(cell.accessibilityLabel, @"data");
165+
XCTAssertEqualObjects(cell.accessibilityValue, @"3");
166+
}
167+
168+
@end

FirebaseUI.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
8D1E107C1DAEB97300AEFCA0 /* FirebaseIndexTableViewDataSourceTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D1E107B1DAEB97300AEFCA0 /* FirebaseIndexTableViewDataSourceTest.m */; };
1011
8D2A84AA1D678B2B0058DF04 /* FirebaseUI.h in Headers */ = {isa = PBXBuildFile; fileRef = 8D2A84A91D678B2B0058DF04 /* FirebaseUI.h */; settings = {ATTRIBUTES = (Public, ); }; };
1112
8D78AF061D9D8CB000CFA9C5 /* UIImageView+FirebaseStorage.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D7AD9B61D9317FB006866B9 /* UIImageView+FirebaseStorage.m */; };
1213
8D7AD9B71D9317FB006866B9 /* UIImageView+FirebaseStorage.h in Headers */ = {isa = PBXBuildFile; fileRef = 8D7AD9B51D9317FB006866B9 /* UIImageView+FirebaseStorage.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -347,6 +348,7 @@
347348
/* End PBXCopyFilesBuildPhase section */
348349

349350
/* Begin PBXFileReference section */
351+
8D1E107B1DAEB97300AEFCA0 /* FirebaseIndexTableViewDataSourceTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FirebaseIndexTableViewDataSourceTest.m; sourceTree = "<group>"; };
350352
8D2A84A61D678B2B0058DF04 /* FirebaseUI.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = FirebaseUI.framework; sourceTree = BUILT_PRODUCTS_DIR; };
351353
8D2A84A91D678B2B0058DF04 /* FirebaseUI.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FirebaseUI.h; sourceTree = "<group>"; };
352354
8D2A84AB1D678B2B0058DF04 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -861,6 +863,7 @@
861863
8D924C601DA6F69100C4DA48 /* FirebaseIndexArrayTest.m */,
862864
8DA941EC1D67951B00CD3685 /* FirebaseCollectionViewDataSourceTest.m */,
863865
8DA941ED1D67951B00CD3685 /* FirebaseTableViewDataSourceTest.m */,
866+
8D1E107B1DAEB97300AEFCA0 /* FirebaseIndexTableViewDataSourceTest.m */,
864867
);
865868
path = FirebaseDatabaseUITests;
866869
sourceTree = "<group>";
@@ -1897,6 +1900,7 @@
18971900
files = (
18981901
8DA941F01D67951B00CD3685 /* FirebaseCollectionViewDataSourceTest.m in Sources */,
18991902
8DA941F11D67951B00CD3685 /* FirebaseTableViewDataSourceTest.m in Sources */,
1903+
8D1E107C1DAEB97300AEFCA0 /* FirebaseIndexTableViewDataSourceTest.m in Sources */,
19001904
8DA941EF1D67951B00CD3685 /* FirebaseArrayTestUtils.m in Sources */,
19011905
8D924C611DA6F69100C4DA48 /* FirebaseIndexArrayTest.m in Sources */,
19021906
8DA941EE1D67951B00CD3685 /* FirebaseArrayTest.m in Sources */,

0 commit comments

Comments
 (0)