Files
trezor-suite/packages/schema-utils/tests/weak-assert.test.ts
2024-01-26 13:35:18 +01:00

44 lines
1.1 KiB
TypeScript

import { AssertWeak, Type } from '../src';
describe('weak-assert', () => {
it('should not throw if type is mismatched', () => {
const schema = Type.Object({
foo: Type.String(),
});
const value = {
foo: 123,
};
console.warn = jest.fn();
expect(() => AssertWeak(schema, value)).not.toThrow();
expect(console.warn).toHaveBeenCalledTimes(1);
});
it('should not throw if a property is mismatched in a union', () => {
const schema = Type.Union([
Type.Object({
foo: Type.String(),
}),
Type.Object({
bar: Type.Number(),
}),
]);
const value = {
foo: 123,
};
console.warn = jest.fn();
expect(() => AssertWeak(schema, value)).not.toThrow();
expect(console.warn).toHaveBeenCalledTimes(1);
});
it('should throw if a required field is missing', () => {
const schema = Type.Object({
foo: Type.String(),
});
const value = {};
expect(() => AssertWeak(schema, value)).toThrow();
});
});