Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavohenke committed Feb 5, 2023
1 parent c250ff8 commit 020cd06
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions src/express-validator.spec.ts
@@ -1,15 +1,9 @@
import {
CustomSanitizer,
CustomValidator,
FieldValidationError,
Meta,
ValidationError,
} from './base';
import { AlternativeValidationError, FieldValidationError, Meta, ValidationError } from './base';
import { ExpressValidator } from './express-validator';

describe('ExpressValidator', () => {
let isAllowedDomain: CustomValidator;
let removeEmailAttribute: CustomSanitizer;
let isAllowedDomain: jest.Mock;
let removeEmailAttribute: jest.Mock;
const createInstance = () => new ExpressValidator({ isAllowedDomain }, { removeEmailAttribute });

beforeEach(() => {
Expand Down Expand Up @@ -68,6 +62,23 @@ describe('ExpressValidator', () => {
});
});

describe('#oneOf()', () => {
it('can be used with custom chains', async () => {
const req = { query: { foo: 1 } };
const { check, oneOf } = createInstance();
isAllowedDomain.mockImplementation(() => {
throw new Error('wow');
});

const result = await oneOf([check('foo').isString(), check('foo').isAllowedDomain()], {
errorType: 'flat',
}).run(req);
const [error] = result.array();
const { nestedErrors } = error as AlternativeValidationError;
expect(nestedErrors[1]).toMatchObject({ msg: 'wow' });
});
});

describe('#checkSchema()', () => {
it('creates a schema with the extension methods', async () => {
const { checkSchema } = createInstance();
Expand Down Expand Up @@ -142,4 +153,15 @@ describe('ExpressValidator', () => {
expect(error).toBe('field: not exists');
});
});

describe('#matchedData()', () => {
it('can be used on custom chains', async () => {
const req = { query: { foo: 1 } };
const { check, matchedData } = createInstance();
await check('foo').isAllowedDomain().run(req);

const data = matchedData(req);
expect(data.foo).toBe(1);
});
});
});

0 comments on commit 020cd06

Please sign in to comment.