|
| 1 | +// import "@graphql-codegen/testing"; |
| 2 | +import { Types } from "@graphql-codegen/plugin-helpers"; |
| 3 | +import { buildSchema } from "graphql"; |
| 4 | +import { plugin } from "../src/index"; |
| 5 | + |
| 6 | +describe("yup", () => { |
| 7 | + test.each([ |
| 8 | + [ |
| 9 | + "required", |
| 10 | + /* GraphQL */ ` |
| 11 | + input PrimitiveInput { |
| 12 | + a: ID! |
| 13 | + b: String! |
| 14 | + c: Boolean! |
| 15 | + d: Int! |
| 16 | + e: Float! |
| 17 | + } |
| 18 | + `, |
| 19 | + [ |
| 20 | + "export function PrimitiveInputSchema(): yup.SchemaOf<PrimitiveInput>", |
| 21 | + "a: yup.string().required()", |
| 22 | + "b: yup.string().required()", |
| 23 | + "c: yup.boolean().required()", |
| 24 | + "d: yup.number().required()", |
| 25 | + "e: yup.number().required()", |
| 26 | + ], |
| 27 | + ], |
| 28 | + [ |
| 29 | + "optional", |
| 30 | + /* GraphQL */ ` |
| 31 | + input PrimitiveInput { |
| 32 | + a: ID |
| 33 | + b: String |
| 34 | + c: Boolean |
| 35 | + d: Int |
| 36 | + e: Float |
| 37 | + z: String! # no required check |
| 38 | + } |
| 39 | + `, |
| 40 | + [ |
| 41 | + "export function PrimitiveInputSchema(): yup.SchemaOf<PrimitiveInput>", |
| 42 | + // alphabet order |
| 43 | + "a: yup.string(),", |
| 44 | + "b: yup.string(),", |
| 45 | + "c: yup.boolean(),", |
| 46 | + "d: yup.number(),", |
| 47 | + "e: yup.number(),", |
| 48 | + ], |
| 49 | + ], |
| 50 | + [ |
| 51 | + "array", |
| 52 | + /* GraphQL */ ` |
| 53 | + input ArrayInput { |
| 54 | + a: [String] |
| 55 | + b: [String!] |
| 56 | + c: [String!]! |
| 57 | + d: [[String]] |
| 58 | + e: [[String]!] |
| 59 | + f: [[String]!]! |
| 60 | + } |
| 61 | + `, |
| 62 | + [ |
| 63 | + "export function ArrayInputSchema(): yup.SchemaOf<ArrayInput>", |
| 64 | + "a: yup.array().of(yup.string()).optional(),", |
| 65 | + "b: yup.array().of(yup.string().required()).optional(),", |
| 66 | + "c: yup.array().of(yup.string().required()).required(),", |
| 67 | + "d: yup.array().of(yup.array().of(yup.string()).optional()).optional(),", |
| 68 | + "e: yup.array().of(yup.array().of(yup.string()).required()).optional(),", |
| 69 | + "f: yup.array().of(yup.array().of(yup.string()).required()).required()", |
| 70 | + ], |
| 71 | + ], |
| 72 | + [ |
| 73 | + "ref input object", |
| 74 | + /* GraphQL */ ` |
| 75 | + input AInput { |
| 76 | + b: BInput! |
| 77 | + } |
| 78 | + input BInput { |
| 79 | + c: CInput! |
| 80 | + } |
| 81 | + input CInput { |
| 82 | + a: AInput! |
| 83 | + } |
| 84 | + `, |
| 85 | + [ |
| 86 | + "export function AInputSchema(): yup.SchemaOf<AInput>", |
| 87 | + "b: yup.lazy(() => BInputSchema().required()) as never", |
| 88 | + "export function BInputSchema(): yup.SchemaOf<BInput>", |
| 89 | + "c: yup.lazy(() => CInputSchema().required()) as never", |
| 90 | + "export function CInputSchema(): yup.SchemaOf<CInput>", |
| 91 | + "a: yup.lazy(() => AInputSchema().required()) as never", |
| 92 | + ], |
| 93 | + ], |
| 94 | + [ |
| 95 | + "nested input object", |
| 96 | + /* GraphQL */ ` |
| 97 | + input NestedInput { |
| 98 | + child: NestedInput |
| 99 | + childrens: [NestedInput] |
| 100 | + } |
| 101 | + `, |
| 102 | + [ |
| 103 | + "export function NestedInputSchema(): yup.SchemaOf<NestedInput>", |
| 104 | + "child: yup.lazy(() => NestedInputSchema()) as never,", |
| 105 | + "childrens: yup.array().of(yup.lazy(() => NestedInputSchema()) as never).optional()", |
| 106 | + ], |
| 107 | + ], |
| 108 | + [ |
| 109 | + "enum", |
| 110 | + /* GraphQL */ ` |
| 111 | + enum PageType { |
| 112 | + PUBLIC |
| 113 | + BASIC_AUTH |
| 114 | + } |
| 115 | + input PageInput { |
| 116 | + pageType: PageType! |
| 117 | + } |
| 118 | + `, |
| 119 | + [ |
| 120 | + "export const PageTypeSchema = yup.mixed().oneOf([PageType.Public, PageType.BasicAuth])", |
| 121 | + "export function PageInputSchema(): yup.SchemaOf<PageInput>", |
| 122 | + "pageType: PageTypeSchema.required()", |
| 123 | + ], |
| 124 | + ], |
| 125 | + [ |
| 126 | + "camelcase", |
| 127 | + /* GraphQL */ ` |
| 128 | + input HTTPInput { |
| 129 | + method: HTTPMethod |
| 130 | + url: URL! |
| 131 | + } |
| 132 | +
|
| 133 | + enum HTTPMethod { |
| 134 | + GET |
| 135 | + POST |
| 136 | + } |
| 137 | +
|
| 138 | + scalar URL # unknown scalar, should be any (yup.mixed()) |
| 139 | + `, |
| 140 | + [ |
| 141 | + "export function HttpInputSchema(): yup.SchemaOf<HttpInput>", |
| 142 | + "export const HttpMethodSchema = yup.mixed().oneOf([HttpMethod.Get, HttpMethod.Post])", |
| 143 | + "method: HttpMethodSchema", |
| 144 | + "url: yup.mixed().required()", |
| 145 | + ], |
| 146 | + ], |
| 147 | + ])("%s", async (_, textSchema, wantContains) => { |
| 148 | + const schema = buildSchema(textSchema); |
| 149 | + const result = await plugin(schema, [], {}, {}); |
| 150 | + expect(result.prepend).toContain("import * as yup from 'yup'"); |
| 151 | + |
| 152 | + for (const wantContain of wantContains) { |
| 153 | + expect(result.content).toContain(wantContain); |
| 154 | + } |
| 155 | + }); |
| 156 | + |
| 157 | + it("with scalars", async () => { |
| 158 | + const schema = buildSchema(/* GraphQL */ ` |
| 159 | + input Say { |
| 160 | + phrase: Text! |
| 161 | + times: Count! |
| 162 | + } |
| 163 | +
|
| 164 | + scalar Count |
| 165 | + scalar Text |
| 166 | + `); |
| 167 | + const result = await plugin( |
| 168 | + schema, |
| 169 | + [], |
| 170 | + { |
| 171 | + scalars: { |
| 172 | + Text: "string", |
| 173 | + Count: "number", |
| 174 | + }, |
| 175 | + }, |
| 176 | + {} |
| 177 | + ); |
| 178 | + expect(result.content).toContain("phrase: yup.string().required()"); |
| 179 | + expect(result.content).toContain("times: yup.number().required()"); |
| 180 | + }); |
| 181 | + |
| 182 | + it("with importFrom", async () => { |
| 183 | + const schema = buildSchema(/* GraphQL */ ` |
| 184 | + input Say { |
| 185 | + phrase: String! |
| 186 | + } |
| 187 | + `); |
| 188 | + const result = await plugin( |
| 189 | + schema, |
| 190 | + [], |
| 191 | + { |
| 192 | + importFrom: "./types", |
| 193 | + }, |
| 194 | + {} |
| 195 | + ); |
| 196 | + expect(result.prepend).toContain("import { Say } from './types'"); |
| 197 | + expect(result.content).toContain("phrase: yup.string().required()"); |
| 198 | + }); |
| 199 | + |
| 200 | + it("with enumsAsTypes", async () => { |
| 201 | + const schema = buildSchema(/* GraphQL */ ` |
| 202 | + enum PageType { |
| 203 | + PUBLIC |
| 204 | + BASIC_AUTH |
| 205 | + } |
| 206 | + `); |
| 207 | + const result = await plugin( |
| 208 | + schema, |
| 209 | + [], |
| 210 | + { |
| 211 | + enumsAsTypes: true, |
| 212 | + }, |
| 213 | + {} |
| 214 | + ); |
| 215 | + expect(result.content).toContain( |
| 216 | + "export const PageTypeSchema = yup.mixed().oneOf(['PUBLIC', 'BASIC_AUTH'])" |
| 217 | + ); |
| 218 | + }); |
| 219 | + // describe("with importFrom", () => { |
| 220 | + // it("Should work", async () => { |
| 221 | + // const schema = buildSchema(/* GraphQL */ ` |
| 222 | + // "custom enum" |
| 223 | + // enum MyEnum { |
| 224 | + // "this is a" |
| 225 | + // A |
| 226 | + // "this is b" |
| 227 | + // B |
| 228 | + // } |
| 229 | + // `); |
| 230 | + // const result = (await plugin(schema, [], { |
| 231 | + // importFrom: "./generated-types", |
| 232 | + // })) as Types.ComplexPluginOutput; |
| 233 | + |
| 234 | + // expect(1).toBe(1) |
| 235 | + // }); |
| 236 | + // }); |
| 237 | +}); |
0 commit comments