Skip to content

Commit

Permalink
tests: runLazyRule
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 25, 2020
1 parent 655fabf commit 8d0b087
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 36 deletions.
28 changes: 7 additions & 21 deletions packages/n4s/src/enforce/compounds/__tests__/anyOf.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,13 @@ import enforce from 'enforce';
describe('AnyOf validation', () => {
describe('Base behavior', () => {
it('Should fail when all rules fail', () => {
expect(
anyOf(
'test',
enforce.isNumber(),
enforce.isUndefined()
)
).toBe(false);
expect(anyOf('test', enforce.isNumber(), enforce.isUndefined())).toBe(
false
);
});
it('Should succeed when atleast one rule applies', () => {
expect(
anyOf(
5,
enforce.isString(),
enforce.isNumber(),
enforce.isUndefined()
)
anyOf(5, enforce.isString(), enforce.isNumber(), enforce.isUndefined())
).toBe(true);
});
it('Should succeed when rule chaining', () => {
Expand All @@ -32,9 +23,7 @@ describe('AnyOf validation', () => {
).toBe(true);
});
it('Should fail with no rules', () => {
expect(
anyOf(5)
).toBe(true);
expect(anyOf(5)).toBe(true);
});
});

Expand All @@ -44,13 +33,10 @@ describe('AnyOf validation', () => {
enforce.isString(),
enforce.isNumber(),
enforce.isUndefined()
)
);

expect(() =>
enforce({ test: 4 }).anyOf(
enforce.isNumber(),
enforce.isUndefined()
)
enforce({ test: 4 }).anyOf(enforce.isNumber(), enforce.isUndefined())
).toThrow();
});
});
Expand Down
33 changes: 19 additions & 14 deletions packages/n4s/src/enforce/compounds/__tests__/shape.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ describe('Shape validation', () => {
)
).toBe(false);
});
})
});

describe('Handling of optional fields', () => {
it('Should allow optional fields to not be defined', () => {
Expand Down Expand Up @@ -364,7 +364,6 @@ describe('Shape validation', () => {
}).shape(shapeRules())
).toThrow();


enforce({
user: {
age: faker.random.number(10),
Expand All @@ -382,16 +381,22 @@ describe('Shape validation', () => {
});
});

const shapeRules = (options) => ({
user: enforce.shape({
age: enforce.isNumber().isBetween(0, 10),
friends: enforce.optional(enforce.isArray()),
id: enforce.isString(),
name: enforce.shape({
first: enforce.isString(),
last: enforce.isString(),
middle: enforce.optional(enforce.isString()),
}, options),
username: enforce.isString(),
}, options),
const shapeRules = options => ({
user: enforce.shape(
{
age: enforce.isNumber().isBetween(0, 10),
friends: enforce.optional(enforce.isArray()),
id: enforce.isString(),
name: enforce.shape(
{
first: enforce.isString(),
last: enforce.isString(),
middle: enforce.optional(enforce.isString()),
},
options
),
username: enforce.isString(),
},
options
),
});
2 changes: 1 addition & 1 deletion packages/n4s/src/enforce/compounds/shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ export function shape(obj, shapeObj, options) {
return true;
}

export const loose = (obj, shapeObj) => shape(obj, shapeObj, { loose: true });
export const loose = (obj, shapeObj) => shape(obj, shapeObj, { loose: true });
51 changes: 51 additions & 0 deletions packages/vest/src/core/test/lib/__tests__/runLazyRules.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import runLazyRules from 'runLazyRules';

describe('runLazyRules', () => {
describe('When single rule passed', () => {
it('Should run rule against value', () => {
const rule = { test: jest.fn(() => true) };
const value = { x: true };
runLazyRules(rule, value);
expect(rule.test).toHaveBeenCalledWith(value);
});

it('Should return true when successful', () => {
const res = runLazyRules({ test: () => true }, 'some_value');
expect(res).toBe(true);
});

it('Should return true when failing', () => {
const res = runLazyRules({ test: () => false }, 'some_value');
expect(res).toBe(false);
});
});

describe('When an array of rules passed', () => {
it('Should run each rule against value', () => {
const rule_1 = { test: jest.fn(() => true) };
const rule_2 = { test: jest.fn(() => true) };
const rule_3 = { test: jest.fn(() => true) };
const value = { x: true };
runLazyRules([rule_1, rule_2, rule_3], value);
expect(rule_1.test).toHaveBeenCalledWith(value);
expect(rule_2.test).toHaveBeenCalledWith(value);
expect(rule_3.test).toHaveBeenCalledWith(value);
});

it('Should return true when successful', () => {
const rule_1 = { test: () => true };
const rule_2 = { test: () => true };
const rule_3 = { test: () => true };
const res = runLazyRules([rule_1, rule_2, rule_3], 'some_value');
expect(res).toBe(true);
});

it('Should return false when false', () => {
const rule_1 = { test: () => false };
const rule_2 = { test: () => false };
const rule_3 = { test: () => false };
const res = runLazyRules([rule_1, rule_2, rule_3], 'some_value');
expect(res).toBe(false);
});
});
});

0 comments on commit 8d0b087

Please sign in to comment.