|
| 1 | +const { deepStrictEqual, strictEqual } = require('assert') |
| 2 | +const { valueByImplType, isStatusCodeError } = require('./testutils') |
| 3 | + |
| 4 | +module.exports.test = function (setup, implType) { |
| 5 | + describe('@constraint ID in INPUT_FIELD_DEFINITION treated like String', function () { |
| 6 | + const query = `mutation createBook($input: BookInput) { |
| 7 | + createBook(input: $input) { |
| 8 | + title |
| 9 | + } |
| 10 | + }` |
| 11 | + |
| 12 | + describe('#minLength', function () { |
| 13 | + before(async function () { |
| 14 | + this.typeDefs = ` |
| 15 | + type Query { |
| 16 | + books: [Book] |
| 17 | + } |
| 18 | + type Book { |
| 19 | + title: ID |
| 20 | + } |
| 21 | + type Mutation { |
| 22 | + createBook(input: BookInput): Book |
| 23 | + } |
| 24 | + input BookInput { |
| 25 | + title: ID! @constraint(minLength: 3) |
| 26 | + }` |
| 27 | + |
| 28 | + this.request = await setup({ typeDefs: this.typeDefs }) |
| 29 | + }) |
| 30 | + |
| 31 | + it('should pass', async function () { |
| 32 | + const { body, statusCode } = await this.request |
| 33 | + .post('/graphql') |
| 34 | + .set('Accept', 'application/json') |
| 35 | + .send({ query, variables: { input: { title: 'he💩' } } }) |
| 36 | + |
| 37 | + strictEqual(statusCode, 200) |
| 38 | + deepStrictEqual(body, { data: { createBook: null } }) |
| 39 | + }) |
| 40 | + |
| 41 | + it('should fail', async function () { |
| 42 | + const { body, statusCode } = await this.request |
| 43 | + .post('/graphql') |
| 44 | + .set('Accept', 'application/json') |
| 45 | + .send({ query, variables: { input: { title: 'a💩' } } }) |
| 46 | + |
| 47 | + isStatusCodeError(statusCode, implType) |
| 48 | + strictEqual(body.errors[0].message, |
| 49 | + 'Variable "$input" got invalid value "a💩" at "input.title"' + valueByImplType(implType, '; Expected type "title_String_NotNull_minLength_3"') + '. Must be at least 3 characters in length') |
| 50 | + }) |
| 51 | + |
| 52 | + it('should fail with empty id', async function () { |
| 53 | + const { body, statusCode } = await this.request |
| 54 | + .post('/graphql') |
| 55 | + .set('Accept', 'application/json') |
| 56 | + .send({ query, variables: { input: { title: '' } } }) |
| 57 | + |
| 58 | + isStatusCodeError(statusCode, implType) |
| 59 | + strictEqual(body.errors[0].message, |
| 60 | + 'Variable "$input" got invalid value "" at "input.title"' + valueByImplType(implType, '; Expected type "title_String_NotNull_minLength_3"') + '. Must be at least 3 characters in length') |
| 61 | + }) |
| 62 | + }) |
| 63 | + }) |
| 64 | +} |
0 commit comments