nestjs-rest-sample/src/post/parse-object-id.pipe.spec.ts
2020-07-03 18:33:38 +08:00

29 lines
668 B
TypeScript

import * as mongoose from 'mongoose';
import { ParseObjectIdPipe } from './parse-object-id.pipe';
describe('ParseObjectIdPipe', () => {
let isObjectId;
beforeEach(() => {
isObjectId = new ParseObjectIdPipe();
});
it('should be defined', () => {
expect(isObjectId).toBeDefined();
});
it('if valid', () => {
const validId = new mongoose.Types.ObjectId().toHexString();
const result = isObjectId.transform(validId, {} as any);
expect(result).toEqual(validId);
});
it('if invalid', () => {
try {
const result = isObjectId.transform('anerror', {} as any);
} catch (e) {
expect(e).not.toBeNull();
}
});
});