Skip to content

Commit

Permalink
fix(ios): Merging empty object should not override current value (#1064)
Browse files Browse the repository at this point in the history
* fix merging

* fixup tests

* nits accepted
  • Loading branch information
krizzu authored Feb 20, 2024
1 parent 67bb6f5 commit ea9fc7a
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 25 deletions.
1 change: 1 addition & 0 deletions packages/default-storage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ to learn more.
2. Build app and run tests
```shell
yarn bundle:ios
pod install --project-directory=example/ios
yarn build:e2e:ios
yarn test:e2e:ios
```
Expand Down
24 changes: 13 additions & 11 deletions packages/default-storage/example/examples/Functional.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,21 @@ async function executeStep(step: TestStep): Promise<void> {

async function execute(steps: TestStep[]): Promise<void> {
const numSteps = steps.length;
for (let i = 0; i < numSteps; ++i) {
try {
await executeStep(steps[i]);
} catch (e) {
if (!Array.isArray(e)) {
throw e;
}
try {
for (let i = 0; i < numSteps; ++i) {
try {
await executeStep(steps[i]);
} catch (e) {
if (!Array.isArray(e)) {
throw e;
}

const [expected, actual] = e;
throw { step: i, expected, actual };
} finally {
await AsyncStorage.clear();
const [expected, actual] = e;
throw { step: i, expected, actual };
}
}
} finally {
await AsyncStorage.clear();
}
}

Expand Down
33 changes: 30 additions & 3 deletions packages/default-storage/example/examples/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type TestStep =
};

const tests: Record<string, TestStep[]> = {
"Should store value in AsyncStorage": [
"Should store value": [
{ command: "set", key: "a", value: "0" },
{ command: "set", key: "a", value: "10" },
{ command: "set", key: "a", value: "20" },
Expand All @@ -31,8 +31,7 @@ const tests: Record<string, TestStep[]> = {
value: {
name: "Jerry",
age: "21",
eyesColor: "blue",
shoeSize: "9",
shoeSize: "10",
},
},
{
Expand All @@ -42,10 +41,38 @@ const tests: Record<string, TestStep[]> = {
name: "Sarah",
age: "23",
eyesColor: "green",
},
expected: {
name: "Sarah",
age: "23",
eyesColor: "green",
shoeSize: "10",
},
},
],
"Should keep existing entries when merging with an empty object": [
{
command: "set",
key: "merge_test_2",
value: {
name: "Jerry",
age: "21",
eyesColor: "blue",
shoeSize: "9",
},
},
{
command: "merge",
key: "merge_test_2",
value: {},
expected: {
name: "Jerry",
age: "21",
eyesColor: "blue",
shoeSize: "9",
},
},
],
};

export default tests;
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,19 @@ - (void)mergeValues:(NSArray<NSString *> *)values
completion:(RNCAsyncStorageResultCallback)completion
{
NSError *error = nil;
NSDictionary *dictionary =
NSMutableDictionary *destination = [NSJSONSerialization
JSONObjectWithData:[_memoryStorage[keys[0]] dataUsingEncoding:NSUTF8StringEncoding]
options:NSJSONReadingMutableContainers
error:&error];

NSDictionary *source =
[NSJSONSerialization JSONObjectWithData:[values[0] dataUsingEncoding:NSUTF8StringEncoding]
options:NSJSONReadingMutableContainers
error:&error];
NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];

[destination addEntriesFromDictionary:source];

NSData *data = [NSJSONSerialization dataWithJSONObject:destination options:0 error:&error];
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
_memoryStorage[keys[0]] = str;
completion(@[str]);
Expand Down
3 changes: 2 additions & 1 deletion packages/default-storage/ios/RNCAsyncStorage.mm
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,8 @@ - (BOOL)_passthroughDelegate
if (value) {
NSError *jsonError;
NSMutableDictionary *mergedVal = RCTJSONParseMutable(value, &jsonError);
if (RCTMergeRecursive(mergedVal, RCTJSONParse(entry[1], &jsonError))) {
NSDictionary *mergingValue = RCTJSONParse(entry[1], &jsonError);
if (!mergingValue.count || RCTMergeRecursive(mergedVal, mergingValue)) {
entry = @[entry[0], RCTNullIfNil(RCTJSONStringify(mergedVal, NULL))];
}
if (jsonError) {
Expand Down
9 changes: 1 addition & 8 deletions packages/default-storage/scripts/ios_e2e.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
#!/bin/bash

RESOURCE_DIR="$PWD/example/ios/build/Build/Products/Release-iphonesimulator/ReactTestApp.app"
ENTRY_FILE="example/index.ts"
BUNDLE_FILE="$RESOURCE_DIR/index.ios.jsbundle"
EXTRA_PACKAGER_ARGS="--entry-file=$ENTRY_FILE"

build_project() {
echo "[iOS E2E] Building iOS project"
eval "xcodebuild \
-workspace example/ios/AsyncStorageExample.xcworkspace \
-scheme ReactTestApp \
-configuration Release \
-sdk iphonesimulator \
-derivedDataPath example/ios/build \
BUNDLE_FILE=$BUNDLE_FILE \
EXTRA_PACKAGER_ARGS=$EXTRA_PACKAGER_ARGS"
-derivedDataPath example/ios/build"
}

bundle_js() {
Expand Down

0 comments on commit ea9fc7a

Please sign in to comment.