|
| 1 | +package com.revenuecat.purchases.paywalls.components.common |
| 2 | + |
| 3 | +import com.revenuecat.purchases.InternalRevenueCatAPI |
| 4 | +import com.revenuecat.purchases.JsonTools |
| 5 | +import com.revenuecat.purchases.paywalls.components.ButtonComponent |
| 6 | +import org.assertj.core.api.Assertions.assertThat |
| 7 | +import org.intellij.lang.annotations.Language |
| 8 | +import org.junit.Test |
| 9 | +import kotlinx.serialization.json.JsonPrimitive |
| 10 | + |
| 11 | +@OptIn(InternalRevenueCatAPI::class) |
| 12 | +internal class PaywallComponentStateTests { |
| 13 | + |
| 14 | + // region state_declarations map |
| 15 | + |
| 16 | + @Test |
| 17 | + fun `decodes a state_declarations map with all value types`() { |
| 18 | + @Language("json") |
| 19 | + val json = """ |
| 20 | + { |
| 21 | + "planComparisonOpen": { "type": "boolean", "default": false }, |
| 22 | + "activeSlide": { "type": "integer", "default": 0 }, |
| 23 | + "discountMultiplier": { "type": "double", "default": 0.5 }, |
| 24 | + "selectedFeatureTab": { "type": "string", "default": "billing" } |
| 25 | + } |
| 26 | + """.trimIndent() |
| 27 | + |
| 28 | + val actual = JsonTools.json.decodeFromString(StateDeclarationMapSerializer, json) |
| 29 | + |
| 30 | + assertThat(actual).hasSize(4) |
| 31 | + assertThat(actual["planComparisonOpen"]?.type).isEqualTo(StateDeclaration.ValueType.BOOLEAN) |
| 32 | + assertThat(actual["planComparisonOpen"]?.defaultValue).isEqualTo(JsonPrimitive(false)) |
| 33 | + assertThat(actual["activeSlide"]?.type).isEqualTo(StateDeclaration.ValueType.INTEGER) |
| 34 | + assertThat(actual["activeSlide"]?.defaultValue).isEqualTo(JsonPrimitive(0)) |
| 35 | + assertThat(actual["discountMultiplier"]?.type).isEqualTo(StateDeclaration.ValueType.DOUBLE) |
| 36 | + assertThat(actual["discountMultiplier"]?.defaultValue).isEqualTo(JsonPrimitive(0.5)) |
| 37 | + assertThat(actual["selectedFeatureTab"]?.type).isEqualTo(StateDeclaration.ValueType.STRING) |
| 38 | + assertThat(actual["selectedFeatureTab"]?.defaultValue).isEqualTo(JsonPrimitive("billing")) |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + fun `ignores unknown fields in a state declaration`() { |
| 43 | + @Language("json") |
| 44 | + val json = """{ "k": { "type": "string", "default": "v", "future_field": 42 } }""" |
| 45 | + |
| 46 | + val actual = JsonTools.json.decodeFromString(StateDeclarationMapSerializer, json) |
| 47 | + |
| 48 | + assertThat(actual["k"]).isEqualTo(StateDeclaration(type = "string", defaultValue = JsonPrimitive("v"))) |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun `drops a malformed entry without failing the rest of the map`() { |
| 53 | + @Language("json") |
| 54 | + val json = """ |
| 55 | + { |
| 56 | + "good": { "type": "string", "default": "v" }, |
| 57 | + "bad": { "type": "string", "default": { "nested": true } } |
| 58 | + } |
| 59 | + """.trimIndent() |
| 60 | + |
| 61 | + val actual = JsonTools.json.decodeFromString(StateDeclarationMapSerializer, json) |
| 62 | + |
| 63 | + assertThat(actual.keys).containsExactly("good") |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun `decodes a non-object state_declarations value as an empty map`() { |
| 68 | + val actual = JsonTools.json.decodeFromString(StateDeclarationMapSerializer, """"not_an_object"""") |
| 69 | + |
| 70 | + assertThat(actual).isEmpty() |
| 71 | + } |
| 72 | + |
| 73 | + // endregion |
| 74 | + |
| 75 | + // region stateUpdates |
| 76 | + |
| 77 | + @Test |
| 78 | + fun `decodes a set update with each literal type`() { |
| 79 | + assertThat(decodeUpdate("""{ "set": "k", "to": "billing" }""")) |
| 80 | + .isEqualTo(StateUpdate.Set("k", StateUpdateValue.Literal(JsonPrimitive("billing")))) |
| 81 | + assertThat(decodeUpdate("""{ "set": "k", "to": 3 }""")) |
| 82 | + .isEqualTo(StateUpdate.Set("k", StateUpdateValue.Literal(JsonPrimitive(3)))) |
| 83 | + assertThat(decodeUpdate("""{ "set": "k", "to": 0.5 }""")) |
| 84 | + .isEqualTo(StateUpdate.Set("k", StateUpdateValue.Literal(JsonPrimitive(0.5)))) |
| 85 | + assertThat(decodeUpdate("""{ "set": "k", "to": true }""")) |
| 86 | + .isEqualTo(StateUpdate.Set("k", StateUpdateValue.Literal(JsonPrimitive(true)))) |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + fun `decodes a set update with the value payload reference`() { |
| 91 | + assertThat(decodeUpdate("""{ "set": "activeSlide", "to": "${'$'}value" }""")) |
| 92 | + .isEqualTo(StateUpdate.Set("activeSlide", StateUpdateValue.PayloadReference)) |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + fun `ignores unknown fields in a set update`() { |
| 97 | + assertThat(decodeUpdate("""{ "set": "k", "to": true, "op": "future" }""")) |
| 98 | + .isEqualTo(StateUpdate.Set("k", StateUpdateValue.Literal(JsonPrimitive(true)))) |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + fun `decodes unknown or malformed update shapes as Unsupported`() { |
| 103 | + assertThat(decodeUpdate("""{ "toggle": "k" }""")).isEqualTo(StateUpdate.Unsupported) |
| 104 | + assertThat(decodeUpdate("""{ "set": "k" }""")).isEqualTo(StateUpdate.Unsupported) |
| 105 | + assertThat(decodeUpdate("""{ "to": true }""")).isEqualTo(StateUpdate.Unsupported) |
| 106 | + assertThat(decodeUpdate("""{ "set": "k", "to": [1, 2] }""")).isEqualTo(StateUpdate.Unsupported) |
| 107 | + assertThat(decodeUpdate(""""not_an_object"""")).isEqualTo(StateUpdate.Unsupported) |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + fun `degrades a malformed entry in a stateUpdates list without failing the others`() { |
| 112 | + @Language("json") |
| 113 | + val json = """ |
| 114 | + [ |
| 115 | + { "set": "a", "to": true }, |
| 116 | + { "unknown": "shape" }, |
| 117 | + { "set": "b", "to": "${'$'}value" } |
| 118 | + ] |
| 119 | + """.trimIndent() |
| 120 | + |
| 121 | + val actual = JsonTools.json.decodeFromString<List<StateUpdate>>(json) |
| 122 | + |
| 123 | + assertThat(actual).containsExactly( |
| 124 | + StateUpdate.Set("a", StateUpdateValue.Literal(JsonPrimitive(true))), |
| 125 | + StateUpdate.Unsupported, |
| 126 | + StateUpdate.Set("b", StateUpdateValue.PayloadReference), |
| 127 | + ) |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + fun `decodes stateUpdates wired onto an interactive component`() { |
| 132 | + @Language("json") |
| 133 | + val json = """ |
| 134 | + { |
| 135 | + "type": "button", |
| 136 | + "action": { "type": "restore_purchases" }, |
| 137 | + "stack": { "type": "stack", "components": [] }, |
| 138 | + "stateUpdates": [ { "set": "planComparisonOpen", "to": true } ] |
| 139 | + } |
| 140 | + """.trimIndent() |
| 141 | + |
| 142 | + val actual = JsonTools.json.decodeFromString<ButtonComponent>(json) |
| 143 | + |
| 144 | + assertThat(actual.stateUpdates).containsExactly( |
| 145 | + StateUpdate.Set("planComparisonOpen", StateUpdateValue.Literal(JsonPrimitive(true))), |
| 146 | + ) |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + fun `decodes a component without stateUpdates as null`() { |
| 151 | + @Language("json") |
| 152 | + val json = """ |
| 153 | + { |
| 154 | + "type": "button", |
| 155 | + "action": { "type": "restore_purchases" }, |
| 156 | + "stack": { "type": "stack", "components": [] } |
| 157 | + } |
| 158 | + """.trimIndent() |
| 159 | + |
| 160 | + val actual = JsonTools.json.decodeFromString<ButtonComponent>(json) |
| 161 | + |
| 162 | + assertThat(actual.stateUpdates).isNull() |
| 163 | + } |
| 164 | + |
| 165 | + // endregion |
| 166 | + |
| 167 | + private fun decodeUpdate(@Language("json") json: String): StateUpdate = |
| 168 | + JsonTools.json.decodeFromString(json) |
| 169 | +} |
0 commit comments