Skip to content

Commit a07cf02

Browse files
committed
Rewrite tests to ensure calls reach the delegate
1 parent 395280d commit a07cf02

File tree

5 files changed

+211
-17
lines changed

5 files changed

+211
-17
lines changed

example/e2e/asyncstorage.e2e.js

Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,21 @@ describe('Async Storage', () => {
155155

156156
describe('get / set / clear item delegate test', () => {
157157
beforeAll(async () => {
158-
if (device.getPlatform() === 'ios') {
159-
await device.openURL({url: 'rnc-asyncstorage://set-delegate'});
160-
}
161158
await test_getSetClear.tap();
159+
if (device.getPlatform() === 'android') {
160+
// Not yet supported.
161+
return;
162+
}
163+
164+
await device.openURL({url: 'rnc-asyncstorage://set-delegate'});
162165
});
163166

164-
it('should store value in async storage', async () => {
167+
it('should store value with delegate', async () => {
168+
if (device.getPlatform() === 'android') {
169+
// Not yet supported.
170+
return;
171+
}
172+
165173
const storedNumberText = await element(by.id('storedNumber_text'));
166174
const increaseByTenButton = await element(by.id('increaseByTen_button'));
167175

@@ -175,18 +183,28 @@ describe('Async Storage', () => {
175183

176184
await expect(storedNumberText).toHaveText(`${tapTimes * 10}`);
177185
await restartButton.tap();
178-
await expect(storedNumberText).toHaveText(`${tapTimes * 10}`);
186+
187+
// The delegate will distinguish itself by always returning the stored value + 1000000
188+
await expect(storedNumberText).toHaveText(`${tapTimes * 10 + 1000000}`);
179189
});
180190

181-
it('should clear item', async () => {
191+
it('should clear item with delegate', async () => {
192+
if (device.getPlatform() === 'android') {
193+
// Not yet supported.
194+
return;
195+
}
196+
182197
const storedNumberText = await element(by.id('storedNumber_text'));
183198
const increaseByTenButton = await element(by.id('increaseByTen_button'));
184199
const clearButton = await element(by.id('clear_button'));
185200

186201
await increaseByTenButton.tap();
187202
await clearButton.tap();
188203
await restartButton.tap();
189-
await expect(storedNumberText).toHaveText('');
204+
205+
// The delegate will distinguish itself by actually setting storing 1000000
206+
// instead of clearing. It will also always return the stored value + 1000000.
207+
await expect(storedNumberText).toHaveText('2000000');
190208
});
191209
});
192210

@@ -198,22 +216,61 @@ describe('Async Storage', () => {
198216
await test_mergeItem.tap();
199217
});
200218

201-
it('should crash when merging items in async storage', async () => {
219+
it('should merge items with delegate', async () => {
202220
if (device.getPlatform() === 'android') {
203221
// Not yet supported.
204222
return;
205223
}
206224

207225
const buttonMergeItem = await element(by.id('mergeItem_button'));
208-
try {
209-
await buttonMergeItem.tap();
210-
211-
// Not quite sure why ESLint thinks Jest hasn't defined fail().
212-
// eslint-disable-next-line no-undef
213-
fail();
214-
} catch {
215-
// Expected
226+
const buttonRestoreItem = await element(by.id('restoreItem_button'));
227+
228+
const nameInput = await element(by.id('testInput-name'));
229+
const ageInput = await element(by.id('testInput-age'));
230+
const eyesInput = await element(by.id('testInput-eyes'));
231+
const shoeInput = await element(by.id('testInput-shoe'));
232+
const storyText = await element(by.id('storyTextView'));
233+
234+
const isAndroid = device.getPlatform() === 'android';
235+
236+
async function performInput() {
237+
const name = Math.random() > 0.5 ? 'Jerry' : 'Sarah';
238+
const age = Math.random() > 0.5 ? '21' : '23';
239+
const eyesColor = Math.random() > 0.5 ? 'blue' : 'green';
240+
const shoeSize = Math.random() > 0.5 ? '9' : '10';
241+
242+
if (!isAndroid) {
243+
await eyesInput.tap();
244+
}
245+
await nameInput.typeText(name);
246+
await closeKeyboard.tap();
247+
248+
if (!isAndroid) {
249+
await eyesInput.tap();
250+
}
251+
await ageInput.typeText(age);
252+
await closeKeyboard.tap();
253+
254+
if (!isAndroid) {
255+
await eyesInput.tap();
256+
}
257+
await eyesInput.typeText(eyesColor);
258+
await closeKeyboard.tap();
259+
260+
if (!isAndroid) {
261+
await eyesInput.tap();
262+
}
263+
await shoeInput.typeText(shoeSize);
264+
await closeKeyboard.tap();
265+
266+
return `${name} from delegate is ${age} from delegate, has ${eyesColor} eyes and shoe size of ${shoeSize}.`;
216267
}
268+
269+
const story = await performInput();
270+
await buttonMergeItem.tap();
271+
await restartButton.tap();
272+
await buttonRestoreItem.tap();
273+
expect(storyText).toHaveText(story);
217274
});
218275
});
219276
});

example/ios/AsyncStorageExample.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
13B07FC11A68108700A75B9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; };
2323
146834051AC3E58100842450 /* libReact.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 146834041AC3E56700842450 /* libReact.a */; };
2424
196F5D682254C1530035A6D3 /* AppDelegate+RNCAsyncStorageDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 196F5D672254C1530035A6D3 /* AppDelegate+RNCAsyncStorageDelegate.m */; };
25+
19C469542256303E00CA1332 /* RNCTestAsyncStorageDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 19C469532256303E00CA1332 /* RNCTestAsyncStorageDelegate.m */; };
2526
3D82E3B72248BD39001F5D1A /* libRNCAsyncStorage.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 3DC5398C220F2C940035D3A3 /* libRNCAsyncStorage.a */; };
2627
832341BD1AAA6AB300B99B32 /* libRCTText.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 832341B51AAA6A8300B99B32 /* libRCTText.a */; };
2728
ADBDB9381DFEBF1600ED6528 /* libRCTBlob.a in Frameworks */ = {isa = PBXBuildFile; fileRef = ADBDB9271DFEBF0700ED6528 /* libRCTBlob.a */; };
@@ -323,6 +324,8 @@
323324
146833FF1AC3E56700842450 /* React.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = React.xcodeproj; path = "../../node_modules/react-native/React/React.xcodeproj"; sourceTree = "<group>"; };
324325
196F5D672254C1530035A6D3 /* AppDelegate+RNCAsyncStorageDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = "AppDelegate+RNCAsyncStorageDelegate.m"; path = "AsyncStorageExample/AppDelegate+RNCAsyncStorageDelegate.m"; sourceTree = "<group>"; };
325326
196F5D8E2254C2C90035A6D3 /* AppDelegate+RNCAsyncStorageDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "AppDelegate+RNCAsyncStorageDelegate.h"; path = "AsyncStorageExample/AppDelegate+RNCAsyncStorageDelegate.h"; sourceTree = "<group>"; };
327+
19C4692D22562FD400CA1332 /* RNCTestAsyncStorageDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = RNCTestAsyncStorageDelegate.h; path = AsyncStorageExample/RNCTestAsyncStorageDelegate.h; sourceTree = "<group>"; };
328+
19C469532256303E00CA1332 /* RNCTestAsyncStorageDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = RNCTestAsyncStorageDelegate.m; path = AsyncStorageExample/RNCTestAsyncStorageDelegate.m; sourceTree = "<group>"; };
326329
2D16E6891FA4F8E400B85C8A /* libReact.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; path = libReact.a; sourceTree = BUILT_PRODUCTS_DIR; };
327330
3DC5395A220F2C940035D3A3 /* RNCAsyncStorage.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RNCAsyncStorage.xcodeproj; path = ../../ios/RNCAsyncStorage.xcodeproj; sourceTree = "<group>"; };
328331
5E91572D1DD0AC6500FF2AA8 /* RCTAnimation.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTAnimation.xcodeproj; path = "../../node_modules/react-native/Libraries/NativeAnimation/RCTAnimation.xcodeproj"; sourceTree = "<group>"; };
@@ -428,6 +431,8 @@
428431
13B07FB01A68108700A75B9A /* AppDelegate.m */,
429432
196F5D8E2254C2C90035A6D3 /* AppDelegate+RNCAsyncStorageDelegate.h */,
430433
196F5D672254C1530035A6D3 /* AppDelegate+RNCAsyncStorageDelegate.m */,
434+
19C4692D22562FD400CA1332 /* RNCTestAsyncStorageDelegate.h */,
435+
19C469532256303E00CA1332 /* RNCTestAsyncStorageDelegate.m */,
431436
13B07FB51A68108700A75B9A /* Images.xcassets */,
432437
13B07FB61A68108700A75B9A /* Info.plist */,
433438
13B07FB11A68108700A75B9A /* LaunchScreen.xib */,
@@ -961,6 +966,7 @@
961966
buildActionMask = 2147483647;
962967
files = (
963968
13B07FBC1A68108700A75B9A /* AppDelegate.m in Sources */,
969+
19C469542256303E00CA1332 /* RNCTestAsyncStorageDelegate.m in Sources */,
964970
13B07FC11A68108700A75B9A /* main.m in Sources */,
965971
196F5D682254C1530035A6D3 /* AppDelegate+RNCAsyncStorageDelegate.m in Sources */,
966972
);

example/ios/AsyncStorageExample/AppDelegate.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#import "AppDelegate.h"
99

1010
#import "AppDelegate+RNCAsyncStorageDelegate.h"
11+
#import "RNCTestAsyncStorageDelegate.h"
1112

1213
#import <RNCAsyncStorage/RNCAsyncStorage.h>
1314
#import <React/RCTBundleURLProvider.h>
@@ -16,6 +17,7 @@
1617

1718
@implementation AppDelegate {
1819
__weak RCTBridge *_bridge;
20+
RNCTestAsyncStorageDelegate *_testDelegate;
1921
}
2022

2123
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
@@ -81,8 +83,11 @@ - (BOOL)application:(UIApplication *)app
8183
}
8284

