@shipengine/connect-carrier-api
Version:
This is the typescript/javascript definitions for carrier api
49 lines (42 loc) • 1.6 kB
text/typescript
import Joi from 'joi';
import { describe, expect, it } from 'vitest';
import { StandardizedStatusCodes, TrackingInfo } from '../../models';
import { CreateManifestResponseSchema } from '../create-manifest-response-schema';
import { TrackingResponseSchema } from '../tracking-response-schema';
const tracking_info: TrackingInfo = {
standardized_status_code: StandardizedStatusCodes.Unknown,
};
const validateResponse = (schema: Joi.AnySchema, result: any): string[] | undefined => {
const validationResults = schema.validate(result, {
abortEarly: false,
});
const errors = validationResults?.error?.details?.map((detail) => detail.message);
return errors;
};
describe('When validating a tracking event, optionally allow a transaction id', () => {
it('it should be allowed', () => {
const result = validateResponse(TrackingResponseSchema, {
transaction_id: '1234',
tracking_info,
});
expect(result).toBeUndefined();
});
it("it should not be an error if it's not present", () => {
const result = validateResponse(TrackingResponseSchema, {
tracking_info,
});
expect(result).toBeUndefined();
});
});
describe('When validating a manifest event, require a transaction id', () => {
it('it should be allowed', () => {
const result = validateResponse(CreateManifestResponseSchema, {
transaction_id: '1234',
});
expect(result).toBeUndefined();
});
it("it should an error if it's not present", () => {
const result = validateResponse(CreateManifestResponseSchema, {});
expect(result).toBeDefined();
});
});