Skip to content

Commit

Permalink
Reusing validation setup steps between tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gudahtt committed Apr 20, 2022
1 parent 2b72d86 commit b5f0d87
Showing 1 changed file with 27 additions and 36 deletions.
63 changes: 27 additions & 36 deletions src/sign-typed-data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,22 @@ function getEip712SolidityTypes() {

const eip712SolidityTypes = getEip712SolidityTypes();

/**
* Validate the given message with the typed message schema.
*
* @param typedMessage - The typed message to validate.
* @returns Whether the message is valid.
*/
function validateTypedMessageSchema(
typedMessage: Record<string, unknown>,
): boolean {
const ajv = new Ajv();
const validate = ajv.compile(TYPED_MESSAGE_SCHEMA);
return validate(typedMessage);
}

describe('TYPED_MESSAGE_SCHEMA', () => {
it('should match valid typed message', () => {
const ajv = new Ajv();
const validate = ajv.compile(TYPED_MESSAGE_SCHEMA);
const typedMessage = {
domain: {},
message: {},
Expand All @@ -49,12 +61,10 @@ describe('TYPED_MESSAGE_SCHEMA', () => {
},
};

expect(validate(typedMessage)).toBe(true);
expect(validateTypedMessageSchema(typedMessage)).toBe(true);
});

it('should allow custom types in addition to domain', () => {
const ajv = new Ajv();
const validate = ajv.compile(TYPED_MESSAGE_SCHEMA);
const typedMessage = {
domain: {},
message: {},
Expand All @@ -65,14 +75,11 @@ describe('TYPED_MESSAGE_SCHEMA', () => {
},
};

expect(validate(typedMessage)).toBe(true);
expect(validateTypedMessageSchema(typedMessage)).toBe(true);
});

for (const solidityType of eip712SolidityTypes) {
// eslint-disable-next-line no-loop-func
eip712SolidityTypes.forEach((solidityType) => {
it(`should allow custom type to have type of '${solidityType}'`, () => {
const ajv = new Ajv();
const validate = ajv.compile(TYPED_MESSAGE_SCHEMA);
const typedMessage = {
domain: {},
message: {},
Expand All @@ -83,13 +90,11 @@ describe('TYPED_MESSAGE_SCHEMA', () => {
},
};

expect(validate(typedMessage)).toBe(true);
expect(validateTypedMessageSchema(typedMessage)).toBe(true);
});
}
});

it('should allow custom type to have a custom type', () => {
const ajv = new Ajv();
const validate = ajv.compile(TYPED_MESSAGE_SCHEMA);
const typedMessage = {
domain: {},
message: {},
Expand All @@ -101,16 +106,14 @@ describe('TYPED_MESSAGE_SCHEMA', () => {
},
};

expect(validate(typedMessage)).toBe(true);
expect(validateTypedMessageSchema(typedMessage)).toBe(true);
});

const invalidStrings = [undefined, null, 0, 1, [], {}];

for (const invalidString of invalidStrings) {
// eslint-disable-next-line no-loop-func
it(`should disallow a primary type with value '${invalidString}'`, () => {
const ajv = new Ajv();
const validate = ajv.compile(TYPED_MESSAGE_SCHEMA);
const typedMessage = {
domain: {},
message: {},
Expand All @@ -120,16 +123,14 @@ describe('TYPED_MESSAGE_SCHEMA', () => {
},
};

expect(validate(typedMessage)).toBe(false);
expect(validateTypedMessageSchema(typedMessage)).toBe(false);
});
}

const invalidObjects = [undefined, null, 0, 1, [], '', 'test'];
for (const invalidObject of invalidObjects) {
// eslint-disable-next-line no-loop-func
it(`should disallow a domain with value '${invalidObject}'`, () => {
const ajv = new Ajv();
const validate = ajv.compile(TYPED_MESSAGE_SCHEMA);
const typedMessage = {
domain: invalidObject,
message: {},
Expand All @@ -139,13 +140,11 @@ describe('TYPED_MESSAGE_SCHEMA', () => {
},
};

expect(validate(typedMessage)).toBe(false);
expect(validateTypedMessageSchema(typedMessage)).toBe(false);
});

// eslint-disable-next-line no-loop-func
it(`should disallow a message with value '${invalidObject}'`, () => {
const ajv = new Ajv();
const validate = ajv.compile(TYPED_MESSAGE_SCHEMA);
const typedMessage = {
domain: {},
message: invalidObject,
Expand All @@ -155,27 +154,23 @@ describe('TYPED_MESSAGE_SCHEMA', () => {
},
};

expect(validate(typedMessage)).toBe(false);
expect(validateTypedMessageSchema(typedMessage)).toBe(false);
});

// eslint-disable-next-line no-loop-func
it(`should disallow types with value '${invalidObject}'`, () => {
const ajv = new Ajv();
const validate = ajv.compile(TYPED_MESSAGE_SCHEMA);
const typedMessage = {
domain: {},
message: {},
primaryType: 'object',
types: invalidObject,
};

expect(validate(typedMessage)).toBe(false);
expect(validateTypedMessageSchema(typedMessage)).toBe(false);
});
}

it('should require custom type properties to have a name', () => {
const ajv = new Ajv();
const validate = ajv.compile(TYPED_MESSAGE_SCHEMA);
const typedMessage = {
domain: {},
message: {},
Expand All @@ -186,12 +181,10 @@ describe('TYPED_MESSAGE_SCHEMA', () => {
},
};

expect(validate(typedMessage)).toBe(false);
expect(validateTypedMessageSchema(typedMessage)).toBe(false);
});

it('should require custom type properties to have a type', () => {
const ajv = new Ajv();
const validate = ajv.compile(TYPED_MESSAGE_SCHEMA);
const typedMessage = {
domain: {},
message: {},
Expand All @@ -202,16 +195,14 @@ describe('TYPED_MESSAGE_SCHEMA', () => {
},
};

expect(validate(typedMessage)).toBe(false);
expect(validateTypedMessageSchema(typedMessage)).toBe(false);
});

const invalidTypes = [undefined, null, 0, 1, [], {}];

for (const invalidType of invalidTypes) {
// eslint-disable-next-line no-loop-func
it(`should disallow a type of '${invalidType}'`, () => {
const ajv = new Ajv();
const validate = ajv.compile(TYPED_MESSAGE_SCHEMA);
const typedMessage = {
domain: {},
message: {},
Expand All @@ -222,7 +213,7 @@ describe('TYPED_MESSAGE_SCHEMA', () => {
},
};

expect(validate(typedMessage)).toBe(false);
expect(validateTypedMessageSchema(typedMessage)).toBe(false);
});
}
});
Expand Down

0 comments on commit b5f0d87

Please sign in to comment.