|
| 1 | +import { AttributeSchema, RelationshipSchema } from "@/entities/schema/types"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { generateAttributeSchema, generateRelationshipSchema } from "../../../../tests/fake/schema"; |
| 4 | +import { addAttributesToRequest, addRelationshipsToRequest } from "./utils"; |
| 5 | + |
| 6 | +describe("addAttributesToRequest", () => { |
| 7 | + it("should return base fragment for simple attribute", () => { |
| 8 | + // GIVEN |
| 9 | + const attributes: Array<AttributeSchema> = [ |
| 10 | + generateAttributeSchema({ name: "test", kind: "Text" }), |
| 11 | + ]; |
| 12 | + |
| 13 | + // WHEN |
| 14 | + const result = addAttributesToRequest(attributes); |
| 15 | + |
| 16 | + // THEN |
| 17 | + expect(result).toEqual({ |
| 18 | + test: { |
| 19 | + id: true, |
| 20 | + value: true, |
| 21 | + }, |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + it("should add dropdown specific fields for dropdown attributes", () => { |
| 26 | + // GIVEN |
| 27 | + const attributes: Array<AttributeSchema> = [ |
| 28 | + generateAttributeSchema({ name: "test", kind: "Dropdown" }), |
| 29 | + ]; |
| 30 | + |
| 31 | + // WHEN |
| 32 | + const result = addAttributesToRequest(attributes); |
| 33 | + |
| 34 | + // THEN |
| 35 | + expect(result).toEqual({ |
| 36 | + test: { |
| 37 | + id: true, |
| 38 | + value: true, |
| 39 | + color: true, |
| 40 | + description: true, |
| 41 | + label: true, |
| 42 | + }, |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + it("should add metadata fields when withMetadata is true", () => { |
| 47 | + // GIVEN |
| 48 | + const attributes: Array<AttributeSchema> = [ |
| 49 | + generateAttributeSchema({ name: "test", kind: "Text" }), |
| 50 | + ]; |
| 51 | + |
| 52 | + // WHEN |
| 53 | + const result = addAttributesToRequest(attributes, { withMetadata: true }); |
| 54 | + |
| 55 | + // THEN |
| 56 | + expect(result).toEqual({ |
| 57 | + test: { |
| 58 | + id: true, |
| 59 | + value: true, |
| 60 | + updated_at: true, |
| 61 | + is_default: true, |
| 62 | + is_from_profile: true, |
| 63 | + is_protected: true, |
| 64 | + is_visible: true, |
| 65 | + source: { |
| 66 | + id: true, |
| 67 | + display_label: true, |
| 68 | + __typename: true, |
| 69 | + }, |
| 70 | + owner: { |
| 71 | + id: true, |
| 72 | + display_label: true, |
| 73 | + __typename: true, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should add permissions fields when withPermissions is true", () => { |
| 80 | + // GIVEN |
| 81 | + const attributes: Array<AttributeSchema> = [ |
| 82 | + generateAttributeSchema({ name: "test", kind: "Text" }), |
| 83 | + ]; |
| 84 | + |
| 85 | + // WHEN |
| 86 | + const result = addAttributesToRequest(attributes, { withPermissions: true }); |
| 87 | + |
| 88 | + // THEN |
| 89 | + expect(result).toEqual({ |
| 90 | + test: { |
| 91 | + id: true, |
| 92 | + value: true, |
| 93 | + permissions: { |
| 94 | + update_value: true, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + it("should add both metadata and permissions fields when both flags are true", () => { |
| 101 | + // GIVEN |
| 102 | + const attributes: Array<AttributeSchema> = [ |
| 103 | + generateAttributeSchema({ name: "test", kind: "Text" }), |
| 104 | + ]; |
| 105 | + |
| 106 | + // WHEN |
| 107 | + const result = addAttributesToRequest(attributes, { |
| 108 | + withMetadata: true, |
| 109 | + withPermissions: true, |
| 110 | + }); |
| 111 | + |
| 112 | + // THEN |
| 113 | + expect(result).toEqual({ |
| 114 | + test: { |
| 115 | + id: true, |
| 116 | + value: true, |
| 117 | + updated_at: true, |
| 118 | + is_default: true, |
| 119 | + is_from_profile: true, |
| 120 | + is_protected: true, |
| 121 | + is_visible: true, |
| 122 | + source: { |
| 123 | + id: true, |
| 124 | + display_label: true, |
| 125 | + __typename: true, |
| 126 | + }, |
| 127 | + owner: { |
| 128 | + id: true, |
| 129 | + display_label: true, |
| 130 | + __typename: true, |
| 131 | + }, |
| 132 | + permissions: { |
| 133 | + update_value: true, |
| 134 | + }, |
| 135 | + }, |
| 136 | + }); |
| 137 | + }); |
| 138 | +}); |
| 139 | + |
| 140 | +describe("addRelationshipsToRequest", () => { |
| 141 | + it("should return base fragment for one-to-one relationship", () => { |
| 142 | + // GIVEN |
| 143 | + const relationships: Array<RelationshipSchema> = [ |
| 144 | + generateRelationshipSchema({ name: "test", cardinality: "one" }), |
| 145 | + ]; |
| 146 | + |
| 147 | + // WHEN |
| 148 | + const result = addRelationshipsToRequest(relationships); |
| 149 | + |
| 150 | + // THEN |
| 151 | + expect(result).toEqual({ |
| 152 | + test: { |
| 153 | + node: { |
| 154 | + id: true, |
| 155 | + display_label: true, |
| 156 | + }, |
| 157 | + }, |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + it("should return edges fragment for one-to-many relationship", () => { |
| 162 | + // GIVEN |
| 163 | + const relationships: Array<RelationshipSchema> = [ |
| 164 | + generateRelationshipSchema({ name: "test", cardinality: "many" }), |
| 165 | + ]; |
| 166 | + |
| 167 | + // WHEN |
| 168 | + const result = addRelationshipsToRequest(relationships); |
| 169 | + |
| 170 | + // THEN |
| 171 | + expect(result).toEqual({ |
| 172 | + test: { |
| 173 | + edges: { |
| 174 | + node: { |
| 175 | + id: true, |
| 176 | + display_label: true, |
| 177 | + }, |
| 178 | + }, |
| 179 | + }, |
| 180 | + }); |
| 181 | + }); |
| 182 | + |
| 183 | + it("should add metadata when withMetadata is true", () => { |
| 184 | + // GIVEN |
| 185 | + const relationships: Array<RelationshipSchema> = [ |
| 186 | + generateRelationshipSchema({ name: "test", cardinality: "one" }), |
| 187 | + ]; |
| 188 | + |
| 189 | + // WHEN |
| 190 | + const result = addRelationshipsToRequest(relationships, { withMetadata: true }); |
| 191 | + |
| 192 | + // THEN |
| 193 | + expect(result).toEqual({ |
| 194 | + test: { |
| 195 | + node: { |
| 196 | + id: true, |
| 197 | + display_label: true, |
| 198 | + }, |
| 199 | + properties: { |
| 200 | + is_visible: true, |
| 201 | + is_protected: true, |
| 202 | + updated_at: true, |
| 203 | + source: { |
| 204 | + id: true, |
| 205 | + display_label: true, |
| 206 | + __typename: true, |
| 207 | + }, |
| 208 | + owner: { |
| 209 | + id: true, |
| 210 | + display_label: true, |
| 211 | + __typename: true, |
| 212 | + }, |
| 213 | + }, |
| 214 | + }, |
| 215 | + }); |
| 216 | + }); |
| 217 | + |
| 218 | + it("should handle multiple relationships", () => { |
| 219 | + // GIVEN |
| 220 | + const relationships: Array<RelationshipSchema> = [ |
| 221 | + generateRelationshipSchema({ name: "one", cardinality: "one" }), |
| 222 | + generateRelationshipSchema({ name: "many", cardinality: "many" }), |
| 223 | + ]; |
| 224 | + |
| 225 | + // WHEN |
| 226 | + const result = addRelationshipsToRequest(relationships); |
| 227 | + |
| 228 | + // THEN |
| 229 | + expect(result).toEqual({ |
| 230 | + one: { |
| 231 | + node: { |
| 232 | + id: true, |
| 233 | + display_label: true, |
| 234 | + }, |
| 235 | + }, |
| 236 | + many: { |
| 237 | + edges: { |
| 238 | + node: { |
| 239 | + id: true, |
| 240 | + display_label: true, |
| 241 | + }, |
| 242 | + }, |
| 243 | + }, |
| 244 | + }); |
| 245 | + }); |
| 246 | +}); |
0 commit comments