|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and 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 | +#pragma once |
| 9 | + |
| 10 | +#include <gtest/gtest.h> |
| 11 | + |
| 12 | +#include <react/renderer/core/DynamicPropsUtilities.h> |
| 13 | +#include <react/renderer/core/PropsParserContext.h> |
| 14 | +#include <react/renderer/core/RawProps.h> |
| 15 | +#include <react/renderer/core/RawPropsParser.h> |
| 16 | +#include <react/utils/ContextContainer.h> |
| 17 | + |
| 18 | +#include <algorithm> |
| 19 | +#include <cmath> |
| 20 | +#include <memory> |
| 21 | +#include <set> |
| 22 | +#include <string> |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +#ifdef RN_SERIALIZABLE_STATE |
| 26 | + |
| 27 | +namespace facebook::react { |
| 28 | + |
| 29 | +/* |
| 30 | + * One commit: the props JS sends (a removed prop is null), and the Props 2.0 |
| 31 | + * values that intentionally differ from what Props 1.5 sends. |
| 32 | + */ |
| 33 | +struct PropsCommit { |
| 34 | + folly::dynamic props; |
| 35 | + folly::dynamic intendedDifferences = folly::dynamic::object(); |
| 36 | +}; |
| 37 | + |
| 38 | +namespace detail { |
| 39 | + |
| 40 | +inline bool payloadValuesMatch(const folly::dynamic &actual, const folly::dynamic &expected) |
| 41 | +{ |
| 42 | + if (actual.isNumber() && expected.isNumber()) { |
| 43 | + // Props 2.0 stores floats, so 0.998 comes back as 0.99800002574... |
| 44 | + auto tolerance = 1e-6 * std::max(1.0, std::abs(expected.asDouble())); |
| 45 | + return std::abs(actual.asDouble() - expected.asDouble()) <= tolerance; |
| 46 | + } |
| 47 | + if (actual.isArray() && expected.isArray()) { |
| 48 | + return actual.size() == expected.size() && |
| 49 | + std::equal(actual.begin(), actual.end(), expected.begin(), payloadValuesMatch); |
| 50 | + } |
| 51 | + if (actual.isObject() && expected.isObject()) { |
| 52 | + return actual.size() == expected.size() && std::ranges::all_of(expected.items(), [&](const auto &item) { |
| 53 | + return actual.count(item.first) != 0 && payloadValuesMatch(actual[item.first], item.second); |
| 54 | + }); |
| 55 | + } |
| 56 | + return actual == expected; |
| 57 | +} |
| 58 | + |
| 59 | +// Each element carries the fields of the expected element, and may add more. |
| 60 | +inline bool elementsContainFields(const folly::dynamic &actual, const folly::dynamic &expected) |
| 61 | +{ |
| 62 | + if (!actual.isArray() || !expected.isArray() || actual.size() != expected.size()) { |
| 63 | + return payloadValuesMatch(actual, expected); |
| 64 | + } |
| 65 | + for (size_t i = 0; i < expected.size(); i++) { |
| 66 | + for (const auto &[field, value] : expected[i].items()) { |
| 67 | + if (actual[i].count(field) == 0 || !payloadValuesMatch(actual[i][field], value)) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + return true; |
| 73 | +} |
| 74 | + |
| 75 | +} // namespace detail |
| 76 | + |
| 77 | +/* |
| 78 | + * Applies the commits in order and checks that each Props 2.0 payload |
| 79 | + * (`getDiffProps`) has the keys, nulls and values of the Props 1.5 payload |
| 80 | + * (accumulated raw props), apart from the commit's intended differences. The |
| 81 | + * elements of the arrays under `keysWithAddedFields` may carry fields that JS |
| 82 | + * does not send. |
| 83 | + */ |
| 84 | +template <typename PropsT> |
| 85 | +void expectPayloadsMatchRawProps( |
| 86 | + const std::vector<PropsCommit> &commits, |
| 87 | + const std::set<std::string> &keysWithAddedFields = {}) |
| 88 | +{ |
| 89 | + ContextContainer contextContainer{}; |
| 90 | + PropsParserContext parserContext{-1, contextContainer}; |
| 91 | + |
| 92 | + auto previous = std::make_shared<const PropsT>(); |
| 93 | + folly::dynamic previousRawProps = folly::dynamic::object(); |
| 94 | + for (size_t i = 0; i < commits.size(); i++) { |
| 95 | + const auto &commit = commits[i]; |
| 96 | + auto rawProps = RawProps(commit.props); |
| 97 | + auto parser = RawPropsParser(); |
| 98 | + parser.template prepare<PropsT>(); |
| 99 | + rawProps.parse(parser); |
| 100 | + auto props = std::make_shared<const PropsT>(parserContext, *previous, rawProps); |
| 101 | + auto accumulatedRawProps = mergeDynamicProps(previousRawProps, commit.props, NullValueStrategy::Override); |
| 102 | + |
| 103 | + auto actual = props->getDiffProps(i == 0 ? nullptr : previous.get()); |
| 104 | + auto expected = i == 0 ? accumulatedRawProps : diffDynamicProps(previousRawProps, accumulatedRawProps); |
| 105 | + for (const auto &[key, value] : commit.intendedDifferences.items()) { |
| 106 | + expected[key] = value; |
| 107 | + } |
| 108 | + |
| 109 | + std::set<std::string> keys; |
| 110 | + for (const auto &payload : {actual, expected}) { |
| 111 | + for (const auto &key : payload.keys()) { |
| 112 | + keys.insert(key.asString()); |
| 113 | + } |
| 114 | + } |
| 115 | + for (const auto &key : keys) { |
| 116 | + bool matches = actual.count(key) != 0 && expected.count(key) != 0 && |
| 117 | + (keysWithAddedFields.contains(key) ? detail::elementsContainFields(actual[key], expected[key]) |
| 118 | + : detail::payloadValuesMatch(actual[key], expected[key])); |
| 119 | + EXPECT_TRUE(matches) << "commit " << i << " " << commit.props << ", key '" << key |
| 120 | + << "'\n Props 2.0: " << actual.getDefault(key, "(absent)") |
| 121 | + << "\n expected: " << expected.getDefault(key, "(absent)"); |
| 122 | + } |
| 123 | + |
| 124 | + previous = props; |
| 125 | + previousRawProps = accumulatedRawProps; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +} // namespace facebook::react |
| 130 | + |
| 131 | +#endif |
0 commit comments