|
| 1 | +import { describe, expect, test } from "vitest"; |
| 2 | +import { validateReactArgs } from "./validateReactArgs"; |
| 3 | +import { connectorConfig } from "@/dataconnect/default-connector"; |
| 4 | +import { getDataConnect } from "firebase/data-connect"; |
| 5 | +import { firebaseApp } from "~/testing-utils"; |
| 6 | + |
| 7 | +// initialize firebase app |
| 8 | +firebaseApp; |
| 9 | + |
| 10 | +describe("validateReactArgs", () => { |
| 11 | + const dataConnect = getDataConnect(connectorConfig); |
| 12 | + |
| 13 | + const emptyObjectVars = {}; |
| 14 | + const nonEmptyVars = { limit: 5 }; |
| 15 | + |
| 16 | + const options = { meta: { hasOptions: true } }; |
| 17 | + |
| 18 | + test.each([ |
| 19 | + { |
| 20 | + argsDescription: "no args are provided", |
| 21 | + dcOrOptions: undefined, |
| 22 | + options: undefined, |
| 23 | + expectedInputVars: undefined, |
| 24 | + expectedInputOpts: undefined, |
| 25 | + }, |
| 26 | + { |
| 27 | + argsDescription: "only dataconnect is provided", |
| 28 | + dcOrOptions: dataConnect, |
| 29 | + options: undefined, |
| 30 | + expectedInputVars: undefined, |
| 31 | + expectedInputOpts: undefined, |
| 32 | + }, |
| 33 | + { |
| 34 | + argsDescription: "only options are provided", |
| 35 | + dcOrOptions: options, |
| 36 | + options: undefined, |
| 37 | + expectedInputVars: undefined, |
| 38 | + expectedInputOpts: options, |
| 39 | + }, |
| 40 | + { |
| 41 | + argsDescription: "dataconnect and options are provided", |
| 42 | + dcOrOptions: dataConnect, |
| 43 | + options: options, |
| 44 | + expectedInputVars: undefined, |
| 45 | + expectedInputOpts: options, |
| 46 | + }, |
| 47 | + ])( |
| 48 | + "parses args correctly when $argsDescription for an operation with no variables", |
| 49 | + ({ dcOrOptions, options, expectedInputVars, expectedInputOpts }) => { |
| 50 | + const { |
| 51 | + dc: dcInstance, |
| 52 | + vars: inputVars, |
| 53 | + options: inputOpts, |
| 54 | + } = validateReactArgs( |
| 55 | + connectorConfig, |
| 56 | + dcOrOptions, |
| 57 | + options |
| 58 | + // hasVars = undefined (false-y) |
| 59 | + // validateArgs = undefined (false-y) |
| 60 | + ); |
| 61 | + |
| 62 | + expect(dcInstance).toBe(dataConnect); |
| 63 | + expect(inputVars).toBe(expectedInputVars); |
| 64 | + expect(inputOpts).toBe(expectedInputOpts); |
| 65 | + } |
| 66 | + ); |
| 67 | + |
| 68 | + test.each([ |
| 69 | + { |
| 70 | + argsDescription: "no args are provided", |
| 71 | + dcOrVarsOrOptions: undefined, |
| 72 | + varsOrOptions: undefined, |
| 73 | + options: undefined, |
| 74 | + expectedInputVars: undefined, |
| 75 | + expectedInputOpts: undefined, |
| 76 | + }, |
| 77 | + { |
| 78 | + argsDescription: "only dataconnect is provided", |
| 79 | + dcOrVarsOrOptions: dataConnect, |
| 80 | + varsOrOptions: undefined, |
| 81 | + options: undefined, |
| 82 | + expectedInputVars: undefined, |
| 83 | + expectedInputOpts: undefined, |
| 84 | + }, |
| 85 | + { |
| 86 | + argsDescription: "only an empty vars object is provided", |
| 87 | + dcOrVarsOrOptions: emptyObjectVars, |
| 88 | + varsOrOptions: undefined, |
| 89 | + options: undefined, |
| 90 | + expectedInputVars: emptyObjectVars, |
| 91 | + expectedInputOpts: undefined, |
| 92 | + }, |
| 93 | + { |
| 94 | + argsDescription: "only vars are provided", |
| 95 | + dcOrVarsOrOptions: nonEmptyVars, |
| 96 | + varsOrOptions: undefined, |
| 97 | + options: undefined, |
| 98 | + expectedInputVars: nonEmptyVars, |
| 99 | + expectedInputOpts: undefined, |
| 100 | + }, |
| 101 | + { |
| 102 | + argsDescription: "only options are provided", |
| 103 | + dcOrVarsOrOptions: undefined, |
| 104 | + varsOrOptions: options, |
| 105 | + options: undefined, |
| 106 | + expectedInputVars: undefined, |
| 107 | + expectedInputOpts: options, |
| 108 | + }, |
| 109 | + { |
| 110 | + argsDescription: "dataconnect and vars are provided", |
| 111 | + dcOrVarsOrOptions: dataConnect, |
| 112 | + varsOrOptions: nonEmptyVars, |
| 113 | + options: undefined, |
| 114 | + expectedInputVars: nonEmptyVars, |
| 115 | + expectedInputOpts: undefined, |
| 116 | + }, |
| 117 | + { |
| 118 | + argsDescription: "dataconnect and options are provided", |
| 119 | + dcOrVarsOrOptions: dataConnect, |
| 120 | + varsOrOptions: undefined, |
| 121 | + options: options, |
| 122 | + expectedInputVars: undefined, |
| 123 | + expectedInputOpts: options, |
| 124 | + }, |
| 125 | + { |
| 126 | + argsDescription: "dataconnect and vars and options are provided", |
| 127 | + dcOrVarsOrOptions: dataConnect, |
| 128 | + varsOrOptions: nonEmptyVars, |
| 129 | + options: options, |
| 130 | + expectedInputVars: nonEmptyVars, |
| 131 | + expectedInputOpts: options, |
| 132 | + }, |
| 133 | + ])( |
| 134 | + "parses args correctly when $argsDescription for an operation with all optional variables", |
| 135 | + ({ |
| 136 | + dcOrVarsOrOptions, |
| 137 | + varsOrOptions, |
| 138 | + options, |
| 139 | + expectedInputVars, |
| 140 | + expectedInputOpts, |
| 141 | + }) => { |
| 142 | + const { |
| 143 | + dc: dcInstance, |
| 144 | + vars: inputVars, |
| 145 | + options: inputOpts, |
| 146 | + } = validateReactArgs( |
| 147 | + connectorConfig, |
| 148 | + dcOrVarsOrOptions, |
| 149 | + varsOrOptions, |
| 150 | + options, |
| 151 | + true, // hasVars = true |
| 152 | + false // validateArgs = false |
| 153 | + ); |
| 154 | + |
| 155 | + expect(dcInstance).toBe(dataConnect); |
| 156 | + expect(inputVars).toBe(expectedInputVars); |
| 157 | + expect(inputOpts).toBe(expectedInputOpts); |
| 158 | + } |
| 159 | + ); |
| 160 | + |
| 161 | + test.each([ |
| 162 | + { |
| 163 | + argsDescription: "only vars are provided", |
| 164 | + dcOrVarsOrOptions: nonEmptyVars, |
| 165 | + varsOrOptions: undefined, |
| 166 | + options: undefined, |
| 167 | + expectedInputVars: nonEmptyVars, |
| 168 | + expectedInputOpts: undefined, |
| 169 | + }, |
| 170 | + { |
| 171 | + argsDescription: "dataconnect and vars are provided", |
| 172 | + dcOrVarsOrOptions: dataConnect, |
| 173 | + varsOrOptions: nonEmptyVars, |
| 174 | + options: undefined, |
| 175 | + expectedInputVars: nonEmptyVars, |
| 176 | + expectedInputOpts: undefined, |
| 177 | + }, |
| 178 | + { |
| 179 | + argsDescription: "vars and options are provided", |
| 180 | + dcOrVarsOrOptions: nonEmptyVars, |
| 181 | + varsOrOptions: options, |
| 182 | + options: undefined, |
| 183 | + expectedInputVars: nonEmptyVars, |
| 184 | + expectedInputOpts: options, |
| 185 | + }, |
| 186 | + { |
| 187 | + argsDescription: "dataconnect and vars and options are provided", |
| 188 | + dcOrVarsOrOptions: dataConnect, |
| 189 | + varsOrOptions: nonEmptyVars, |
| 190 | + options: options, |
| 191 | + expectedInputVars: nonEmptyVars, |
| 192 | + expectedInputOpts: options, |
| 193 | + }, |
| 194 | + ])( |
| 195 | + "parses args correctly when $argsDescription for an operation with any required variables", |
| 196 | + ({ |
| 197 | + dcOrVarsOrOptions, |
| 198 | + varsOrOptions, |
| 199 | + options, |
| 200 | + expectedInputVars, |
| 201 | + expectedInputOpts, |
| 202 | + }) => { |
| 203 | + const { |
| 204 | + dc: dcInstance, |
| 205 | + vars: inputVars, |
| 206 | + options: inputOpts, |
| 207 | + } = validateReactArgs( |
| 208 | + connectorConfig, |
| 209 | + dcOrVarsOrOptions, |
| 210 | + varsOrOptions, |
| 211 | + options, |
| 212 | + true, // hasVars = true |
| 213 | + true // validateArgs = true |
| 214 | + ); |
| 215 | + |
| 216 | + expect(dcInstance).toBe(dataConnect); |
| 217 | + expect(inputVars).toBe(expectedInputVars); |
| 218 | + expect(inputOpts).toBe(expectedInputOpts); |
| 219 | + } |
| 220 | + ); |
| 221 | + |
| 222 | + test.each([ |
| 223 | + { |
| 224 | + argsDescription: "only dataconnect is provided", |
| 225 | + dcOrVarsOrOptions: dataConnect, |
| 226 | + varsOrOptions: undefined, |
| 227 | + options: undefined, |
| 228 | + }, |
| 229 | + { |
| 230 | + argsDescription: "only options are provided", |
| 231 | + dcOrVarsOrOptions: undefined, |
| 232 | + varsOrOptions: options, |
| 233 | + options: undefined, |
| 234 | + }, |
| 235 | + { |
| 236 | + argsDescription: "only dataconnect and options are provided", |
| 237 | + dcOrVarsOrOptions: dataConnect, |
| 238 | + varsOrOptions: undefined, |
| 239 | + options: options, |
| 240 | + }, |
| 241 | + ])( |
| 242 | + "throws error when $argsDescription for an operation with any required variables", |
| 243 | + ({ dcOrVarsOrOptions, varsOrOptions, options }) => { |
| 244 | + expect(() => { |
| 245 | + validateReactArgs( |
| 246 | + connectorConfig, |
| 247 | + dcOrVarsOrOptions, |
| 248 | + varsOrOptions, |
| 249 | + options, |
| 250 | + true, // hasVars = true |
| 251 | + true // validateArgs = true |
| 252 | + ); |
| 253 | + }).toThrowError("invalid-argument: Variables required."); |
| 254 | + } |
| 255 | + ); |
| 256 | +}); |
0 commit comments