|
| 1 | +import { faker } from '@faker-js/faker'; |
| 2 | +import assert from 'assert'; |
| 3 | + |
| 4 | +import { RecommendationsQueryVariables } from '../../generated/graphql/types'; |
| 5 | +import { |
| 6 | + GlobalRecsQueryParameterStrings, |
| 7 | + handleQueryParameters, |
| 8 | + setDefaultsAndCoerceTypes, |
| 9 | +} from './inputs'; |
| 10 | + |
| 11 | +import { APIError, APIErrorResponse, BFFFxError } from '../../bfffxError'; |
| 12 | + |
| 13 | +describe('input.ts recommendations query parameters', () => { |
| 14 | + describe('setDefaultsAndCoerceTypes', () => { |
| 15 | + it('converts count to an integer and passes through others', () => { |
| 16 | + const res = setDefaultsAndCoerceTypes({ |
| 17 | + count: '3', |
| 18 | + locale_lang: 'preValidatedLocale', |
| 19 | + region: 'preValidatedRegion', |
| 20 | + }); |
| 21 | + expect(res).toMatchObject({ |
| 22 | + count: 3, |
| 23 | + locale: 'preValidatedLocale', |
| 24 | + region: 'preValidatedRegion', |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + it('sets count to 20 if no default is provided, values without defaults are undefined', () => { |
| 29 | + const res = setDefaultsAndCoerceTypes({}); |
| 30 | + // validation should return an error in this case, validating defaults though |
| 31 | + expect(res).toMatchObject({ |
| 32 | + count: 20, |
| 33 | + }); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('handleQueryParameters', () => { |
| 38 | + it('returns errors if invalid query parameters', () => { |
| 39 | + const params: GlobalRecsQueryParameterStrings = { |
| 40 | + count: '-1', |
| 41 | + }; |
| 42 | + |
| 43 | + const error = handleQueryParameters(params); |
| 44 | + assert(error instanceof BFFFxError); |
| 45 | + const errors = JSON.parse(error.stringResponse); |
| 46 | + expect(errors).toEqual( |
| 47 | + expect.objectContaining<APIErrorResponse>({ |
| 48 | + errors: expect.arrayContaining<Array<APIError>>([ |
| 49 | + expect.objectContaining<APIError>({ |
| 50 | + status: '400', |
| 51 | + title: 'Bad Request', |
| 52 | + }), |
| 53 | + ]), |
| 54 | + }) |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it('returns GraphQL query variables on success', () => { |
| 59 | + const params: GlobalRecsQueryParameterStrings = { |
| 60 | + count: faker.datatype.number({ min: 1, max: 30 }).toString(), |
| 61 | + locale_lang: 'fr', |
| 62 | + region: 'FR', |
| 63 | + }; |
| 64 | + |
| 65 | + const variables = handleQueryParameters(params); |
| 66 | + expect(variables).toStrictEqual( |
| 67 | + expect.objectContaining<RecommendationsQueryVariables>({ |
| 68 | + count: parseInt(params.count, 10), |
| 69 | + locale: params.locale_lang, |
| 70 | + region: params.region, |
| 71 | + }) |
| 72 | + ); |
| 73 | + }); |
| 74 | + }); |
| 75 | +}); |
0 commit comments