Skip to content

Commit

Permalink
Add TypedDataUtils.sanitizeData tests
Browse files Browse the repository at this point in the history
This function should now be comprehensively tested.
  • Loading branch information
Gudahtt committed Aug 11, 2021
1 parent 62e962d commit 54b00e0
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2984,6 +2984,82 @@ describe('TypedDataUtils.hashType', () => {
});
});

describe('TypedDataUtils.sanitizeData', function () {
it('should return correctly formatted data unchanged', function () {
const typedMessage = {
domain: {},
message: {},
primaryType: 'Person' as const,
types: {
EIP712Domain: [{ name: 'name', type: 'string' }],
Person: [
{ name: 'name', type: 'string' },
{ name: 'wallet', type: 'address' },
],
},
};

const sanitizedTypedMessage =
sigUtil.TypedDataUtils.sanitizeData(typedMessage);

expect(sanitizedTypedMessage).toStrictEqual(typedMessage);
});

it("should add `EIP712Domain` to `types` if it's missing", function () {
const typedMessage = {
domain: {},
message: {},
primaryType: 'Person' as const,
types: {
Person: [
{ name: 'name', type: 'string' },
{ name: 'wallet', type: 'address' },
],
},
};

const sanitizedTypedMessage = sigUtil.TypedDataUtils.sanitizeData(
typedMessage as any,
);

expect(sanitizedTypedMessage).toStrictEqual({
...typedMessage,
types: { ...typedMessage.types, EIP712Domain: [] },
});
});

it('should sanitize empty object', function () {
const typedMessage = {};

const sanitizedTypedMessage = sigUtil.TypedDataUtils.sanitizeData(
typedMessage as any,
);

expect(sanitizedTypedMessage).toStrictEqual({});
});

it('should omit unrecognized properties', function () {
const expectedMessage = {
domain: {},
message: {},
primaryType: 'Person' as const,
types: {
EIP712Domain: [{ name: 'name', type: 'string' }],
Person: [
{ name: 'name', type: 'string' },
{ name: 'wallet', type: 'address' },
],
},
};
const typedMessage = { ...expectedMessage, extraStuff: 'Extra stuff' };

const sanitizedTypedMessage =
sigUtil.TypedDataUtils.sanitizeData(typedMessage);

expect(sanitizedTypedMessage).toStrictEqual(expectedMessage);
});
});

it('normalize address lower cases', function () {
const initial = '0xA06599BD35921CfB5B71B4BE3869740385b0B306';
const result = sigUtil.normalize(initial);
Expand Down

0 comments on commit 54b00e0

Please sign in to comment.