Skip to content

Commit ea9fc7a

Browse files
authored
fix(ios): Merging empty object should not override current value (#1064)
* fix merging * fixup tests * nits accepted
1 parent 67bb6f5 commit ea9fc7a

File tree

6 files changed

+57
-25
lines changed

6 files changed

+57
-25
lines changed

packages/default-storage/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ to learn more.
3535
2. Build app and run tests
3636
```shell
3737
yarn bundle:ios
38+
pod install --project-directory=example/ios
3839
yarn build:e2e:ios
3940
yarn test:e2e:ios
4041
```

packages/default-storage/example/examples/Functional.tsx

+13-11
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,21 @@ async function executeStep(step: TestStep): Promise<void> {
7979

8080
async function execute(steps: TestStep[]): Promise<void> {
8181
const numSteps = steps.length;
82-
for (let i = 0; i < numSteps; ++i) {
83-
try {
84-
await executeStep(steps[i]);
85-
} catch (e) {
86-
if (!Array.isArray(e)) {
87-
throw e;
88-
}
82+
try {
83+
for (let i = 0; i < numSteps; ++i) {
84+
try {
85+
await executeStep(steps[i]);
86+
} catch (e) {
87+
if (!Array.isArray(e)) {
88+
throw e;
89+
}
8990

90-
const [expected, actual] = e;
91-
throw { step: i, expected, actual };
92-
} finally {
93-
await AsyncStorage.clear();
91+
const [expected, actual] = e;
92+
throw { step: i, expected, actual };
93+
}
9494
}
95+
} finally {
96+
await AsyncStorage.clear();
9597
}
9698
}
9799

packages/default-storage/example/examples/tests.ts

+30-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type TestStep =
1313
};
1414

1515
const tests: Record<string, TestStep[]> = {
16-
"Should store value in AsyncStorage": [
16+
"Should store value": [
1717
{ command: "set", key: "a", value: "0" },
1818
{ command: "set", key: "a", value: "10" },
1919
{ command: "set", key: "a", value: "20" },
@@ -31,8 +31,7 @@ const tests: Record<string, TestStep[]> = {
3131
value: {
3232
name: "Jerry",
3333
age: "21",
34-
eyesColor: "blue",
35-
shoeSize: "9",
34+
shoeSize: "10",
3635
},
3736
},
3837
{
@@ -42,10 +41,38 @@ const tests: Record<string, TestStep[]> = {
4241
name: "Sarah",
4342
age: "23",
4443
eyesColor: "green",
44+
},
45+
expected: {
46+
name: "Sarah",
47+
age: "23",
48+
eyesColor: "green",
4549
shoeSize: "10",
4650
},
4751
},
4852
],
53+
"Should keep existing entries when merging with an empty object": [
54+
{
55+
command: "set",
56+
key: "merge_test_2",
57+
value: {
58+
name: "Jerry",
59+
age: "21",
60+
eyesColor: "blue",
61+
shoeSize: "9",
62+
},
63+
},
64+
{
65+
command: "merge",
66+
key: "merge_test_2",
67+
value: {},
68+
expected: {
69+
name: "Jerry",
70+
age: "21",
71+
eyesColor: "blue",
72+
shoeSize: "9",
73+
},
74+
},
75+
],
4976
};
5077

5178
export default tests;

packages/default-storage/example/ios/AsyncStorageExample/AsyncStorageDevSupport.m

+10-2
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,19 @@ - (void)mergeValues:(NSArray<NSString *> *)values
7171
completion:(RNCAsyncStorageResultCallback)completion
7272
{
7373
NSError *error = nil;
74-
NSDictionary *dictionary =
74+
NSMutableDictionary *destination = [NSJSONSerialization
75+
JSONObjectWithData:[_memoryStorage[keys[0]] dataUsingEncoding:NSUTF8StringEncoding]
76+
options:NSJSONReadingMutableContainers
77+
error:&error];
78+
79+
NSDictionary *source =
7580
[NSJSONSerialization JSONObjectWithData:[values[0] dataUsingEncoding:NSUTF8StringEncoding]
7681
options:NSJSONReadingMutableContainers
7782
error:&error];
78-
NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];
83+
84+
[destination addEntriesFromDictionary:source];
85+
86+
NSData *data = [NSJSONSerialization dataWithJSONObject:destination options:0 error:&error];
7987
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
8088
_memoryStorage[keys[0]] = str;
8189
completion(@[str]);

packages/default-storage/ios/RNCAsyncStorage.mm

+2-1
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,8 @@ - (BOOL)_passthroughDelegate
787787
if (value) {
788788
NSError *jsonError;
789789
NSMutableDictionary *mergedVal = RCTJSONParseMutable(value, &jsonError);
790-
if (RCTMergeRecursive(mergedVal, RCTJSONParse(entry[1], &jsonError))) {
790+
NSDictionary *mergingValue = RCTJSONParse(entry[1], &jsonError);
791+
if (!mergingValue.count || RCTMergeRecursive(mergedVal, mergingValue)) {
791792
entry = @[entry[0], RCTNullIfNil(RCTJSONStringify(mergedVal, NULL))];
792793
}
793794
if (jsonError) {

packages/default-storage/scripts/ios_e2e.sh

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
#!/bin/bash
22

3-
RESOURCE_DIR="$PWD/example/ios/build/Build/Products/Release-iphonesimulator/ReactTestApp.app"
4-
ENTRY_FILE="example/index.ts"
5-
BUNDLE_FILE="$RESOURCE_DIR/index.ios.jsbundle"
6-
EXTRA_PACKAGER_ARGS="--entry-file=$ENTRY_FILE"
7-
83
build_project() {
94
echo "[iOS E2E] Building iOS project"
105
eval "xcodebuild \
116
-workspace example/ios/AsyncStorageExample.xcworkspace \
127
-scheme ReactTestApp \
138
-configuration Release \
149
-sdk iphonesimulator \
15-
-derivedDataPath example/ios/build \
16-
BUNDLE_FILE=$BUNDLE_FILE \
17-
EXTRA_PACKAGER_ARGS=$EXTRA_PACKAGER_ARGS"
10+
-derivedDataPath example/ios/build"
1811
}
1912

2013
bundle_js() {

0 commit comments

Comments
 (0)