8385
if ([url.host isEqualToString:@"set-delegate"]) {
86+
if (_testDelegate == nil) {
87+
_testDelegate = [RNCTestAsyncStorageDelegate new];
88+
}
8489
RNCAsyncStorage *asyncStorage = [_bridge moduleForClass:[RNCAsyncStorage class]];
85-
asyncStorage.delegate = self;
90+
asyncStorage.delegate = _testDelegate;
8691
} else if ([url.host isEqualToString:@"unset-delegate"]) {
8792
RNCAsyncStorage *asyncStorage = [_bridge moduleForClass:[RNCAsyncStorage class]];
8893
asyncStorage.delegate = nil;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import <RNCAsyncStorage/RNCAsyncStorageDelegate.h>
9+
10+
/*!
11+
* Implementation of |RNCAsyncStorageDelegate| used for E2E testing purposes only.
12+
*/
13+
@interface RNCTestAsyncStorageDelegate : NSObject <RNCAsyncStorageDelegate>
14+
@end
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import "RNCTestAsyncStorageDelegate.h"
9+
10+
const NSInteger RNCTestAsyncStorageExtraValue = 1000000;
11+
12+
NSString *RNCTestAddExtraValue(NSString *value)
13+
{
14+
NSInteger i = [value integerValue];
15+
if (i == 0) {
16+
return value;
17+
}
18+
return [NSString stringWithFormat:@"%ld", (long)i + RNCTestAsyncStorageExtraValue];
19+
}
20+
21+
NSString *RNCTestRemoveExtraValue(NSString *value)
22+
{
23+
NSInteger i = [value integerValue];
24+
if (i > RNCTestAsyncStorageExtraValue) {
25+
i -= RNCTestAsyncStorageExtraValue;
26+
}
27+
return [NSString stringWithFormat:@"%ld", (long)i];
28+
}
29+
30+
@implementation RNCTestAsyncStorageDelegate {
31+
NSMutableDictionary<NSString *, NSString *> *_memoryStorage;
32+
}
33+
34+
- (instancetype)init
35+
{
36+
if (self = [super init]) {
37+
_memoryStorage = [NSMutableDictionary dictionary];
38+
}
39+
return self;
40+
}
41+
42+
- (void)allKeys:(RNCAsyncStorageResultCallback)completion
43+
{
44+
completion(_memoryStorage.allKeys);
45+
}
46+
47+
- (void)mergeValues:(nonnull NSArray<NSString *> *)values
48+
forKeys:(nonnull NSArray<NSString *> *)keys
49+
completion:(nonnull RNCAsyncStorageResultCallback)completion
50+
{
51+
NSError *error = nil;
52+
NSDictionary *dictionary =
53+
[NSJSONSerialization JSONObjectWithData:[values[0] dataUsingEncoding:NSUTF8StringEncoding]
54+
options:NSJSONReadingMutableContainers
55+
error:&error];
56+
NSMutableDictionary *modified = [NSMutableDictionary dictionaryWithCapacity:dictionary.count];
57+
for (NSString *key in dictionary) {
58+
NSObject *value = dictionary[key];
59+
if ([value isKindOfClass:[NSString class]]) {
60+
modified[key] = [(NSString *)value stringByAppendingString:@" from delegate"];
61+
} else {
62+
modified[key] = value;
63+
}
64+
}
65+
66+
NSData *data = [NSJSONSerialization dataWithJSONObject:modified options:0 error:&error];
67+
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
68+
_memoryStorage[keys[0]] = str;
69+
completion(@[str]);
70+
}
71+
72+
- (void)removeAllValues:(nonnull RNCAsyncStorageCompletion)completion
73+
{
74+
[_memoryStorage removeAllObjects];
75+
completion(nil);
76+
}
77+
78+
- (void)removeValuesForKeys:(nonnull NSArray<NSString *> *)keys
79+
completion:(nonnull RNCAsyncStorageResultCallback)completion
80+
{
81+
for (NSString *key in keys) {
82+
[_memoryStorage
83+
setObject:[NSString stringWithFormat:@"%ld", (long)RNCTestAsyncStorageExtraValue]
84+
forKey:key];
85+
}
86+
completion(@[]);
87+
}
88+
89+
- (void)setValues:(nonnull NSArray<NSString *> *)values
90+
forKeys:(nonnull NSArray<NSString *> *)keys
91+
completion:(nonnull RNCAsyncStorageResultCallback)completion
92+
{
93+
for (NSUInteger i = 0; i < values.count; ++i) {
94+
NSString *value = values[i];
95+
NSString *key = keys[i];
96+
[_memoryStorage setObject:RNCTestRemoveExtraValue(value) forKey:key];
97+
}
98+
completion(@[]);
99+
}
100+
101+
- (void)valuesForKeys:(nonnull NSArray<NSString *> *)keys
102+
completion:(nonnull RNCAsyncStorageResultCallback)completion
103+
{
104+
NSMutableArray *values = [NSMutableArray arrayWithCapacity:keys.count];
105+
for (NSString *key in keys) {
106+
NSString *value = _memoryStorage[key];
107+
[values addObject:value == nil ? [NSNull null] : RNCTestAddExtraValue(value)];
108+
}
109+
completion(values);
110+
}
111+
112+
@end

0 commit comments

Comments
 (0)